drrw8672 2017-05-11 02:20
浏览 86
已采纳

产品小部件中的WooCommerce库存状态

I have a client who is wanting to display the status of product availability in his future website. We know that you can see the status in the single product page (ie: 5 in stock) and that it's possible to display it in the store archive and category pages. However, I can't find a solution where you can display the stock status in the product widget.

Can this be achieved?

I looked in the content-widget-product.php template and tried adding:

<?php echo wp_kses_post( $availability ); ?>

But it didn't work.

Any help is much appreciated.

Thanks

  • 写回答

2条回答 默认 最新

  • dss524049 2017-05-11 03:25
    关注

    Find below the WooCommerce source code for the template content-widget-product.php (product widget) with some customization at the end, to get stock Status:

    <?php
    /**
     * ... / ...
     * @see     https://docs.woocommerce.com/document/template-structure/
     * @author  WooThemes
     * @package WooCommerce/Templates
     * @version 2.5.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    global $product; ?>
    
    <li>
        <a href="<?php echo esc_url( $product->get_permalink() ); ?>">
            <?php echo $product->get_image(); ?>
            <span class="product-title"><?php echo $product->get_name(); ?></span>
        </a>
        <?php if ( ! empty( $show_rating ) ) : ?>
            <?php echo wc_get_rating_html( $product->get_average_rating() ); ?>
        <?php endif; ?>
        <?php echo $product->get_price_html(); ?>
        <?php 
            // Compatibility for WC versions from 2.5.x to 3.0+
            if ( method_exists( $product, 'get_stock_status' ) ) {
                $stock_status = $product->get_stock_status(); // For version 3.0+
            } else {
                $stock_status = $product->stock_status; // Older than version 3.0
            }
            echo ' '.$stock_status;
        ?>
    </li>
    

    (This code is compatible from WooCommerce version 2.5.x to 3.0+)

    This code is tested and works.

    You can display the product Stock quantity using WC_Product get_stock_quantity() method.


    Official documentation: WC_Product method get_stock_status()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失