猿问

显示 WooCommerce 单一产品简短描述中的库存可用变体

我的产品有很多变体,其中只有少数商品实际上有库存,而大多数其他变体“可缺货”

我希望能够在每个产品页面的简短产品描述中仅显示有库存商品的快速列表,这样客户就不必逐一尝试所有变体以最终找出哪些商品有库存。

我搜索过可以执行此操作的插件或代码,但没有找到任何内容。

我发现的最接近的代码是:

add_action( 'woocommerce_after_shop_loop_item', 'bb_echo_stock_variations_loop' );

function bb_echo_stock_variations_loop(){

    global $product;


    if ( $product->get_type() == 'variable' ) {

        foreach ( $product->get_available_variations() as $key ) {

            $attr_string = array();


            foreach ( $key['attributes'] as $attr_name => $attr_value ) {

                $attr_string[] = $attr_value;

            }


            if ( $key['max_qty'] > 0 ) { 

              echo '<br/>' . implode( ', ', $attr_string ) . ': ' . $key['max_qty'] . ' in stock'; 

            } else { 

              echo '<br/>' . implode(', ', $attr_string ) . ': out of stock'; 

            }

        }

    }

}

但它在商店页面上显示“有库存”可用变体,我希望它显示在单个产品简短描述上。


如何在单个产品简短描述中显示“有库存”可用变体?


忽然笑
浏览 123回答 1
1回答

慕容3067478

要在产品单页的库存变体列表中显示简短说明,请使用以下命令:add_filter( 'woocommerce_short_description', 'display_in_stock_variations_to_short_description' );function display_in_stock_variations_to_short_description( $excerpt ){&nbsp; &nbsp; global $product;&nbsp; &nbsp; if ( ! is_product() || empty($product) || ! is_a( $product, 'WC_Product' ) )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $excerpt;&nbsp; &nbsp; if( $product->is_type('variable') ) {&nbsp; &nbsp; &nbsp; &nbsp; // Loop through visible children&nbsp; &nbsp; &nbsp; &nbsp; foreach( $product->get_children() as $variation_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variation = wc_get_product( $variation_id );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Filter 'woocommerce_hide_invisible_variations' to optionally hide invisible variations (disabled variations and variations with empty price).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( apply_filters( 'woocommerce_hide_invisible_variations', true, $product->get_id(), $variation ) && ! $variation->variation_is_visible() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $max_qty&nbsp; &nbsp; = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $term_names = []; // Initializing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop through variation attributes for current varation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $variation->get_variation_attributes() as $attribute => $term_slug ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set the term name in an array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $term_names[] = ucfirst( str_replace( ['-', '_'],[' ', ' '], $term_slug ) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $max_qty > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $excerpt .= sprintf( '<br/>%s: %s %s',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; implode(', ', $term_names),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $max_qty,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __('in stock', 'woocommerce')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $excerpt;}// Avoid additional content from product short description to be displayed in variation descriptionadd_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_desscription', 10, 3);function filter_wc_available_variation_desscription( $data, $product, $variation ) {&nbsp; &nbsp; $max_qty&nbsp; &nbsp; = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( $max_qty > 0 )&nbsp; &nbsp; &nbsp; &nbsp; $data['variation_description'] = get_post_meta( $variation->get_id(), '_variation_description', true );&nbsp; &nbsp; return $data;}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
随时随地看视频慕课网APP
我要回答