Nisi t
Nisi t

Reputation: 13

Apply a discounted price based on Woocommerce cart weight

I am looking to apply product price based on total cart weight. Each product has an additional custom_price field which should be applied if cart weight exceeds 100kgs.

Example:

Product A - Weight 10kg, Regular price 100, Custom price field 80

Product B - Weight 20kg, Regular price 200, Custom price field 180

Scenario 1

Products A and B are added in the cart with certain unit quantity and the total cart weight is less than 100kgs

In this case, regular price should be applied to both products. ie For Product A - 100 and For Product B - 200

Scenario 2

Products A and B are added in the cart with certain unit quantity and the total cart weight exceeds 100kgs

In this case, Custom price field should be applied to both the products ie For Product A - 80 and For Product B - 180.

Further displaying the total difference amount (ie total of both products with regular price - total of both products with custom price) as a discount.

How can I achieve this? I will appreciate any help on this.

I did check a similar question in this link but it is based on the discount percentage and I am looking to call a custom price field.

Upvotes: 0

Views: 99

Answers (1)

businessbloomer
businessbloomer

Reputation: 1132

You can override the Cart/Checkout price of a WooCommerce product with the set_price() WooCommerce function. Here I have an example where you alter the product price in case customers are logged in, which is very similar to your case.

I'll try to come up with a quick fix for you based on that code:

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_alter_price_cart_if_weight', 9999 );
 
function bbloomer_alter_price_cart_if_weight( $cart ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
 
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
 
    // IF BELOW CART WEIGHT THRESHOLD, EXIT
    if ( WC()->cart->get_cart_contents_weight() < 100 ) return;
 
    // ELSE LOOP THROUGH CART ITEMS & APPLY CUSTOM PRICE
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $special_price = get_post_meta( $cart_item['product_id'], '_whatever_key', true );
        $cart_item['data']->set_price( $special_price );
    }
 
}

In order to show the total discount/savings when the cart reaches the weight threshold, you can reuse some of this code: Show How Much Customer Saved @ WooCommerce Cart and Checkout Pages

Of course, you need to change the foreach part, and it would be something like this:

add_action( 'woocommerce_cart_totals_after_order_total', 'bbloomer_show_total_weight_discount_cart_checkout', 9999 );
add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_show_total_weight_discount_cart_checkout', 9999 );
  
function bbloomer_show_total_weight_discount_cart_checkout() {  
   if ( WC()->cart->get_cart_contents_weight() < 100 ) return; 
   $discount_total = 0;  
   foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {         
      $product = $values['data'];
      $discount = ( (float)$product->get_price() - (float)get_post_meta( $cart_item['product_id'], '_whatever_key', true ) ) * (int)$values['quantity'];
         $discount_total += $discount;
      }
   }          
   if ( $discount_total > 0 ) {
      echo '<tr><th>You Saved</th><td data-title="You Saved">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
   }
}

Upvotes: 0

Related Questions