• Resolved Kazam Creative

    (@goldenagemedia)


    Hi guys,

    I’ve seen a similar question asked to what I’m after (see link – add coupon exclusion by attribute) but a solution wasn’t provided. It was suggested that “you can exclude specific variations (say a red version of a t-shirt) from a coupon just like you would a normal product.”

    Look at any product on https://amurphyphotography.com.au/ and you’ll see I have 3 different variations per product under the attribute “Print Type” and I only want a coupon to apply to the “HD Canvas” variation. Is there a simple solution to this? I already have code that performs an action if the “Framed Print” variation is selected, but I can’t figure out how to manipulate it for this coupon issue. The code I use is below:

    /** DISABLE ADD-TO-CART BUTTON IF FRAMED PRINT VARIATION IS SELECTED */
    add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
    function conditional_variation_is_purchasable( $purchasable, $product ) {
    
        ## ---- Your settings ---- ##
        $taxonomy  = 'pa_print-type';
        $term_name =  'Framed Print';
    
        ## ---- 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->name == $term_name ){
                $found = true;
                break;
            }
        }
        if( $found )
            $purchasable = false;
    
        return $purchasable;
    }

    I’d greatly appreciate any help you could provide.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Apply Coupon to Specific Variation’ is closed to new replies.