【问题标题】:Display WooCommerce size product attribute on shop page在商店页面上显示 WooCommerce 尺寸产品属性
【发布时间】:2022-01-26 22:29:30
【问题描述】:

我在商店页面上展示了产品的变体,但它的变体缺货。如何仅显示有货的变体?

按照我的代码:

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

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    我找到了这样的解决方案,也许我正在分享它作为其他人的需要。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多