Support » Plugin: WooCommerce » is_purchasable() filter doesn't work on Variable products

  • I’m building a store which It has on some products a countdown timer. When the countdown is over it reveals the Add to cart button. First I did this by removing the action for add to cart from woocommerce_single_product_summary. Then I read a more safe way to do this by applying filters. So I found the filter is_purchasable(). I just put an arg with true/false and it works.

    Except variable products. How can I make it work for variable products?

    Here is a sample working code which shows if a product is Instore or Online. It works for Simple but not for variable

    function availableOrNot()
    {
        global $product, $post;
        $field = get_field_object('availabilityInOut');
        $value = get_field('availabilityInOut');
    
        if ('instore' == $value || 'tba' == $value) {
            add_filter( 'woocommerce_is_purchasable', 'hide_add_cart');
        }
        else{
          add_filter( 'woocommerce_is_purchasable', 'show_add_cart');
        }
    }
    
    add_action('woocommerce_single_product_summary', 'availableOrNot', 10.2);

    Thank you

    https://wordpress.org/plugins/woocommerce/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mike Jolley

    (@mikejolley)

    woocommerce_variation_is_purchasable filter.

    Thread Starter hambos22

    (@hambos22)

    Hello Mike, thanks for your response. I tried that too, after digging the code and it’s not working. I put it on the code above

    Keep in mind that the second arg “hide_add_cart” is a function

    function hide_add_cart(){
    return false;
    }
    Plugin Author Mike Jolley

    (@mikejolley)

    woocommerce_variation_is_purchasable is the correct filter, but you should probably move your logic into a function hooked into woocommerce_variation_is_purchasable so you know which variation ID is being checked.

    Thread Starter hambos22

    (@hambos22)

    I’m a little bit confused right now. I don’t want to use it at variation level but at product level. I just don’t want the users to be able to buy stuff if its instore only.

    Is it safe to just remove the add to cart button by removing its action? If its safe, I will not use the is_purchasable and I’ll use the remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

    I tested this code and is not working.

    function availableOrNot()
    {
        global $product, $post;
        $field = get_field_object('availabilityInOut');
        $value = get_field('availabilityInOut');
    
        if ('instore' == $value || 'tba' == $value) {
    
            return false;
        }
        else{
          return true;
        }
    }
    
    add_filter('woocommerce_variation_is_purchasable', 'availableOrNot');
    Plugin Author Mike Jolley

    (@mikejolley)

    The filter pass an ID, so you don’t need the globals.

    e.g.

    add_filter('woocommerce_variation_is_purchasable', 'availableOrNot');
    
    function availableOrNot( $id ) {
    // code
    }

    To get meta, you need https://developer.wordpress.org/reference/functions/get_post_meta/ – I don’t know what get_field_object is.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘is_purchasable() filter doesn't work on Variable products’ is closed to new replies.