【问题标题】:Change default variation value for the same product according to the category woocommerce根据类别 woocommerce 更改同一产品的默认变体值
【发布时间】:2017-09-06 10:52:39
【问题描述】:

我正在研究一种根据他所在的类别显示 SAME 产品的默认变体值的方法。 例如,我出售一张带有蓝色和红色选项的卡片。 当用户来自类别 ONE 时,我希望默认值为蓝色。 如果他属于类别二,则值为红色。

找到了一个钩子woocommerce_product_default_attributes,但是不知道怎么用。

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


示例 (编辑)

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

当用户通过 Cat 1 来时,我希望默认值为 Blue。 如果他通过 Cat 2 来,则值为 Red

@LoicTheAztech 的答案代码 (如下) 有效,但是:

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

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

【问题讨论】:

    标签: php wordpress woocommerce attributes product


    【解决方案1】:

    从 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 文件中。尚未测试,但它应该可以工作。

    说明:

    由于 Woocommerce 3 和新引入的 CRUDS setter 方法,当方法使用 get_prop() WC_Data 方法时,可以使用基于对象类型和方法名称的动态挂钩 (主要用于前端:"查看”上下文)

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

    this linereturn 'woocommerce_' . $this->object_type . '_get_';

    所以过滤器钩子是动态制作的这样(其中$this->object_type等于'product'

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

    有 2 个参数:

    • $attributes(替换$value的属性值)
    • $product(替换$this的类实例对象)

    查看Woocommerce Github closed and solved issue

    【讨论】:

    • @Luiggity 看到这个解决问题的新答案……
    【解决方案2】:

    带有替换过滤器挂钩的新 updated answer 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_”+属性段开头
    • 您需要在变体选项卡设置中为可变产品设置此属性的默认值。

    代码:

    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 文件或任何插件文件中。

    此代码已经过测试并且可以工作。

    【讨论】:

    • 嗨@LoicTheAztec,很抱歉回答迟了,感谢您的帮助!我看到了您的代码,但我意识到一件事:Woocommerce 不允许对同一产品进行两种分类方式。事实上,SAME 产品分为两类。当我尝试您的代码时,该产品仅在一个中被识别..
    • @Efbi 这很正常,因为不可能为同一个产品设置 2 个不同的默认属性值……所以如果您的产品有类别“一”和“二”,它只适用于一个……所以可能你必须更新你的问题,因为它不够明确。
    • @nachtigall 你可以使用woocommerce_product_get_default_attributes 动态过滤钩子like in this new answer ... 因为这是一个基于Woocommerce 3 引入的新CRUDS setter 和getter 方法的动态钩子,它不会被Automatic 引用。
    • @nachtigall 我希望你没有在这里否决答案,因为我已经花了一些时间和精力来帮助你,这不公平......我同意你关于文档的看法,但是 Woocommerce/ Automatic 永远不会为这个钩子制作任何文档,因为它们就像数百个使用新的 CRUD setter 和 getter 方法......
    • @LoicTheAztec 你确实帮了我很多,我很感激,我投了反对票,以便另一个答案(更有用)起来。我不想表现出任何不尊重。我恢复了反对票并再次赞成。再次谢谢你,我是认真的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2018-08-26
    • 1970-01-01
    • 2018-09-27
    相关资源
    最近更新 更多