Siobahn Hotaling
Siobahn Hotaling

Reputation: 33

Grey out Woocommerce product variation based on conditions?

I've got a customized WooCommerce shop where I want to be able to grey out certain product variations based on conditions, but I'm not sure where in the code to do it.

Can anyone point me to the files/hooks/actions where I can inject some custom code into the variation output so I can make this happen?

Thanks.

Upvotes: 2

Views: 1032

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

For your two questions you can use in a different way woocommerce_add_to_cart_validation filter hook. With some conditions based on your users roles and on existing function arguments, customer will be able to select a variation of your product, but will not be able to added it to cart, if he is not allowed. You can even display a notice when customer try to add something when is not allowed…

add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 10, 5 );
function custom_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {

    // Your code goes here

    // If $passed is set to "false", the product is not added
    // $passed = false;

    return $passed;
}

For your last question, you will need to set/update some custom user meta data regarding the 5 products by month for the members, each time they place an order. You can use for this the woocommerce_thankyou or woocommerce_order_status_completed action hooks to set that custom data in the user meta data. Then you will be able to check this data enabling or disabling the right product variation in the woocommerce_add_to_cart_validation filter hook.

To finish, if you really want to grey out some variations, you will have to use Javascript/jQuery, but is going to be quite difficult as there is already a lot of javascript/Ajax WooCommerce events on product variations, that will conflict yours. But in WooCommerce everything is possible depending on time and skills…

Upvotes: 1

Related Questions