【问题标题】:woocommerce shortcode with stock quantity带有库存数量的 woocommerce 简码
【发布时间】:2020-07-10 14:11:50
【问题描述】:

我想要一个类似于以下示例的简码,用于显示产品的库存:

[products limit="8" columns="4" category="pantalones" cat_operator="IN"]

我需要这样的东西:

[products limit="8" columns="4" category="pantalones" cat_operator="IN" showstock="yes"]

我几天前找到了这段代码,我在stackoverflow上找到了它,但是现在我不记得链接了,对不起。:

add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );

function minimum_stock_shortcode( $atts ) {

global $woocommerce_loop;

// Attributes 
        $atts = shortcode_atts(
            array(
            'limit'         => '40',
            'columns'       => '5',
            'orderby'       => 'title',
            'order'         => 'asc',
            'category'      => '',
            'cat_operator'  => 'IN',
            'stock'         => '',
            ),
            $atts, 'minimum_stock'
        );

        $args = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'posts_per_page'        => $atts['limit'],
            'orderby'               => $atts['orderby'],
            'order'                 => $atts['order'],
            'meta_query'            => array(
                array(
                    'key'           => '_stock',
                    'value'         => $atts['stock'],
                    'compare'       => '>='
                )
            ),
            'tax_query'             => array(
                array(
                    'taxonomy'      => 'product_cat',
                    'field'         => 'slug',
                    'terms'         => $atts['category'],
                )   
            )
        );


ob_start();

$products = new WP_Query( $args );

$woocommerce_loop['columns'] = $atts['columns'];

if ( $products->have_posts() ) : ?>     

    <?php woocommerce_product_loop_start(); ?>

        <?php while ( $products->have_posts() ) : $products->the_post(); ?>

            <?php woocommerce_get_template_part( 'content', 'product' ); ?>

        <?php endwhile; // end of the loop. ?>

    <?php woocommerce_product_loop_end(); ?>

<?php endif;

wp_reset_postdata();

return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}

此代码完美运行,但在主页中不显示库存

我认为解决方案不是修改简码,而是修改它用于可视化数据的模板,但我不知道该怎么做,如果有人知道它在哪里或者它是如何完成的,我深深欣赏一下

非常感谢您的宝贵时间。

【问题讨论】:

    标签: php wordpress woocommerce stock


    【解决方案1】:

    我找到了,解决办法,谢谢,非常感谢

    add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_show_stock_shop', 10 );
      
    function bbloomer_show_stock_shop() {
       global $product;
       echo wc_get_stock_html( $product );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2017-04-17
      相关资源
      最近更新 更多