• Resolved wpuser293

    (@wpuser293)


    Hi team, I am trying to hide for specific product variations the Add to Cart button. I found a snippet which should do the work but somehow doesnt work for me. I created and attribute test and a value test. But the add to cart button shows anyway. Can you let me know how I could solve that?

    add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
    function conditional_variation_is_purchasable( $purchasable, $product ) {
    
        ## ---- Your settings ---- ##
    
        $taxonomy  = 'test';
        $term_name =  'test';
    
        ## ---- The active code ---- ##
    
        $found = false;
    
        // Loop through all product attributes in the variation
        foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
            $attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
            $term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
            // Searching for attribute 'pa_size' with value 'XL'
            if($attribute_taxonomy == $taxonomy && $term->slug == $term_name ){
                $found = true;
                break;
            }
        }
    
        if( $found )
            $purchasable = false;
    
        return $purchasable;
    }
    
    • This topic was modified 1 year, 7 months ago by wpuser293.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Brad Dalton

    (@wordpresssites)

    That code works for taxonomy terms not product attributes.

    Plugin Support Raif D. a11n

    (@rdeari)

    Hi @wpuser293 !

    Trying Chat GPT here is another snippet that could work for this case:

    // Add this code to your theme's functions.php file or a custom plugin
    
    function hide_add_to_cart_button_for_specific_variations($variation_id) {
        // Define an array of variation IDs for which you want to hide the Add to Cart button
        $variation_ids_to_hide = array(123, 456); // Replace these with the actual variation IDs
    
        // Check if the current variation is in the list of variation IDs to hide
        if (in_array($variation_id, $variation_ids_to_hide)) {
            // If the variation is in the list, remove the Add to Cart button
            remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10);
        }
    }
    
    add_action('woocommerce_before_single_product_summary', 'hide_add_to_cart_button_for_specific_variations', 10);
    
    // Optionally, you can also hide the button on the shop loop for these variations
    function hide_add_to_cart_button_on_shop_loop_for_specific_variations($variation_id) {
        // Define an array of variation IDs for which you want to hide the Add to Cart button on the shop loop
        $variation_ids_to_hide_on_shop_loop = array(123, 456); // Replace these with the actual variation IDs
    
        // Check if the current variation is in the list of variation IDs to hide on the shop loop
        if (in_array($variation_id, $variation_ids_to_hide_on_shop_loop)) {
            // If the variation is in the list, remove the Add to Cart button on the shop loop
            remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
        }
    }
    
    add_action('woocommerce_before_shop_loop_item', 'hide_add_to_cart_button_on_shop_loop_for_specific_variations', 10);
    

    Otherwise, we are leaving this open for someone from the community to help further!

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hide Add to cart button for specific product variations’ is closed to new replies.