[SOLVED]Quantity Discount

Home – SaaS Forums Support questions WooPrice Calculator (Pro) [SOLVED]Quantity Discount

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #7193
    Tyler Robinson
    Participant

    There are a number of quantity discount plugins out there, but none of them seem to work in tandem with the WooPrice Calculator. I had to resort to custom coding a quantity discount option and it works for the most part. Have you come across a quantity discount plugin that works with WooPrice Calculator?

    I have managed to get the correct discounted price per quantity to show in the cart with the correct total price via the code below, but it only works correctly when one product is added to the cart. If you add two or more, then the price doesn’t accurately show. The total at the bottom is correct, but the price for the products doesn’t show correctly. If I delete the second product from the cart, then the remaining product price shows correctly. I need to figure out how to make them show correctly when multiple products are added as this will confuse the buyer when they see the total (although correct) doesn’t match each product price.

    To do this, I have added custom fields to the products in the back-end that the discount can be applied to. Can anyone help shed some light on this?

    Here is what I have come up with so far in my functions.php file

    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    echo '

    ';

    // Text Field
    woocommerce_wp_text_input(
    array(
    'id' => '_discount_1_4_items',
    'label' => __( 'Discount 1-4 items', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter discount percentage amount for 1 to 4 items.', 'woocommerce' )
    )
    );

    woocommerce_wp_text_input(
    array(
    'id' => '_discount_5_9_items',
    'label' => __( 'Discount 5-9 items', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter discount percentage amount for 5 to 9 items.', 'woocommerce' )
    )
    );

    woocommerce_wp_text_input(
    array(
    'id' => '_discount_10_19_items',
    'label' => __( 'Discount 10-19 items', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter discount percentage amount for 10 to 19 items.', 'woocommerce' )
    )
    );

    woocommerce_wp_text_input(
    array(
    'id' => '_discount_20_items',
    'label' => __( 'Discount 20+ items', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter discount percentage amount for 20+ items.', 'woocommerce' )
    )
    );

    echo '

    ';

    echo '

    ';

    // Text Field
    woocommerce_wp_text_input(
    array(
    'id' => '_discount_1_5_items',
    'label' => __( 'Discount 1-5 items', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter discount percentage amount for 1 to 5 items.', 'woocommerce' )
    )
    );

    woocommerce_wp_text_input(
    array(
    'id' => '_discount_6_10_items',
    'label' => __( 'Discount 6-10 items', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter discount percentage amount for 6 to 10 items.', 'woocommerce' )
    )
    );

    woocommerce_wp_text_input(
    array(
    'id' => '_discount_11_items',
    'label' => __( 'Discount 11+ items', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter discount percentage amount for 11+ items.', 'woocommerce' )
    )
    );

    echo '

    ';

    }

    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

    function woo_add_custom_general_fields_save( $post_id ){

    // Text Field
    $woocommerce_discount_1_4_items = $_POST['_discount_1_4_items'];
    //if( !empty( $woocommerce_discount_1_4_items ) )
    update_post_meta( $post_id, '_discount_1_4_items', esc_attr( $woocommerce_discount_1_4_items ) );

    $woocommerce_discount_5_9_items = $_POST['_discount_5_9_items'];
    //if( !empty( $woocommerce_discount_5_9_items ) )
    update_post_meta( $post_id, '_discount_5_9_items', esc_attr( $woocommerce_discount_5_9_items ) );

    $woocommerce_discount_10_19_items = $_POST['_discount_10_19_items'];
    //if( !empty( $woocommerce_discount_10_19_items ) )
    update_post_meta( $post_id, '_discount_10_19_items', esc_attr( $woocommerce_discount_10_19_items ) );

    $woocommerce_discount_20_items = $_POST['_discount_20_items'];
    //if( !empty( $woocommerce_discount_20_items ) )
    update_post_meta( $post_id, '_discount_20_items', esc_attr( $woocommerce_discount_20_items ) );

    $woocommerce_discount_1_5_items = $_POST['_discount_1_5_items'];
    //if( !empty( $woocommerce_discount_1_5_items ) )
    update_post_meta( $post_id, '_discount_1_5_items', esc_attr( $woocommerce_discount_1_5_items ) );

    $woocommerce_discount_6_10_items = $_POST['_discount_6_10_items'];
    //if( !empty( $woocommerce_discount_6_10_items ) )
    update_post_meta( $post_id, '_discount_6_10_items', esc_attr( $woocommerce_discount_6_10_items ) );

    $woocommerce_discount_11_items = $_POST['_discount_11_items'];
    //if( !empty( $woocommerce_discount_11_items ) )
    update_post_meta( $post_id, '_discount_11_items', esc_attr( $woocommerce_discount_11_items ) );

    }

    add_filter( 'woocommerce_cart_item_subtotal', 'wp_update_woocommerce_cart_item_subtotal', 99, 3 );
    function wp_update_woocommerce_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ){
    $qty = $cart_item['quantity'];

    $discount1 = get_post_meta($cart_item['product_id'], '_discount_1_4_items', true);
    $discount2 = get_post_meta($cart_item['product_id'], '_discount_5_9_items', true);
    $discount3 = get_post_meta($cart_item['product_id'], '_discount_10_19_items', true);
    $discount4 = get_post_meta($cart_item['product_id'], '_discount_20_items', true);

    $discount1_1 = get_post_meta($cart_item['product_id'], '_discount_1_5_items', true);
    $discount2_2 = get_post_meta($cart_item['product_id'], '_discount_6_10_items', true);
    $discount3_3 = get_post_meta($cart_item['product_id'], '_discount_11_items', true);

    $OriginalPrice = $cart_item['data']->get_price();

    if($discount1){
    if($qty >= 1 && $qty <=4){ $FinalPrice = $OriginalPrice*$discount1; }elseif($qty >= 5 && $qty <=9){ $FinalPrice = $OriginalPrice*$discount2; }elseif($qty >= 10 && $qty <=19){ $FinalPrice = $OriginalPrice*$discount3; }elseif($qty >= 20 ){
    $FinalPrice = $OriginalPrice*$discount4;
    }
    }
    if($discount1_1){
    if($qty >= 1 && $qty <=5){ $FinalPrice = $OriginalPrice*$discount1_1; }elseif($qty >= 6 && $qty <=10){ $FinalPrice = $OriginalPrice*$discount2_2; }elseif($qty >= 11 ){
    $FinalPrice = $OriginalPrice*$discount3_3;
    }
    }

    $subtotal = wc_price( $FinalPrice * $cart_item['quantity']);

    //return sprintf( '%s %s', $subtotal, $newsubtotal );
    //$cart_item['data']->set_price( $FinalPrice * $cart_item['quantity'] );

    return $subtotal;

    }

    add_action( 'woocommerce_add_cart_item_data', 'save_custom_data_hidden_fields', 10, 2 );
    function save_custom_data_hidden_fields( $cart_item_data, $product_id ) {

    $data = array();

    $cart_item_data['custom_data']['discount_1'] = get_post_meta($product_id, '_discount_1_4_items', true);
    $data['discount_1'] = get_post_meta($product_id, '_discount_1_4_items', true);

    $cart_item_data['custom_data']['discount_2'] = get_post_meta($product_id, '_discount_1_4_items', true);
    $data['discount_2'] = get_post_meta($product_id, '_discount_5_9_items', true);

    $cart_item_data['custom_data']['discount_3'] = get_post_meta($product_id, '_discount_1_4_items', true);
    $data['discount_3'] = get_post_meta($product_id, '_discount_10_19_items', true);

    $cart_item_data['custom_data']['discount_4'] = get_post_meta($product_id, '_discount_1_4_items', true);
    $data['discount_4'] = get_post_meta($product_id, '_discount_20_items', true);

    $cart_item_data['custom_data']['discount_1_1'] = get_post_meta($product_id, '_discount_1_5_items', true);
    $data['discount_1_1'] = get_post_meta($product_id, '_discount_1_5_items', true);

    $cart_item_data['custom_data']['discount_2_2'] = get_post_meta($product_id, '_discount_6_10_items', true);
    $data['discount_2_2'] = get_post_meta($product_id, '_discount_6_10_items', true);

    $cart_item_data['custom_data']['discount_3_3'] = get_post_meta($product_id, '_discount_11_items', true);
    $data['discount_3_3'] = get_post_meta($product_id, '_discount_11_items', true);

    // below statement make sure every add to cart action as unique line item
    $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
    WC()->session->set( 'price_calculation', $data );

    return $cart_item_data;
    }

    // Change the cart prices if a unit_price is set
    add_filter( 'woocommerce_before_calculate_totals', 'sv_change_product_price_cart', 10, 3 );
    function sv_change_product_price_cart( $cart ) {

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
    // The instance of the WC_Product Object
    $product = $cart_item['data'];
    $product_id = $product->get_id(); // The product ID

    $price = $product->get_price(); // The product price
    $qty = $cart_item['quantity'];

    $discount1 = get_post_meta($cart_item['product_id'], '_discount_1_4_items', true);
    $discount2 = get_post_meta($cart_item['product_id'], '_discount_5_9_items', true);
    $discount3 = get_post_meta($cart_item['product_id'], '_discount_10_19_items', true);
    $discount4 = get_post_meta($cart_item['product_id'], '_discount_20_items', true);

    $discount1_1 = get_post_meta($cart_item['product_id'], '_discount_1_5_items', true);
    $discount2_2 = get_post_meta($cart_item['product_id'], '_discount_6_10_items', true);
    $discount3_3 = get_post_meta($cart_item['product_id'], '_discount_11_items', true);

    if($discount1){
    if($qty >= 1 && $qty <=4){ $discount = $discount1; } if($qty >= 5 && $qty <=9){ $discount = $discount2; } if($qty >= 10 && $qty <=19){ $discount = $discount3; } if($qty >= 20 ){
    $discount = $discount4;
    }
    }
    if($discount1_1){
    if($qty >= 1 && $qty <=5){ $discount = $discount1_1; } if($qty >= 6 && $qty <=10){ $discount = $discount2_2; } if($qty >= 11 ){
    $discount = $discount3_3;
    }
    }

    // Set the new cart item price
    $product->set_price( $price*$discount );
    }

    }

    In my cart.php template file within the foreach loop:

    $qty = $cart_item['quantity'];

    $discount1 = get_post_meta($cart_item['product_id'], '_discount_1_4_items', true);
    $discount2 = get_post_meta($cart_item['product_id'], '_discount_5_9_items', true);
    $discount3 = get_post_meta($cart_item['product_id'], '_discount_10_19_items', true);
    $discount4 = get_post_meta($cart_item['product_id'], '_discount_20_items', true);

    $discount1_1 = get_post_meta($cart_item['product_id'], '_discount_1_5_items', true);
    $discount2_2 = get_post_meta($cart_item['product_id'], '_discount_6_10_items', true);
    $discount3_3 = get_post_meta($cart_item['product_id'], '_discount_11_items', true);

    $OriginalPrice = $cart_item['data']->get_price();

    if($discount1){
    if($qty >= 1 && $qty <=4){ $discount = $discount1; } if($qty >= 5 && $qty <=9){ $discount = $discount2; } if($qty >= 10 && $qty <=19){ $discount = $discount3; } if($qty >= 20 ){
    $discount = $discount4;
    }
    }
    if($discount1_1){
    if($qty >= 1 && $qty <=5){ $discount = $discount1_1; } if($qty >= 6 && $qty <=10){ $discount = $discount2_2; } if($qty >= 11 ){
    $discount = $discount3_3;
    }
    }

    $cart_item['data']->set_price( $OriginalPrice*$discount );

    I’ve also had to replace the Price call (cart.php):
    echo (apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key )*); // PHPCS: XSS ok.
    With:
    echo '$'.number_format((float)$OriginalPrice, 2, '.', '').'';

    #7199
    Naidi
    Participant

    Hi,

    you can create quantity discounts in another way too.
    Firstly remove the default quantity input of Woocommerce, and create a quantity input field with WooPriceCalculator.
    Now you can create different rules and logic inside the excel file as you do for the other calculations too.

    Best regards.
    Naidi
    AT84

    #7198
    Tyler Robinson
    Participant

    I didn’t use an excel file for the calculations as my clients excel file was way to complicated. I manually set the calculations with WooPriceCalculator. If I could do it with an excel file, then that would be great, but I’m not sure it could be done.

    #7197
    Tyler Robinson
    Participant

    Here is one of the simpler product excel price sheets that my client had. I couldn’t figure out how to make this work in the WooPriceCalculator. So, I made a calculator manually. [attachment=321]LENOXPalletMasterB.xls[/attachment]

    #7196
    Naidi
    Participant

    Hi,

    creating a calculator form an excel file is a very easy step.
    Here I’m sending you the links to our documentation on how you can achieve that:

    https://altoswebsolutions.com/manual/woo-price-calculator/start

    https://altoswebsolutions.com/live-demo/best-cases

    Best regards.
    Naidi
    AT849

    #7195
    Tyler Robinson
    Participant

    I have found a way to create a calculator in excel and import that. I works great! However, there are two minor issues. For some reason, the product defaults to the incorrect price. It defaults to the price starting at 9 feet. The other is that when I import the excel file, WooPrice Calculator rounds the price down 1 cent from what the excel file shows the price is. I have attached my excel file for viewing and testing if anyone can help me out with one or both of the issues.

    Thanks! [attachment=331]TEST-BAHCOSandflexM42.xlsx[/attachment]

    #7194
    Naidi
    Participant

    Hi Tyler,

    About the wrong default price:
    You should check the default values you have set to the input fields you have created.
    If you have set no default values to the input fields, than you should clear the cells
    on the excel file before upload it.

    About the 1 cent round problem:
    Instead of using TEXT() formula use ROUND()
    here some documentation how to use it :https://exceljet.net/excel-functions/excel-round-function

    do not forget to reformat the cell B19 to be a general one.

    Attached will find the excel file of your case that use the ROUND formula.

    Best regards.
    Naidi
    AT849 [attachment=332]TEST-BAHCOSandflexM42.xlsx[/attachment]

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