Set Woocommerce product availability by custom field


I added a custom field at products which i called it custom_product_availability

when i set the value to 1 product should be unavailable. For this i made this code

add_filter('woocommerce_is_purchasable', 'my_product_is_purchasable', 10, 2);
function my_product_is_purchasable($purchasable, $product) {
global $woocommerce;

$product_disabled =  get_post_meta( $product->get_id(), 'custom_product_availability', true );

        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );                

        add_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

The main purpose is when i set the product unavailable to remove the add to cart.
Code works. When i set the custom_product_availability to 1 cart disappears but set all other products unavailable for purchase (add to cart appears but when i add this to cart returns woocommerce message that product is not available)

how could i fix this?

Matina1985 2 months 0 Answers 17 views 0

Leave an answer