Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter GTech Web Store

    (@gtechwebstore)

    Hi, Dima:

    Thanks for the response, the code below are used for recalculating the total based on quantity discount, will try disable that piece later

    function gtechwebstore_before_calculate_totals($cart) {
    //get max vip discount
    $user = wp_get_current_user();
    //get _force_reassign_role_time meta, reassign role if required
    $date = get_user_meta($user->ID, ‘_force_reassign_role_time’, true );
    if ($date === false || time() > strtotime($date)) {
    gtechwebstore_assign_role_based_on_completed_order($user);
    }

    $vip_discount = 1;
    $roles = (array) $user->roles;
    if ( in_array( ‘vip2’, $roles ) ) $vip_discount = 0.98;
    if ( in_array( ‘vip4’, $roles ) ) $vip_discount = 0.96;
    if ( in_array( ‘vip6’, $roles ) ) $vip_discount = 0.94;
    if ( in_array( ‘vip8’, $roles ) ) $vip_discount = 0.92;
    if ( in_array( ‘vip10’, $roles ) ) $vip_discount = 0.9;

    //registered user, first order ever? 10% off then
    if ($user->exists() && count(wc_get_orders( array(
    ‘meta_key’ => ‘_customer_user’,
    ‘meta_value’ => $user->ID,
    ‘status’ => array(‘wc-pending’, ‘wc-processing’, ‘wc-on-hold’, ‘wc-completed’),
    ‘limit’ => 1,
    ‘return’ => ‘ids’,
    ) )) == 0) {
    $vip_discount = 0.9;
    }

    $product_quantities = array();

    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
    //gift price = 0
    if (array_key_exists(“is_gift”, $cart_item) && $cart_item[“is_gift”])
    $cart_item[“data”]->set_price(0);
    else {
    //get total quantity for same product
    $product_id = $cart_item[“product_id”];
    $quantity = $cart_item[“quantity”];
    if (array_key_exists($product_id, $product_quantities))
    $product_quantities[$product_id] += $quantity;
    else $product_quantities[$product_id] = $quantity;
    }
    }
    //discount
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
    $product = $cart_item[“data”];
    if (array_key_exists(“is_gift”, $cart_item) && $cart_item[“is_gift”]) {
    ;
    }
    else if (array_key_exists(“price”, $product->get_changes())) {
    ;
    } else {
    $quantity = $product_quantities[$cart_item[“product_id”]];
    $quantity_discount = 1;
    if ($quantity > 1) { $quantity_discount = 0.98; }
    if ($quantity > 2) { $quantity_discount = 0.96; }
    if ($quantity > 3) { $quantity_discount = 0.94; }
    if ($quantity > 4) { $quantity_discount = 0.92; }
    if ($quantity > 5) { $quantity_discount = 0.9; }
    $product->set_price($product->get_price() * min($vip_discount, $quantity_discount));

    }
    }

    }
    add_action( ‘woocommerce_before_calculate_totals’, ‘gtechwebstore_before_calculate_totals’, 20, 1 );

    Hi, Nick

    any update on this, same issue here.

    Thanks

    Gary

    Thread Starter GTech Web Store

    (@gtechwebstore)

    Great, this does the trick. Thanks for the help Frank!

Viewing 3 replies - 1 through 3 (of 3 total)