Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default variation value for the same product according to the category woocommerce

I'm working on a way to display default variations value for the SAME product according to the category he is. For example, I sell a card with option blue & red. When the user comes by the category ONE, I want the default value is blue. If he comes by the category TWO, the value will be red.

I found a hook woocommerce_product_default_attributes, but I don't know how to use it .

Note : It seems that woocommerce recognize only one category per product even if your product is in two category.


Example (edit):

I have a product P.
Product P is in two categories : Cat 1 & Cat 2.
Also, product P has two variables : Blue & Red

When the user comes by Cat 1, I want the default value is Blue. If he comes by Cat 2, the value will be Red.

The answer code of @LoicTheAztech (below) works, BUT:

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.

So before everything, I need to solve the woocommerce issue.

like image 444
Jean R. Avatar asked Dec 03 '25 13:12

Jean R.


1 Answers

New updated answer HERE with the replacement filter hook, as since WooCommerce 3, get_variation_default_attributes() method is replaced by get_default_attributes() and woocommerce_product_get_default_attributes is the new related filter hook to be used.


Before WooCommerce 3, the filter hook woocommerce_product_default_attributes was located in get_variation_default_attributes() deprecated method, so it's not really the right hook to achieve what you want.

You can achieve your conditional function in woocommerce_before_add_to_cart_form action hook, for example.

Notes:

  • Product Attribute taxonomy always begin by 'pa_' + the attribute slug
  • You need to set in for variable products the default value for this attribute in the variation tab settings.

The code:

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 );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

like image 147
LoicTheAztec Avatar answered Dec 06 '25 01:12

LoicTheAztec