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.
- I have two prices for each item, one is a member price and one is a retail price.
- I want everyone to see each variation but not be able to select the one that isn’t available to them, so that non-members can see the retail price but can’t select it, and vice versa.
- The other customization I want is the following: I want to be able to only allow members to buy 5 products at member price per month, and then it will switch them over to retail price, so I need to be able to grey out the member price for members based on certain conditions as well.
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.
Advertisement
Answer
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…