Virtuemart Minimum Amount for Free Shipment with Coupon

Virtuemart is a great shopping cart system for Joomla CMS. No system is perfect, and Virtuemart is no exception. In this post, I will describe how to change the behavior of the Minimum amount for free shipment when a coupon code has been entered.

The Problem

You go to admin->Virtuemart->Shop->Shipping methods and setup in Shipping method using the method "VM Shipment - By weight, ZIP and countries". In the process of setting up this method, you set the " Minimum Amount for Free Shipment". Lets for example say you set it to 50, otherwise the shipping cost is 2.49.

Then in the front end, the customer adds an item to the cart that costs over 50, but at the same time enters a coupon code which brings the product total under 50. Virtuemart still gives free shipping.

This happens regarless if if you are using AwoCoupon or the core Virtuemart coupons. The problem is the total used by Virtuemart to determine if the customer receives free shipping or not does not include the coupon discount.



The solution

In order to fix this problem, alterations have to be made to the shipping method code. To select the file to alter, you would have to first determine what shipping method you are using to give free shipping. If using "VM Shipment - By weight, ZIP and countries", then you would want to alter the file:

www/plugins/vmpayment/weight_countries/weight_countries.php

In this case, the shipping method is weight_countries/weight_countries.php

Around line 200, you should see the line:
<?php
if ($method->free_shipment && $cart_prices['salesPrice'] >= $method->free_shipment) {
?>

If you are using Virtuemart 2.0.24 or above, then you would change the above to:
<?php
//if ($method->free_shipment && $cart_prices['salesPrice'] >= $method->free_shipment) {
$amount $this->getCartAmount($cart_prices);
if (
$method->free_shipment && $amount >= $method->free_shipment) {
?>

If you are using Virtuemart 2.0.22 or less, then you would change the above to:
<?php
//if ($method->free_shipment && $cart_prices['salesPrice'] >= $method->free_shipment) {
if(empty($cart_prices['salesPrice'])) $cart_prices['salesPrice'] = 0.0;
$cartPrice = !empty($cart_prices['withTax'])? $cart_prices['withTax']:$cart_prices['salesPrice'];
if(empty(
$cart_prices['salesPriceShipment'])) $cart_prices['salesPriceShipment'] = 0.0;
if(empty(
$cart_prices['salesPriceCoupon'])) $cart_prices['salesPriceCoupon'] = 0.0;
$amount$cartPrice $cart_prices['salesPriceShipment'] + $cart_prices['salesPriceCoupon'] ;
if (
$amount <= 0$amount=0;
if (
$method->free_shipment && $amount >= $method->free_shipment) {
?>

After making that change and refreshing the cart, shipping should now be charged if the cart total, including coupon discounts is less than 50, and not charged if greater than 50.

Comments (6)

  1. Your avatar
    seyi 4 years ago 4 years ago
    There is also an issue with minimum/maxmimum order amount not taking into account the coupon code. If using AwoCoupon, you can use this solution:
    Assuming you are using standard shipping, in the file www/plugins/vmshipment/weight_countries/weight_countries.php around line 277 is this:
    <?php
                $orderamount_cond 
    $this->testRange($cart_prices['salesPrice'],$method,'orderamount_start','orderamount_stop','order amount');
    ?>

    Comment it out and add more code like this:
    <?php
                
    //$orderamount_cond = $this->testRange($cart_prices['salesPrice'],$method,'orderamount_start','orderamount_stop','order amount');

                
    $amount $cart_prices['salesPrice'];

                if ( 
    function_exists'AC' ) && ! empty($cart_prices['salesPriceCoupon'] ) ) {

                    
    $awosess AC()->storediscount->get_coupon_session();

                    if ( ! empty( 
    $awosess ) ) {

                        
    $amount -= ( $awosess->product_discount $awosess->shipping_discount );

                    }

                }

                
    $orderamount_cond $this->testRange($amount,$method,'orderamount_start','orderamount_stop','order amount');
    ?>

    And that should take into account AwoCoupon discount.
    • Default avatar
      david19 4 months ago 4 months ago
      Finally, found my soultion. After applying the coupon code - VM was showing wrong range shipping methods. But after applying your code it still did not work correct. Changing the empty to !empty in your code made the solution. I guess you made a small mistake with empty !empty. Here is the right row of code:
      if ( function_exists( 'AC' ) && !empty($cart_prices['salesPriceCoupon'] ) ) {
    • Default avatar
      david19 4 months ago 4 months ago
      It calculates now correctly after changing empty to !empty, but now it does not allow to confirm the order with error "COM_VIRTUEMART_CART_NO_SHIPPINGRATE": "No shipping rate could be selected, you may not have entered your address or the vendor/shipment does not support your location"
      • Your avatar
        seyi 4 months ago 4 months ago
        Hello,

        In awocoupon > installation check, make sure everything is installed. If still having issues, please post on the forum the exact issue, this blog is really old.
  2. Default avatar
    jan448 4 years ago 4 years ago
    This guide is totally wrong.
    First of all, the file to be changed IS NOT www/plugins/vmpayment/weight_countries/weight_countries.php
    but is
    www/plugins/VMSHIPMENT/weight_countries/weight_countries.php

    Then, the code is totally wrong.
    The site will be broken with this error: syntax error, unexpected '<'

    Please write the REAL SOLUTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    • Your avatar
      seyi 4 years ago 4 years ago
      Thanks for spotting the error in file location, vmpayment has been changed to vmshipment.

      As for the solution, it is very much real. I have used it and I know users that have used it also. Likely your error happens because you enter the "</?php" and "?>". Those are there to indicate it is php code but should not be entered when updating your code.

      If you are still having problems, you can forward the file to me that you edited, contact email on the contact page and happy to fix any issues.

      Regards
      Seyi