Repository Forums Support WooCommerce Extended Coupon Features PRO Add 2 different products and get 5% discount on specific product

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #17705
    Loes Arentsen
    Participant

    Hi there,

    For a client I’m looking for a solution.
    If somebody buys product A (variable prodyct with variable prices) and adds a product out of category B, the customer should get 5% discount on product A. So the discount depends on the chosen variation.

    Hope you can help me with this. Thanks!
    Loes

    #17709
    Soft79
    Keymaster

    You need to apply a snippet similar to https://www.soft79.nl/forums/topic/buy-product-a-get-30-off-each-product-b/#post-3029 . Are you comfortable with pasting php snippets? I can alter the snippet so it will accept category B instead of only a single product of type B.

    #17710
    Loes Arentsen
    Participant

    Yes pasting php is not a problem at all!
    Thanks so much!

    #17715
    Soft79
    Keymaster

    This snippet should work.

    Create a 5% discount coupon that only applies to product A.

    Paste the snippet in your child theme’s functions.php and replace the value after $required_cat with category B’s id and replace the value after $coupon_code with the coupon code. The snippet will invalidate the coupon if there’s no item from category B in the cart.

    
    
    add_filter( 'woocommerce_coupon_is_valid', function( $valid, $coupon, $wc_discounts ) {
        $required_cat = 6666;
        $coupon_code = 'THE_COUPON_CODE';
    
        if ( strcasecmp( $coupon->get_code(), $coupon_code ) !== 0 ) {
            return $valid;
        }
    
        if ( ! class_exists( 'WJECF_WC_Discounts' ) ) {
            return $valid;
        }
    
        if ( ! $valid ) {
            return $valid;
        }
    
        if ( is_null( $wc_discounts ) ) {
            $wc_discounts = new WJECF_WC_Discounts( WC()->cart );
        }
    
        //Get array of all cart product and variation ids
        $product_cats = array();
    
        foreach ( $wc_discounts->get_items() as $item_key => $item ) {
            if ( ! $item->product ) {
                continue;
            }
    
            $product_id = $item->product->get_id();
            if ( 'product_variation' == get_post_type( $product_id ) ) {
                $product_id = $item->product->get_parent_id();
            }
            $product_cats = array_merge( $product_cats, wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) );
        }
    
        if ( ! in_array( $required_cat, $product_cats ) ) {
            $valid = false;
        }
    
        return $valid;
    }, 10, 3 );
    
    

    Note: untested.

    #18161
    Loes Arentsen
    Participant

    Hi there,

    In addition to this: is it possible to do this with just categories as well?
    So when somemebody buys a product from category A and category B, he gets 5% discount on the product of category B.

    Hope to hear from you 🙂

    #18462
    Loes Arentsen
    Participant

    @soft79 any help on my last message?
    Thanks!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.