【问题标题】:Hide Add to Cart button in Woocommerce product variations for a specific attribute value隐藏特定属性值的 Woocommerce 产品变体中的“添加到购物车”按钮
【发布时间】:2018-03-27 14:10:34
【问题描述】:

在 Woocommerce 中,我试图隐藏“添加到购物车”按钮,以显示属性之一具有特定选定值的变体。每个变体都有两个属性(pa_color 和 pa_size) 例如对于可变产品,我们有以下选项:

1) Red - XL 2)Red - XXL 3)Blue - M 4)Blue - XL

我想隐藏 XL 的添加到购物车按钮,因此用户无法将具有 XL 的选项添加到购物车(本例中为 1 和 4)

附注: 我们不想禁用变体,因此可以通过选择此选项来显示变体图像,因此停用变体或删除价格......不是我们的解决方案。

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy variations


    【解决方案1】:

    这是在产品属性 "pa_size" 具有 "XL" 值的产品变体上使 添加到购物车按钮无效 的方法:

    add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
    function conditional_variation_is_purchasable( $purchasable, $product ) {
    
        ## ---- Your settings ---- ##
    
        $taxonomy  = 'pa_size';
        $term_name =  'XL';
    
        ## ---- The active code ---- ##
    
        $found = false;
    
        // Loop through all product attributes in the variation
        foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
            $attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
            $term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
            // Searching for attribute 'pa_size' with value 'XL'
            if($attribute_taxonomy == $taxonomy && $term->name == $term_name ){
                $found = true;
                break;
            }
        }
    
        if( $found )
            $purchasable = false;
    
        return $purchasable;
    }
    

    代码进入您活动的子主题(或主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢。我似乎无法让它与我的自定义属性或仅包含文本的属性一起使用。它与只有数字的属性完美配合。
    【解决方案2】:

    使用您的 slug 文本作为 term_name

    add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
    function conditional_variation_is_purchasable( $purchasable, $product ) {
    
        ## ---- Your settings ---- ##
    
        $taxonomy  = 'pa_size';
        $term_name =  'XL';
    
        ## ---- The active code ---- ##
    
        $found = false;
    
        // Loop through all product attributes in the variation
        foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
            $attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
            $term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
            // Searching for attribute 'pa_size' with value 'XL'
            if($attribute_taxonomy == $taxonomy && $term->slug == $term_name ){
                $found = true;
                break;
            }
        }
    
        if( $found )
            $purchasable = false;
    
        return $purchasable;
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多