Jump to content
Global Attribute AB

[Solved] Amount left to free shipping

Recommended Posts

Hi!

I'm searching for a module that shows the amount left to free shipping to the customer in the cartblock.

Would be very easy to code right? Yet I haven't found any module with this function.


/Robin

Share this post


Link to post
Share on other sites

Add the following code to the smartyAssigns function in modules/blockcart/blockcart.php:

$summary = $params['cart']->getSummaryDetails();

if ($free_ship = Tools::convertPrice(floatval(Configuration::get('PS_SHIPPING_FREE_PRICE')), new Currency(intval($params['cart']->id_currency))))
{
   $discounts = $params['cart']->getDiscounts();
   $total_free_ship =  $free_ship - ($summary['total_products_wt'] + $summary['total_discounts']);
   foreach ($discounts as $discount)
       if ($discount['id_discount_type'] == 3)
       {
           $total_free_ship = 0;
           break ;
       }
   $smarty->assign('free_ship', $total_free_ship);
}



Then add the following to modules/blockcart/blockcart.tpl:

{if $free_ship > 0 AND !$isVirtualCart}


{l s='Remaining amount to be added to your cart in order to obtain free shipping:' mod='blockcart'}
{displayPrice price=$free_ship}

{/if}

Share this post


Link to post
Share on other sites

(PS v1.3.5.0) This does not work for me, when I add the extra code to the smartyAssigns function, my entire right block column disappears. I have the 'Tax on next line' modification added to my blockcart, but I don't believe this should affect it, right? Here is the whole function before adding:

public function smartyAssigns(&$smarty, &$params)
   {

       global $errors, $cookie;

       // Set currency
       if (!intval($params['cart']->id_currency))
           $currency = new Currency(intval($params['cookie']->id_currency));
       else
           $currency = new Currency(intval($params['cart']->id_currency));
       if (!Validate::isLoadedObject($currency))
           $currency = new Currency(intval(Configuration::get('PS_CURRENCY_DEFAULT')));

       if ($params['cart']->id_customer)
       {
           $customer = new Customer(intval($params['cart']->id_customer));
           $taxCalculationMethod = Group::getPriceDisplayMethod(intval($customer->id_default_group));
       }
       else
           $taxCalculationMethod = Group::getDefaultPriceDisplayMethod();
       $usetax = $taxCalculationMethod == PS_TAX_EXC ? false : true;

       $products = $params['cart']->getProducts(true);
       $nbTotalProducts = 0;
       foreach ($products AS $product)
           $nbTotalProducts += intval($product['cart_quantity']);

       $wrappingCost = floatval($params['cart']->getOrderTotal($usetax, 6));

       $smarty->assign(array(
           'products' => $products,
           'customizedDatas' => Product::getAllCustomizedDatas(intval($params['cart']->id)),
           'CUSTOMIZE_FILE' => _CUSTOMIZE_FILE_,
           'CUSTOMIZE_TEXTFIELD' => _CUSTOMIZE_TEXTFIELD_,
           'discounts' => $params['cart']->getDiscounts(false, $usetax),
           'nb_total_products' => intval($nbTotalProducts),
           'shipping_cost' => Tools::displayPrice($params['cart']->getOrderTotal($usetax, 5), $currency),
           'show_wrapping' => $wrappingCost > 0 ? true : false,
           'wrapping_cost' => Tools::displayPrice($wrappingCost, $currency),
           'product_total' => Tools::displayPrice($params['cart']->getOrderTotal($usetax, 4), $currency),
           'total' => Tools::displayPrice($params['cart']->getOrderTotal(true), $currency),
           'tax' => Tools::displayPrice($params['cart']->getOrderTotal()-$params['cart']->getOrderTotal(false), $currency),
           'id_carrier' => intval($params['cart']->id_carrier),
           'ajax_allowed' => intval(Configuration::get('PS_BLOCK_CART_AJAX')) == 1 ? true : false
       ));
       if (sizeof($errors))
           $smarty->assign('errors', $errors);
       if(isset($cookie->ajax_blockcart_display))
           $smarty->assign('colapseExpandStatus', $cookie->ajax_blockcart_display);

   }

  • Like 1

Share this post


Link to post
Share on other sites

Edit config/config.inc.php and temporarily change 'display_errors' from 'off' to 'on'. After you've pasted the code, you should then get an error message instead of a blank right column, which will help to figure out what's going wrong.

Share this post


Link to post
Share on other sites

Fatal error: Call to a member function getSummaryDetails() on a non-object in /homepages/3/d344777950/htdocs/shop/modules/blockcart/blockcart.php on line 73

Line 73: $summary = $cart->getSummaryDetails();

Share this post


Link to post
Share on other sites

Thanks, that error message helps. That line should be:

$summary = $params['cart']->getSummaryDetails();

Share this post


Link to post
Share on other sites

AAAAwwwwwesooooooome!! Thanks, rocky! I'm totally psyched to add this to the store, you rule :) oh, and the $params['cart'] also needed to be added to the $discounts = $cart->getDiscounts(); as well. Cheers!

Mark this puppy [sOLVED]! :D

Share this post


Link to post
Share on other sites

I don't suppose I could bother you for one more alteration, rocky? I would like to display different messages depending on the current amount left to get free shipping. How might I go about doing that?

i.e. $50-100, 'Add {displayPrice price=$free_ship} more, get free shipping!'
$101-150, 'Almost there, add {displayPrice price=$free_ship} more, get free shipping!'
$151-199, 'Nearly there, it'd be crazy not to add {displayPrice price=$free_ship} more to get free shipping!'

Share this post


Link to post
Share on other sites

Hi Rocky,

Great solution. I have done as you instructed and everything works except the AJAX auto update feature of the cart. I need to refresh the page in order to see an updated value for "Amount Left for Free Shipping".

Do you know the necessary modifications needed to make the cart auto update with the amount?

I also noticed a solution you implemented to auto update the block cart with the products' weight in this post:
http://www.prestashop.com/forums/viewthread/74831/integration/solved_display_total_weight_in_blockcart

I assume the solution would be similar but using different variables..? Thanks Rocky and looking forward to implementing this on my site.

  • Like 1

Share this post


Link to post
Share on other sites

@MrMcQ

You can use if statements to do that. For example:

{if $free_ship > 0}{if $free_ship < 50}{l s='Nearly, there, it\'d be crazy not to add' mod='blockcart'} {displayPrice price=$free_ship} {l s='more to get free shipping!' mod='blockcart'}{elseif $free_ship < 100}{l s='Almost there, add' mod='blockcart'} {displayPrice price=$free_ship} {l s='more, get free shipping!' mod='blockcart'}{else}{l s='Add' mod='blockcart'} {displayPrice price=$free_ship} {l s='more, get free shipping!' mod='blockcart'}{/if}{/if}

Share this post


Link to post
Share on other sites

@plauengco

You will need to change line 68 of modules/blockcart/blockcart-json.tpl from:

'shippingCost': '{$shipping_cost|html_entity_decode:2:'UTF-8'}',



to:

'shippingCost': '{$shipping_cost|html_entity_decode:2:'UTF-8'}',
'freeShip': '{$free_ship|html_entity_decode:2:'UTF-8'}',



Then change line 507 of modules/blockcart/ajax-cart.js from:

$('.ajax_cart_shipping_cost').text(jsonData.shippingCost);



to:

$('.ajax_cart_shipping_cost').text(jsonData.shippingCost);
$('.ajax_cart_free_shipping').text(jsonData.freeShipping);



That should use AJAX to put the $free_ship value into the .ajax_cart_free_shipping span in modules/blockcart/blockcart.tpl.

Share this post


Link to post
Share on other sites

Awesomeness, thanks a bunch! I really didn't know which variable to use for the current total...and for future reference, what kind of code is Prestashop using for development?

Muchas gracias senor rocky!

Share this post


Link to post
Share on other sites

Thanks for the guidance, rocky, unfortunately as plauengco mentioned the auto-update feature, your code does not seem to update the cart in this manner. I still have to refresh the page to see the remaining shipping balance, though I followed your changes to the letter :|

@plauengco, did you get this to work...perhaps it something I'm not doing right.

Share this post


Link to post
Share on other sites

I installed this module, but when you reach the limit of free shipping then it starts to show how much you are over the free shipping limit by putting a (minus), has someone solved it?

Share this post


Link to post
Share on other sites

Is there any update as to perhaps why my auto-refresh on the AJAX blockcart won't reset when an item is added? Otherwise, the page has to be reloaded :\

@nobynator - I'm not entirely sure, the fix that rocky posted above worked efficiently. Have you made any other modifications, or are you using a custom blockcart module?

Share this post


Link to post
Share on other sites

@rocky - Thank you very much for your reply. However, I still cannot get my AJAX blockcart to autoupdate after your proposed modification to blockcart-json.tpl and ajax-cart.js.

I also tried changing the following lines from:

           $('.ajax_cart_shipping_cost').text(jsonData.shippingCost);
       $('.ajax_cart_free_shipping').text(jsonData.freeShipping); 



to:

           $('.ajax_cart_shipping_cost').text(jsonData.shippingCost);
       $('.ajax_cart_free_shipping').text(jsonData.freeShip); 



but still no luck. Any thoughts, rocky?

Share this post


Link to post
Share on other sites

@rocky - this is still not functioning properly, is there any updates yet to the AJAX auto-refresh?

@plauengco - did you get yours to work?

Share this post


Link to post
Share on other sites

Sorry, I'm too busy working on updating my modules to work with PrestaShop v1.4 to help at the moment.

Share this post


Link to post
Share on other sites

Yeah, i'm working on this Ajax things too, but no luck =( But well, thanks you Rocky for everything =) i'm looking forward to your carrousel update =)

Share this post


Link to post
Share on other sites

Here, here! The work is well appreciated, rocky! Even without the AJAX update..its still a fine mod :)

Share this post


Link to post
Share on other sites

Well, i did find why it wouldn't update.

change modules/blockcart/ajax-cart.js

$('.ajax_cart_shipping_cost').text(jsonData.shippingCost);
$('.ajax_cart_free_shipping').text(jsonData.freeShipping); 



to

$('.ajax_cart_shipping_cost').text(jsonData.shippingCost);
$('.ajax_cart_free_shipping').text(jsonData.freeShip); 



and change modules/blockcart/blockcart.tpl:

{if $free_ship > 0 AND !$isVirtualCart}


{l s='Remaining amount to be added to your cart in order to obtain free shipping:' mod='blockcart'}
{displayPrice price=$free_ship}

{/if} 



to

{if $free_ship > 0 AND !$isVirtualCart}


{l s='Remaining amount to be added to your cart in order to obtain free shipping:' mod='blockcart'}
{displayPrice price=$free_ship}

{/if} 



but now, it shows a negative amount when i add more stuff in my cart (when i have already passed the freeshiping amount), because when i reach the freeshipping amount, it don't desapear xD (it desepear when i use F5).

Also, the € sign appear for 1 second when i refresh and desapear instantly, even if i'm below the freeshipping minimum. (and the "," of the price change to ".")

Also, when my cart is already above the minimum for freeshipping (and after i use F5 to not show the negative amount), i delete items in order to go below the minimum amount for freeshipping, the new amount for freeshipping won't appear.

Share this post


Link to post
Share on other sites

Can you post up the full modifications on this thread ... after everybody had a little bit of tinkering with the codes ?

Share this post


Link to post
Share on other sites

Look for

"tr.cart_free_shipping" in your global.css file and add the "display:none;" code

Share this post


Link to post
Share on other sites

Any one did really suggest how to make ajax fully work? after all the modifications done in this thread the realtime free shipping price update still doesn't work...

 

Guys I might have found a problem - I've tried doing everything like Rocky said, but instead of editing his code by other's suggested tinkerings, I made this:

 

in modules/blockcart.tpl I changed rocky's suggested code to:

 

{if $free_ship > 0 AND !$isVirtualCart}
   <span>{l s='Until free shipping' mod='blockcart'}</span>
   <span id="cart_block_free_shipping" class="price ajax_cart_free_shipping">{displayPrice price=$free_ship}</span>
{/if}

 

so after adding the <span> and giving some ID and class to the SPAN it started updating the sum realtime with same mistakes (negative amounts and/or such), and it has style issues. In any case, I will try to modify it and give any thoughts if any success.</span>

Edited by sting5 (see edit history)

Share this post


Link to post
Share on other sites

Ok, i finally made it work in ajax as well. You need to insert this code to blockcart.tpl:

 

<!-- free shipping module -->
 {if $free_ship >= 0 AND !$isVirtualCart}<p class="ajax_cart_free_shipping_on">
   <span>{l s='Till free shipping' mod='blockcart'}</span>
   <span id="cart_block_free_shipping" class="price ajax_cart_free_shipping">{$free_ship}</span></p>
{/if}

{if $free_ship < 0 AND !$isVirtualCart}
<p class="ajax_cart_free_shipping_off"><span>{l s='Shipping is free!' mod='blockcart'}</span></p>
{/if}

<!-- end of free shipping module -->

 

 

and add this code to ajax-cart.js right after "$('.ajax_cart_shipping_cost').text(jsonData.shippingCost);":

 

 $('.ajax_cart_free_shipping').text(jsonData.freeShipping);

 //make ajax hide when below zero, and show when above

 if(parseFloat(jsonData.freeShipping.replace(",", ".")) >0)
  {
   $('.ajax_cart_free_shipping_on').each( function () {
 $(this).fadeIn('slow');
   });

   $('.ajax_cart_free_shipping').each( function () {
 $(this).fadeIn('slow');
   });
   $('.ajax_cart_free_shipping_off').each( function () {
 $(this).fadeOut('slow');
   });

the second code needs corrections, while it only works either when sum till free shipping is above zero or below zero.

Share this post


Link to post
Share on other sites

I prefer using the not ajax cart module, and set in Back Office the minimun amount for free shipping. But in the blockcart-tpl there is nothing like a code free shipping or similar. How can I enable the sign under the cart summary?

thanks.

Share this post


Link to post
Share on other sites

@rocky - this is still not functioning properly, is there any updates yet to the AJAX auto-refresh?

 

@plauengco - did you get yours to work?

 

I agree! it is still good but not perfect. It is still remaining un-solved. Could anyone help out with this? Thanks

Share this post


Link to post
Share on other sites

This is how I got it working well enough to do me in 1.5.3.1

(Bug- adding an item to cart to take it to an amount that qualifies for free shipping doesn't actually clear the shipping cost itself, though it does adjust the new message. not sure if I've created this myself or if it was in the original cart code.

 

ajax-cart.js

after

if (parseFloat(jsonData.shippingCostFloat) > 0 || jsonData.nbTotalProducts < 1)
$('.ajax_cart_shipping_cost').text(jsonData.shippingCost);
else if (typeof(freeShippingTranslation) != 'undefined')
$('.ajax_cart_shipping_cost').html(freeShippingTranslation);

add

//ROB2013
$('.ajax_cart_free_shipping').text(jsonData.freeShipping);

//make ajax hide when below zero, and show when above

if(parseFloat(jsonData.freeShipping.replace(",", ".")) >0)
{
$('.ajax_cart_free_shipping_on').each( function () {
$(this).fadeIn('slow');
});

$('.ajax_cart_free_shipping').each( function () {
$(this).fadeIn('slow');
});
$('.ajax_cart_free_shipping_off').each( function () {
$(this).fadeOut('slow');
});
};
// End ROB2013

 

blockcart.php. After

else if (typeof(freeShippingTranslation) != 'undefined')
$('.ajax_cart_shipping_cost').html(freeShippingTranslation);

add

//ROB2013
$summary = $params['cart']->getSummaryDetails();

if ($free_ship = Tools::convertPrice(floatval(Configuration::get('PS_SHIPPING_FREE_PRICE')), new Currency(intval($params['cart']->id_currency))))
{
$discounts = $params['cart']->getDiscounts();
$total_free_ship = round($free_ship - ($summary['total_products_wt'] + $summary['total_discounts']),2);
foreach ($discounts as $discount)
if ($discount['id_discount_type'] == 3)
{
$total_free_ship = 0;
break ;
}
if ($total_free_ship <= 0) $total_free_ship = "nothing";
   else $total_free_ship = Tools::displayPrice($total_free_ship, $currency);

$this->smarty->assign('free_ship', $total_free_ship);
}
//ROB2013 End

 

blockcart-json.tpl after

"shippingCost": "{$shipping_cost|html_entity_decode:2:'UTF-8'}",

Add

{* ROB2013 *}
"freeShipping": "{$free_ship|html_entity_decode:2:'UTF-8'}",
{* ROB2013 End *}

 

blockcart.tpl before

<p id="cart-prices">

Add

{* ROB2013 added, and virtualcart removed * }
<!-- free shipping module -->
{* {if true} *}
{if $free_ship != ""}
</p><p class="ajax_cart_free_shipping_on">
<span>{l s='For free shipping add' mod='blockcart'}</span>
<span id="cart_block_free_shipping" class="price ajax_cart_free_shipping">{$free_ship}</span></p>
{/if}
{* {/if} *}

{* if $free_ship <= 0}
<p class="ajax_cart_free_shipping_off"><span>{l s='Shipping is free!' mod='blockcart'}</span></p>
{/if *}

<!-- end of free shipping module -->
{* ROB2013 End *}

 

Hope that helps...

 

Edit: Add round( .. ,2) to $total_free_ship definition after getting a "0.01000000000002" value appear! (Floats are not precise!)

Edited by irrelevant (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

Hello, I tried above solution on 1.5.5 but it's not working plus i am not able to find this 

 

blockcart.php. After

else if (typeof(freeShippingTranslation) != 'undefined')
$('.ajax_cart_shipping_cost').html(freeShippingTranslation);

Share this post


Link to post
Share on other sites

I have installed this fucntion on my shop for more than 1 year now. It is still not auto upated. It is little bit disapointing but better than nothing.

Edited by todaytonight (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

@mianzeeshantahir

else if (typeof(freeShippingTranslation) != 'undefined')
$('.ajax_cart_shipping_cost').html(freeShippingTranslation);

refers to js file, nothing to do with php

Share this post


Link to post
Share on other sites

Hi maio, thank you very much for relay. Could you please give little bit more information about the changes please? Where this code should be replaced? Thanks

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

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