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

iih3973s  于 2022-10-30  发布在  PHP
关注(0)|答案(2)|浏览(149)

我正在研究一种方法来显示默认的变化值为同一产品根据他的类别。例如,我卖一张卡与选项蓝色和红色。当用户来的类别一,我希望默认值是蓝色。如果他来的类别二,该值将是红色。
我找到了一个钩子woocommerce_product_default_attributes,但是我不知道怎么用。

**注意:似乎woocommerce只识别每个产品的一个类别,即使您的产品在两个类别中。
示例
(编辑)

我有一个产品**P
产品
P分为两类:Cat 1Cat 2
此外,产品
P有两个变量:一月六日一月七日**
当用户输入**Cat 1时,我希望默认值为Blue。如果用户输入Cat 2,则该值将为Red**。

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

当我转到Cat 1Cat 2时,我可以看到对于Woocommerce,产品只在Cat 1中,即使我们可以通过这两个类别访问。
所以在一切之前,我需要解决woocommerce的问题。

q1qsirdb

q1qsirdb1#

由于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方法时,可以使用基于对象类型和方法名称的动态挂钩 (基本上对于前端:“查看”上下文)...

请参阅中的这一行$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
中的此行return '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上查看

wbgh16ku

wbgh16ku2#

新的不同的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**action挂接中实现您的条件函数。

备注:

  • 产品属性分类始终以“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文件中,或者也可以在任何插件文件中。
这段代码已经过测试并且可以正常工作。

相关问题