phpwordpresswoocommercediscountproduct-quantity

WooCommerce apply coupon depends of cart line item quantity


After long looking, I was not able to find any proper code how would be possible to apply coupon for a cart line items. Lets say customer added some product quantity of 10, my selected coupon should be applied for that product. If he adds another product with quantity more than 10, again same coupon should to be applied for that product. Any assistance here? I was able to find something similar but this only works for specific products id, any assistance how to update this code to go through each cart products ,check their quantities and apply coupon for products which quantity is 10 or more?

Reference for similar code but only for specific products:
Conditionally apply coupons automatically for specific Product IDs and quantities

Image example:

Image example attached  here


Solution

  • tried to create a custom solution for my question below. And did it in some way, not sure if this is proper and good option, but it at least work for me as exactly I need. This creates a separate coupon for every product in a shop (if newly product added it creates an unique coupon for it as well). Coupons is applied automatically per cart line item, if product quantity is 10 or more in a cart. It gives a 10% discount for that product. Code as per below, maybe for someone will be useful as I couldn't find any plugins or codes to work like this anywhere...

    $args = array(
        'posts_per_page'   => -1,
        'orderby'          => 'title',
        'order'            => 'asc',
        'post_type'        => 'shop_coupon',
        'post_status'      => 'publish',
    );
        
    $all_coupons = get_posts( $args );
    
    // Loop through the available coupons
    foreach ( $all_coupons as $coupon ) {
        // Get the name for each coupon and add to the previously created array
        $coupon_name = $coupon->post_title;
    }
    
    foreach ($all_coupons as $coupon) {
        $coupons_array[] = $coupon->post_title;
    }
    
    $all_ids = get_posts( array(
            'post_type' => 'product',
            'numberposts' => -1,
            'post_status' => 'publish',
            'fields' => 'ids',
       ) );
    
       foreach ( $all_ids as $id ) {
           $product_id_array[] = $id;
        }
    
    // Get values from arr2 and arr1 unique
     $output = array_merge(array_diff($coupons_array, $product_id_array), array_diff($product_id_array, $coupons_array));
        
    function coupon_exists($coupon_code) {
        global $wpdb;
            $sql = $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_type = 'shop_coupon' AND post_name = '%s'", $coupon_code );
            $coupon_codes = $wpdb->get_results($sql);
            if (count($coupon_codes)> 0) {
                return true;
            }
        else {
                return false;
            }
        }
    
        foreach ($output as $o) {
            if (is_numeric($o)) {
                if (!coupon_exists($o)) {
                    generate_coupon($o);
                }
            }
    }
    
    function generate_coupon($coupon_code){
                    $coupon = new WC_Coupon();
                    $coupon->set_code($coupon_code);
                    //the coupon discount type can be 'fixed_cart', 'percent' or 'fixed_product', defaults to 'fixed_cart'
                    $coupon->set_discount_type('percent_product');
                    //the discount amount, defaults to zero
                    $coupon->set_amount(10);
                    $coupon->set_individual_use(false);
                     $coupon->set_product_ids(array($coupon_code));
                    //save the coupon
                    $coupon->save();
                    return $coupon_code;
    }
    
    
    add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
    function conditional_auto_add_coupons() {
    
           $all_ids = get_posts( array(
            'post_type' => 'product',
            'numberposts' => -1,
            'post_status' => 'publish',
            'fields' => 'ids',
           ) );
    
        if ( !WC()->cart->is_empty() ){
    
    
            // First cart loop: Counting number of subactegory items in cart
            foreach ( $all_ids as $id ){
            foreach ( WC()->cart->get_cart() as $cart_item ){
                    if( $id == $cart_item['data']->id ){
                        if( 10 <= $cart_item['quantity']  ){
                            WC()->cart->add_discount( $id );
                            //wc_add_notice( __( 'Discount of <strong>10%</strong> for quantity.', 'theme_domain' ), 'success' );
                        }else{
                             WC()->cart->remove_coupon( $id );
                             //wc_add_notice( __( 'Discount of <strong>10%</strong> due to low quantity removed.', 'theme_domain' ), 'success' );}
                    }
                }
            }
        }
    }
    }