【问题标题】:Display variation stock status on single dropdown variable products in Wocommerce 3 [closed]在 Woocommerce 3 中显示单个下拉变量产品的变化库存状态 [关闭]
【发布时间】:2020-11-25 10:24:13
【问题描述】:

我正在使用Show stock status next to each attribute value in WooCommerce variable products 答案代码在可变产品页面的单个产品属性下拉列表中显示变体库存状态。

这很好用,但加载产品需要花费太多时间。

如何优化代码以使其加载更快?

【问题讨论】:

    标签: php wordpress woocommerce taxonomy-terms product-variations


    【解决方案1】:

    改用下面的,会更轻一些(所以可变产品应该可以快速加载)

    add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name' );
    function customizing_variations_terms_name( $term_name ){
        global $product;
    
        if( is_admin() ) return $term_name; // Only on frontend single products
    
        // Iterating through each visible product variation Ids
        foreach( $product->get_visible_children() as $variation_id ){
            $variation = new WC_Product_Variation( $variation_id );
    
            $stock_status = $variation->get_stock_status();
            $stock_qty    = $variation->get_stock_quantity();
    
                // The attributes taxonomy key and slug value for this variation
                $attributes = $variation->get_attributes();
    
            // Caution: Works only for 1 attribute set in the product
            if(count($attributes) == 1 ) {
                $attributes_keys = array_keys($attributes);
                $attr_taxonomy   = str_replace('attribute_', '', reset($attributes_keys) );
                if( $variation->get_attribute( $attr_taxonomy ) === $term_name ) {
                    break; // stop the loop
                }
            }
            $term_name .= ' - ' . $stock_status;
            $term_name  = $stock_qty > 0 ? $term_name . ' ('.$stock_qty.')' : $term_name;
        }
        return $term_name;
    }
    

    【讨论】:

    • 我有 4 个属性,库存取决于第四个,我应该做一些特别的事情吗?
    猜你喜欢
    • 2018-05-25
    • 2017-12-15
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多