首页 文章

根据类别woocommerce更改同一产品的默认变化值

提问于
浏览
3

我正在研究根据他所属的类别显示SAME产品的默认变化值的方法 . 例如,我卖了一张带蓝色和红色选项的卡片 . 当用户来到类别ONE时,我希望默认值为蓝色 . 如果他来自类别TWO,则该值将为红色 .

我找到了一个挂钩 woocommerce_product_default_attributes ,但我不知道如何使用它 .

Note : 即使您的产品属于两类,woocommerce似乎也只能识别每个产品的一个类别 .


Example (编辑):

我有一个产品 P .
产品 P 分为两类: Cat 1Cat 2 .
此外,产品 P 有两个变量: BlueRed

当用户来自 Cat 1 时,我希望默认值为 Blue . 如果他来自 Cat 2 ,则该值将为 Red .

The answer code of @LoicTheAztech (下面)有效,但是:

当我去Cat 1或Cat 2时,我可以看到,对于Woocommerce,该产品仅在Cat 1中,即使我们可以访问这两个类别 .

所以在一切之前,我需要解决woocommerce问题 .

2 回答

  • 2

    从Woocommerce 3开始,你可以使用 woocommerce_product_get_default_attributes 过滤钩...所以你的2个产品类别的代码将是这样的:

    add_filter( 'woocommerce_product_get_default_attributes', 'filtering_product_get_default_attributes', 10, 2 );
    function filtering_product_get_default_attributes( $default_attributes, $product ){
        // We EXIT if it's not a variable product
        if( ! $product->is_type('variable') ) return $default_attributes;
    
        ## --- YOUR SETTINGS (below) --- ##
    
        // The desired product attribute taxonomy (always start with "pa_")
        $taxonomy = 'pa_color';
    
        $category1 = 'one' // The 1st product category (can be an ID, a slug or a name)
        $value1 = 'blue' // The corresponding desired attribute slug value for $category1
    
        $category2 = 'two' // The 2nd product category (can be an ID, a slug or a name)
        $value2 = 'red' // The corresponding desired attribute slug value for $category2
    
        ## --- The code --- ##
    
        // Get the default attribute used for variations for the defined taxonomy
        $default_attribute = $product->get_variation_default_attribute( $taxonomy );
    
        // We EXIT if define Product Attribute is not set for variation usage
        if( empty( $default_attribute ) ) return $default_attributes;
    
        // For product category slug 'one' => attribute slug value "blue"
        if( has_term( 'one', 'product_cat', $product->get_id() ) )
            $default_attributes[$taxonomy] = 'blue';
    
        // For product category slug 'two' => attribute slug value "red"
        elseif( has_term( 'two', 'product_cat', $product->get_id() ) )
            $default_attributes[$taxonomy] = 'red';
    
        else return $default_attributes; // If no product categories match we exit
    
        return $default_attributes; // Always return the values in a filter hook
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 . 尚未测试,但它应该工作 .

    Explanations:

    自Woocommerce 3和新引入的CRUDS setter方法以来,当一个方法使用get_prop()WC_Data方法时,可以使用基于对象类型和方法名称的动态钩子(主要用于前端:“view”上下文)...

    See this line $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); in

    and this linereturn 'woocommerce_' . $this->object_type . 'get'; in

    所以 the filter hook is dynamically made 这样(其中 $this->object_type 等于 'product' ):

    'woocommerce_' . $this->object_type . '_get_' . 'default_attributes'
    

    有2个参数:

    • $attributes (替换 $value 的属性值)

    • $product (替换 $this 的类实例对象)...


    Woocommerce Github closed and solved issue上看到它

  • 2

    新的不同更新答案HERE与替换过滤器钩


    在WooCommerce 3中,过滤器钩子woocommerce_product_default_attributes位于get_variation_default_attributes()不推荐使用的方法中,因此它不是实现所需内容的正确钩子 . get_variation_default_attributes()方法被get_default_attributes()替换 .

    例如,您可以在 woocommerce_before_add_to_cart_form 动作钩子中实现条件函数 .

    注意:产品属性分类始终以“pa_”属性slug开头您需要为变量产品设置变体选项卡设置中此属性的默认值 .

    代码:

    add_action( 'woocommerce_before_add_to_cart_form', function(){
        global $product;
    
        // We EXIT if it's not a variable product
        if( ! $product->is_type('variable') ) return;
    
        ## DEFINE HERE the desired product attribute taxonomy
        $pa_attribute = 'pa_color';
        $default_attribute_for_variation = $product->get_variation_default_attribute( $pa_attribute );
    
        // We EXIT if Product Attribute Color is not set as variation usage
        if( empty( $default_attribute_for_variation ) ) return;
    
        // Get the array of default attributes
        $default_attributes = $product->get_default_attributes();
    
        // For product category 'ONE => Attribute "blue" slug value
        if( has_term( 'clothing', 'product_cat', $product->get_id() ) )
            $default_attributes[$pa_attribute] = 'blue';
    
        // For product category 'TWO' => Attribute "blue" slug value
        elseif( has_term( 'TWO', 'product_cat', $product->get_id() ) )
            $default_attributes[$pa_attribute] = 'red';
    
        else return; // If no product categories match we exit
    
        // If a product category match we set the default attribute
        $product->set_default_attributes( $default_attributes );
    }, 80, 0 );
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .

    此代码经过测试并可以使用 .

相关问题