簡體   English   中英

woocommerce 短代碼與庫存數量

[英]woocommerce shortcode with stock quantity

我想要一個類似於以下示例的簡碼,它顯示產品的庫存:

[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>';
}

此代碼完美運行,但未在主頁顯示庫存

我認為解決方案不是修改簡碼,而是修改它用於可視化數據的模板,但我不知道該怎么做,如果有人知道它在哪里或如何完成,我深表感謝

非常感謝您的時間。

我找到了,解決辦法,謝謝,非常感謝

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 );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM