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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题