Jump to content

[Contribution] Free shipping if total is over x, excluding specific categories


Recommended Posts

Use this code to exclude specific categories from counting to the total necessary for free shipping. Part of this code can be also used to add to your cart a line with something like "Remaining amount in order to obtain free shipping:"

 

Most of the code was taken from here, but also here. Works on prestashop 1.5.6.2.

 

First, define the value for "Free shipping starts at" in Shipping/Shipping of the Backoffice. This would include every category and product. If you sell, for example, virtual products and physical products you might want to exclude the virtual products from counting to the total necessary for free shipping. Let's assume you want to exclude category A, B and C from the free shipping calculation.

 

Backup and edit classes/cart.php:

 

- in line 2681, bellow "// Free fees" and before "$free_fees_price = 0;" add:

//Use this to select the categories.
        $categoryA[0]['id_category'] = A;
	$categoryB[0]['id_category'] = B;
	$categoryC[0]['id_category'] = C;
        $no_free_shipping = 0;
		
	foreach ($products AS $product)
	{
	   //Check if added product is in selected categories and totalize.
           if (Product::idIsOnCategoryId((int) ($product['id_product']), $categoryA)) {
               $no_free_shipping += $product['price_wt']*$product['cart_quantity'];
           }
            if (Product::idIsOnCategoryId((int) ($product['id_product']), $categoryB)) {
                $no_free_shipping += $product['price_wt']*$product['cart_quantity'];
            }
            if (Product::idIsOnCategoryId((int) ($product['id_product']), $categoryC)) {
                $no_free_shipping += $product['price_wt']*$product['cart_quantity'];
            }
	}

This is for 3 categories. Substitute A, B and C for the id_category numbers. Delete unnecessary code or copy if needed.

 

 

- Substitute line 2685 (now 2704) 
if ($orderTotalwithDiscounts >= (float)($free_fees_price) && (float)($free_fees_price) > 0)
for
(($orderTotalwithDiscounts - $no_free_shipping) >= (float)($free_fees_price) && (float)($free_fees_price) > 0)
 
- In line 2842 (now 2861), after "public function checkDiscountValidity...{...}" and before "public function getSummaryDetails..." add:
public function no_free_shipping($no_free_shipping)
{	
	$products = $this->getProducts();
   //Use this to select the categories.
        $categoryA[0]['id_category'] = A;
	$categoryB[0]['id_category'] = B;
	$categoryC[0]['id_category'] = C;
        $no_free_shipping = 0;
		
	foreach ($products AS $product)
	{
	    //Check if added product is in selected categories and totalize.
            if (Product::idIsOnCategoryId((int) ($product['id_product']), $categoryA)) {
               $no_free_shipping += $product['price_wt']*$product['cart_quantity'];
          }
            if (Product::idIsOnCategoryId((int) ($product['id_product']), $categoryB)) {
               $no_free_shipping += $product['price_wt']*$product['cart_quantity'];
          }
            if (Product::idIsOnCategoryId((int) ($product['id_product']), $categoryC)) {
               $no_free_shipping += $product['price_wt']*$product['cart_quantity'];
          }
      }				
	return $no_free_shipping;			
}

Again, adapt category numbers.

 

 

- In line 2945 (now 2989) after "return array(" add:

'amount_until_free_shipping' => ((Tools::convertPrice((float)(Configuration::get('PS_SHIPPING_FREE_PRICE')))) - ($total_products_wt - $total_discounts - $this->no_free_shipping(isset($no_free_shipping)))),

Now, backup and edit themes/<your theme>/shopping-cart.tpl:

 

- After line 146, between the two "{/if}" add:

<tr class="shipping_free_price">
<td colspan="5"><span class="shipping_free_price">{l s='Remaining amount in order to obtain free shipping*:'}</span><br /> <span class="free_ship_except">{l s='*except ABC' mod="blockcart"}</span></td>
<td colspan="2" class="price" id="amount_until_free_shipping" >{displayPrice price=$amount_until_free_shipping}</td></tr>

Adapt your text (categories!).

 

 

Finally, backup and edit themes/<your theme>/js/cart-summary.js:

 

- Substitute this code (I believe it's obsolete):

	if (json.free_ship > 0 && !json.is_virtual_cart)
	{
		$('.cart_free_shipping').fadeIn();
		$('#free_shipping').html(formatCurrency(json.free_ship, currencyFormat, currencySign, currencyBlank));
	}
	else
		$('.cart_free_shipping').hide();

for this one:

	if (json.amount_until_free_shipping > 0 && !json.is_virtual_cart)
	{
		$('.shipping_free_price').fadeIn();
		$('#amount_until_free_shipping').html(formatCurrency(json.amount_until_free_shipping, currencyFormat, currencySign, currencyBlank));
	}
	else
		$('.shipping_free_price').hide();

And that's it!

 

Let me know if it's working fine.

 
Cheers!

Alex

 

PS: There are one limitation with this code: it does not calculate properly if you have discounts (vouchers/cart rules) for the selected categories.

Edited by ilovekutchi.com (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites
  • 2 months later...

I have little problem.

 

In shopping-cart.tpl i have this error

Notice: Undefined index: shipping_free_price in /var/www/vhosts/pikante.cz/httpdocs/cache/smarty/compile/ce/12/01/ce1201ff99c0a695bf1609d41b181aba0097c8aa.file.shopping-cart.tpl.php on line 597 Notice: Trying to get property of non-object in /var/www/vhosts/pikante.cz/httpdocs/cache/smarty/compile/ce/12/01/ce1201ff99c0a695bf1609d41b181aba0097c8aa.file.shopping-cart.tpl.php on line 597

In shopping-cart.tpl i have this code

<p id="amount_fee_shipping">
  <span class = "ajax_shipping_free_price_span {if ($amount_until_free_shipping) <= 0} hidden{/if}">{l s="Do dopravy zdarma zbývá:" mod="blockcart"}<span class="ajax_shipping_free_price">{$amount_until_free_shipping}</span><span class="kc">kč</span></span>


  <span class = "ajax_shipping_free_price_free_spans {if ($amount_until_free_shipping) > 0} hidden {/if}"> <span class="ajax_shipping_free_price_free">{$shipping_free_price}</span> {l s="DOPRAVA ZADARMO!" mod="blockcart"}
 </p>

 

Any idea?

Edited by tozi (see edit history)

Share this post


Link to post
Share on other sites
  • 7 months later...

Thx kutchi for your post. I was looking for something like this for a few days, without excluding categories but still it helped me a lot.

I just needed to make one change in shopping-cart.tpl:

 

<tr class="shipping_free_price" {if $amount_until_free_shipping <= 0} style="display: none;" {/if}>

 

Without it when order gets free shipping and then I get back from 5th step to the 1st step of my order the <tr> was visible again, with negative amount. I'm using presta 1.5.6.0.

Share this post


Link to post
Share on other sites
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More