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

我正在努力根据他的类别显示SAME产品的默认变化值.
例如,我出售的卡带有选项blue&红色.
当用户来自类别ONE时,我希望默认值为蓝色.
如果他来自两个类别,则该值将为红色.

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

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

示例(编辑):

我有一个产品P.
产品P分为两类:类别1和类别1.猫2.
而且,产品P具有两个变量:蓝色&红色

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

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

When I go to Cat 1 or Cat 2, I can see that for Woocommerce, the product is only in Cat 1, even if we can access by both category.

因此,在一切之前,我需要解决woocommerce问题.

解决方法:

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

说明:

Since Woocommerce 3 and new introduced CRUDS setter methods, when a method is using get_prop() WC_Data method, a dynamic hook based on the object type and the method name can be used (essentially for frontend: “view” context)

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

this line返回’woocommerce_’. $this-> object_type. ‘_得到_’;在

因此,以这种方式动态制作了过滤器挂钩(其中$this-> object_type等于’product’):

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

有2个参数:

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

Woocommerce Github closed and solved issue上看到

上一篇:php-如何在Woocommerce中的自定义产品类型中添加“变化”标签?


下一篇:php-当购物车中有2种特定产品类别时,显示自定义消息