簡體   English   中英

在商店頁面顯示 WooCommerce 尺寸產品屬性

[英]Display WooCommerce size product attribute on shop page

我在商店頁面上展示了產品的變體,但它有缺貨的變體。 如何僅顯示有庫存的變體?

按照我的代碼:

add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
    global $product;
    
    if ( $product->is_type('variable') ) {
        $taxonomy_size  = 'pa_numara';
        echo '<span class="attribute-size">' . $product->get_attribute($taxonomy_size) . '</span>';
    }
}

我找到了這樣的解決方案,也許我正在分享它作為其他人的需要。

add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_echo_stock_variations_loop' );
    
function bbloomer_echo_stock_variations_loop(){
    global $product;
    if ( $product->get_type() == 'variable' ) {
        foreach ( $product->get_available_variations() as $key ) {
            $variation = wc_get_product( $key['variation_id'] );
            $stock = $variation->get_availability();
            $stock_string = $stock['availability'] ? $stock['availability'] : __( 'In stock', 'woocommerce' );
            $attr_string = array();
            foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                $attr_string[] = $attr_value;
            }
            if($key['max_qty'] >= 1 ){
            echo '<div class="shop-attribute">'. implode( ', ', $attr_string ).'</div>';
            }
            else {
            echo "";
            }
        }
    }
}

暫無
暫無

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

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