Jump to content

Adding an array to smarty $cart subtotals array


Erkan Yilmaz

Recommended Posts

Hi Everyone,

I want to add a new array in smarty global variables $cart.subtotals

The array now is like this:

array (size=4)
  'products' =>array (size=4)
      'type' => string 'products' (length=8)
      'label' => string 'Sous-total' (length=10)
      'amount' => float 29
      'value' => string '29,00 €' (length=10)
  'discounts' => null
  'shipping' =>array (size=4)
      'type' => string 'shipping' (length=8)
      'label' => string 'Livraison' (length=9)
      'amount' => float 10
      'value' => string '10,00 €' (length=10)
  'tax' => array (size=4)
      'type' => string 'tax' (length=3)
      'label' => string 'Taxes' (length=5)
      'amount' => float 8.19
      'value' => string '8,19 €' (length=9)

Required array is like this (pleace see bold and italic phrase):

array (size=4)
  'products' =>array (size=4)
      'type' => string 'products' (length=8)
      'label' => string 'Sous-total' (length=10)
      'amount' => float 29
      'value' => string '29,00 €' (length=10)
  'discounts' => null
  'shipping' =>array (size=4)
      'type' => string 'shipping' (length=8)
      'label' => string 'Livraison' (length=9)
      'amount' => float 10
      'value' => string '10,00 €' (length=10)
      'handling_cost_enabled' =>boolean true|false
       'handling_cost' => float 5

  'tax' => array (size=4)
      'type' => string 'tax' (length=3)
      'label' => string 'Taxes' (length=5)
      'amount' => float 8.19
      'value' => string '8,19 €' (length=9)

I spent hours on searching how to do but I couldn't find it. Which class have I to modify to achieve desired result?

Anyone an idea?

With kind regards, Erkan

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

Hi 4you.software,

Handlingcost can be found in db  The keyvalues can be assigned to value while smarty compiles $cart array. But I don't know where and which classes compiles that array. I need the values in context and not hard coded. If someone knows, it would be nice to know. It's also nice for whose are looking for solution the same issue.

With kind regards, Erkan

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

./src/Adapter/Presenter/Cart/CartPresenter.php => present

It is necessary to add functions to either Presenter or Cart.php.

This is a task for programmers who know Prestashop. I do not recommend such modifications for beginners.

If you don't know where the two parameters you need are stored, there can't even be a sample code for editing. It is quite a complex modification and writing it here a million times with modifications is time-consuming.

 

Link to comment
Share on other sites

Hi 4you.software,

Thank you for your reply. It helped me a lot.

Solutions:

At first I created a public function in Cart.php with boolean return. Afterwards extended the $shipping array with value of function I created before. Important here is that the value has to be stored in Cache (Cache::store($cache_id, $public_variable)). Otherwise it doesn't work.

With kind regards, Erkan.

Link to comment
Share on other sites

I think this is going about it the wrong way.
Just use the hook in the module.

Add Hook

$this->registerHook('actionPresentCart');

https://devdocs.prestashop-project.org/1.7/modules/concepts/hooks/list-of-hooks/

* find actionPresentCart for more info.

 

And hook function

public function hookActionPresentCart($params)
{
	$presentedCart = $params;    
	/* add your function for find values */

	$presentedCart['presentedCart']['subtotals']['shipping']['handling_cost_enabled'] = false;
	$presentedCart['presentedCart']['subtotals']['shipping']['handling_cost'] = floatval('5');

}

 

Tested in cart-summary-subtotals.tpl

{$cart.subtotals.shipping|@var_dump}

result:

obrazek.png.02e96fb6a1d15883e7fa565ce4ac35df.png

Edited by 4you.software (see edit history)
  • Thanks 1
Link to comment
Share on other sites

Hi,

Yes, indeed. It's one of possible solution. I didn't try it. Now the solution I created it works. Why? My client wants it seperatly. Depending on the shipping option, the handling fee may or may not be calculated. That's where the difficulty lay. On carrier X the handling fee may not added and on carrier Y the handling fee must be added. In Cart.php is the handling fee calculated automaticlly and can't be shown seperatlly. Now the "handling_cost_enabled" is assigned dynamiclly in cart-summary-subtotals.tpl and I can show the correct fee seperatly.

I do appreciate your well documented explanation and is to remember.

With regards, Erkan.

  • Like 1
Link to comment
Share on other sites

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...