亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

顯示 WooCommerce 單一產品簡短描述中的庫存可用變體

顯示 WooCommerce 單一產品簡短描述中的庫存可用變體

PHP
忽然笑 2023-10-15 15:26:57
我的產品有很多變體,其中只有少數商品實際上有庫存,而大多數其他變體“可缺貨”我希望能夠在每個產品頁面的簡短產品描述中僅顯示有庫存商品的快速列表,這樣客戶就不必逐一嘗試所有變體以最終找出哪些商品有庫存。我搜索過可以執行此操作的插件或代碼,但沒有找到任何內容。我發現的最接近的代碼是: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';             }        }    }}但它在商店頁面上顯示“有庫存”可用變體,我希望它顯示在單個產品簡短描述上。如何在單個產品簡短描述中顯示“有庫存”可用變體?
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

要在產品單頁的庫存變體列表中顯示簡短說明,請使用以下命令:


add_filter( 'woocommerce_short_description', 'display_in_stock_variations_to_short_description' );

function display_in_stock_variations_to_short_description( $excerpt ){

    global $product;


    if ( ! is_product() || empty($product) || ! is_a( $product, 'WC_Product' ) ) 

        return $excerpt;


    if( $product->is_type('variable') ) {

        // Loop through visible children

        foreach( $product->get_children() as $variation_id ) {

            $variation = wc_get_product( $variation_id );


            // Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.

            if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {

                continue;

            }


            // Filter 'woocommerce_hide_invisible_variations' to optionally hide invisible variations (disabled variations and variations with empty price).

            if ( apply_filters( 'woocommerce_hide_invisible_variations', true, $product->get_id(), $variation ) && ! $variation->variation_is_visible() ) {

                continue;

            }


            $max_qty    = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();

            $term_names = []; // Initializing


            // Loop through variation attributes for current varation

            foreach ( $variation->get_variation_attributes() as $attribute => $term_slug ) {

                // Set the term name in an array

                $term_names[] = ucfirst( str_replace( ['-', '_'],[' ', ' '], $term_slug ) );

            }


            if ( $max_qty > 0 ) {

                $excerpt .= sprintf( '<br/>%s: %s %s',

                    implode(', ', $term_names),

                    $max_qty,

                    __('in stock', 'woocommerce')

                );

            }

        }

    }

    return $excerpt;

}



// Avoid additional content from product short description to be displayed in variation description

add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_desscription', 10, 3);

function filter_wc_available_variation_desscription( $data, $product, $variation ) {

    $max_qty    = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();

    

    if( $max_qty > 0 )

        $data['variation_description'] = get_post_meta( $variation->get_id(), '_variation_description', true );


    return $data;

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-10-15
  • 1 回答
  • 0 關注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號