Jump to content

[SOLVED] Display total weight in blockcart


Recommended Posts

Hi
I would like to display the total weight in the module blockcart.

In module/blockcart.php,
after

'id_carrier' => $params['cart']->id_carrier,


I added the code

'weightUnit' => Configuration::get('PS_WEIGHT_UNIT'),




and in module/blockcart.tpl
after

{l s='Total' mod='blockcart'}
{$total}

I added the code

{l s='Weight :' mod='blockcart'}
{$cart->getTotalWeight()|escape:'htmlall':'UTF-8'|number_format:3} {$weightUnit}



The total weight is displayed if you refresh the page, but there is no interaction effect with ajax when you add or remove products.

Can anyone help me?

--------------
Ver 1.2.5

Link to comment
Share on other sites

Unfortunately, it's not an easy change to make. I'll try to explain what you need to do. Try changing lines 137-142 of modules/blockcart/blockcart.php from:

public function hookAjaxCall($params)
{
   global $smarty;
   $this->smartyAssigns($smarty, $params);
   return $this->display(__FILE__, 'blockcart-json.tpl');
}



to:

public function hookAjaxCall($params)
{
   global $smarty;
   $this->smartyAssigns($smarty, $params);
   $smarty->assign('total_weight', $params['cart']->getTotalWeight() . ' ' . Configuration::get('PS_WEIGHT_UNIT'));
   return $this->display(__FILE__, 'blockcart-json.tpl');
}



then change line 68 of modules/blockcart/blockcart-json.tpl from:

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



to:

'totalWeight': '{$total_weight|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_block_cart_weight').text(jsonData.totalWeight);

Link to comment
Share on other sites

I have not received notification of the response to this message.
Thank you very much for your help!
your explanations are clear and I managed to make the changes.
In the line 68 of modules/blockcart/blockcart-json.tpl from I kept the code :

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


and i add after :

'totalWeight': '{$total_weight|html_entity_decode:2:'UTF-8'}', 


because when the blockcart is emptied, the shipping cost is not updated.

Last question about this solution :
How to display the weight to 3 decimal (after comma) ?

Link to comment
Share on other sites

The number_format function can be used to display the weight with 3 decimal places:

public function hookAjaxCall($params)
{
   global $smarty;
   $this->smartyAssigns($smarty, $params);
   $smarty->assign('total_weight', number_format($params['cart']->getTotalWeight(), 3) . ' ' . Configuration::get('PS_WEIGHT_UNIT'));
   return $this->display(__FILE__, 'blockcart-json.tpl');
}

Link to comment
Share on other sites

Hi Rocky!
how are you?
I hope you can help me out quickly ;)
I installed your contributions to do the WEIGHT as a SHIPPING PRICE

But here is my problem...
i add my product , add attributes to the product ( i make SURE not to increase weight)
Then i set the general weight of item to 25Kg for instance...

THen i save it all , but when i click on item in my shop and decide to check out , no shipping cost is added....
How come?

My shop address : www.officefive.com

thanks!

Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...
  • 1 month later...
  • 1 month later...
  • 3 months later...

Hi guys

 

I follow this post because i want to do the same thing show the total weight in the block cart

 

so i follow one by one the instructions for the version 1.4.4.1

 

First you must do all the steps sayed before I repeat ALL

 

and the final step is this:

 

in the file blockcart.tpl

 

 

leeloo

 

say add this

 

{l s='Weight :' mod='blockcart'}
{$cart->getTotalWeight()|escape:'htmlall':'UTF-8'|number_format:3} {$weightUnit}

 

you must change it for:

 

			<span>{l s='Weight :' mod='blockcart'}</span>
		<span id="cart_block_weight" class="price ajax_block_cart_weight">{$totalWeight}{$weightUnit}</span

 

I hope it helps you and thanks to Leeloo and rocky

Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

I answer myself alone .....

 

Simply just add in order-carrier.tpl (because I want it here):

 

{$cart->getTotalWeight()|escape:'htmlall':'UTF-8'|number_format:0} {Configuration::get('PS_WEIGHT_UNIT')}

 

but if in "order summary" page I add (or remove) some products total weight doesn't refresh....

 

someone can help me to complete my work?

 

Thank!

Link to comment
Share on other sites

  • 1 month later...

Works perfectly on 1.4.7.3

No problems at all, and it doesn't matter how many products the customer want to buy, it will calculate it all together.

 

Great option, might be a lovely option for the 1.5, to have an option for the weight calculator on the cart.

 

Thanks for the help!!

Link to comment
Share on other sites

  • 7 months later...
  • 3 months later...
  • 1 month later...
  • 1 month later...

Unfortunately, it's not an easy change to make. I'll try to explain what you need to do. Try changing lines 137-142 of modules/blockcart/blockcart.php from:

 

public function hookAjaxCall($params)
{
global $smarty;
$this->smartyAssigns($smarty, $params);
return $this->display(__FILE__, 'blockcart-json.tpl');
}

 

to:

 

public function hookAjaxCall($params)
{
global $smarty;
$this->smartyAssigns($smarty, $params);
$smarty->assign('total_weight', $params['cart']->getTotalWeight() . ' ' . Configuration::get('PS_WEIGHT_UNIT'));
return $this->display(__FILE__, 'blockcart-json.tpl');
}

 

then change line 68 of modules/blockcart/blockcart-json.tpl from:

 

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

 

to:

 

'totalWeight': '{$total_weight|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_block_cart_weight').text(jsonData.totalWeight);

 

Hi rocky tanks for your help

 

how to make the same "update the weight when changing the quantity of the product" on the prestashop 1.5.4.1?

 

 

best regards

Edited by majority (see edit history)
Link to comment
Share on other sites

  • 1 month later...

somehow i got the dynamic weight on blockcart not functioning anymore, it's happen after i doing re-install the PS with the same version with maintaining old data site, the sad news is Rocky not wandering anymore for Prestashop, he sure have other things to do, back again i think perhaps the culprit is the smarty 3, since before i do the install i used smarty 2, i search Google and Forum but there's still no one offering the same dynamic idea for blockcart, temporary solution for me is putting static weight info shopping page detail, when customer do checkout they gonna see the total weight info, but if they're doing any change with the cart they still need to refresh the page to get updated weight

 

put this code whenever you want the weight to show:

{$cart->getTotalWeight()|escape:'htmlall':'UTF-8'|number_format:2} {Configuration::get('PS_WEIGHT_UNIT')}

 

change number decimal format into your desire

add

{l s='text'}

before or after the code to give extra information for your customer

 

ps: i put the code on order-opc.tpl after {include file="$tpl_dir./shopping-cart.tpl"}

Edited by deprims (see edit history)
Link to comment
Share on other sites

  • 8 months later...
  • 5 months 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...