News:

Support the VirtueMart project and become a member

Main Menu

custom price calculation how to change cart price

Started by bitstomper, May 28, 2012, 19:04:39 PM

Previous topic - Next topic

bitstomper

Hi,
I managed in VM 1.xxx to create a custom calculation and pass results on to the cart (see: www.gordijnliefde.nl).
Now with VM 2 I want to do this slightly better.
Can anyone point me to what is the route for the addtocart part.
How can I add my own price to the cart.
Always bugged me that VM does not seem to provide an easy solution for developing own calculations.
VM keep looking at the original set price for an item (product_price)
Where is the price stored when for example custom fields with values are added to the price.
Does VM keep calculating the price based on some paramaters (eg. custom field A ($20) is added to the price so cart keeps adding this to the price).
Or is the new price stored somewhere (cookie/session)? and cart grabs price from there?

I would really like to understand the routing.
Once in the product details view where are the values of the form fields sent to once submitted?

Once I understand more I might look in to developing a plugin which does this.
Seems like there are a lot of requests for this.

Thanks heaps in advance!!!




sadrem

same here, very pure developing documentation.  Also want to know how to develop plugins, what classes to use ...
p.s. Do not forget to change message icon to Solved.

bitstomper

OK,

I finally managed to change prices.
So FYI, here is what I did:

After selecting a product in category view, minimal product data is set in the session. VM2 developers have tried to keep the session as clean as possible as loading a lot of data into session slows down the site heaps. So at this level there is not a lot of data in the session cookie.
The data is set with the function setCartIntoSession() in components/com_vm/helpers/cart.php.
You can not manipulate prices here but you can add variables to the session (eg. 'updated_price').
This will show up in the session. The easy thing here is that you can pick up variables sent via 'post' from your addtocart form:
$product -> product_adjusted_price = $post['adjusted_price'];

So I have made a calculation and determined the final price in the form depending on user input. Called that 'adjusted_price' and loaded that in an (hidden) input field in the form. This value is now picked up and sent to the session where it can be picked up further down the track.

The variable is picked up by the calculation model which calculates all prices (tax, subtotals, discounts, totals, etc.).
So you can manipulate prices here: admin/components/com_vm/helpers/calculationh.php

My set up is quite simple: I only have one total price after the calculation in the addtocart form. If you have other criteria which effect the outcome you could easily set more parameters via the above steps and pick them up in the calculation model.

To give you a simple example: I changed line 469 to be:

if($this->_currencyDisplay->_priceConfig['salesPrice']) $this->_cartPrices['salesPrice'] = $this->_cartPrices['salesPrice'] + $product->product_adjusted_price * $product->quantity;

The product_adjusted_price is the one I passed on from the addtocart form.

Now all you need to do is make sure these prices are displayed:
Create a override file (duplicate the com_vm/views/cart/default_priceslist.php into a new folder called 'cart' in your folder your_template/html).

Since my shop will only use one currency I commented out:
echo $this->currencyDisplay->createPriceDiv('salesPrice','', $this->cart->pricesUnformatted[$pkey],false);
and instead used:

echo "€ ".$prow->product_adjusted_price;


where applicable.
It is better however to formulate the price in the standard VM way. Not sure yet how that is done (if you can use the createPriceDiv object or if you have to write your own).

Hope this helps.




priyank2512

#4
prdodcut_adjusted_price not working properly.
can anybody help?
1st time $product->product_adjusted_price take value in session but second time update same product it can't be update value.
quantity update but $product->product_adjusted_price value not update.
please help.

how to unset or update prdodcut_adjusted_price value when same product update.

thank you
no signature

Milbo

Interesting. But why not just using the value of the customfield? load the sampeldata and look at the chain saw
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

spk063

@bitstomper, i have a doubt in your post. u said that

To give you a simple example: I changed line 469 to be:

if($this->_currencyDisplay->_priceConfig['salesPrice']) $this->_cartPrices['salesPrice'] = $this->_cartPrices['salesPrice'] + $product->product_adjusted_price * $product->quantity;

in which file this code to be palced. I'm using Virtuemart 2.0.2a

Maxim Pishnyak

Quote from: bitstomper on May 29, 2012, 23:15:37 PM
So you can manipulate prices here: admin/components/com_vm/helpers/calculationh.php
Quote from: spk063 on August 19, 2013, 14:12:12 PM
I'm using Virtuemart 2.0.2a
Maybe 22a?
You can support Community by voting for Project on the JED
https://extensions.joomla.org/extension/virtuemart/#reviews
Join us at
https://twitter.com/virtuemart

spk063

i tried but it doesn't works.. Please explain me clearly!!

Maxim Pishnyak

Quote from: spk063 on August 21, 2013, 07:53:25 AM
i tried but it doesn't works..
What possible issues you could have with posted above solution
http://forum.virtuemart.net/index.php?topic=103218.msg343047#msg343047
?

Maybe it's possible to nullify price for free products with the help of coupons?

It's a bit late to talk about this, but I think I saw in 3rd party forum extension(s) for utilizing 'bonus products'.
You can support Community by voting for Project on the JED
https://extensions.joomla.org/extension/virtuemart/#reviews
Join us at
https://twitter.com/virtuemart

lmalcom

Hey bitstomper I need help taking 10% of the total cost and making that the shipping cost. Can you help with this?

Quote from: bitstomper on May 29, 2012, 23:15:37 PM
OK,

I finally managed to change prices.
So FYI, here is what I did:

After selecting a product in category view, minimal product data is set in the session. VM2 developers have tried to keep the session as clean as possible as loading a lot of data into session slows down the site heaps. So at this level there is not a lot of data in the session cookie.
The data is set with the function setCartIntoSession() in components/com_vm/helpers/cart.php.
You can not manipulate prices here but you can add variables to the session (eg. 'updated_price').
This will show up in the session. The easy thing here is that you can pick up variables sent via 'post' from your addtocart form:
$product -> product_adjusted_price = $post['adjusted_price'];

So I have made a calculation and determined the final price in the form depending on user input. Called that 'adjusted_price' and loaded that in an (hidden) input field in the form. This value is now picked up and sent to the session where it can be picked up further down the track.

The variable is picked up by the calculation model which calculates all prices (tax, subtotals, discounts, totals, etc.).
So you can manipulate prices here: admin/components/com_vm/helpers/calculationh.php

My set up is quite simple: I only have one total price after the calculation in the addtocart form. If you have other criteria which effect the outcome you could easily set more parameters via the above steps and pick them up in the calculation model.

To give you a simple example: I changed line 469 to be:

if($this->_currencyDisplay->_priceConfig['salesPrice']) $this->_cartPrices['salesPrice'] = $this->_cartPrices['salesPrice'] + $product->product_adjusted_price * $product->quantity;

The product_adjusted_price is the one I passed on from the addtocart form.

Now all you need to do is make sure these prices are displayed:
Create a override file (duplicate the com_vm/views/cart/default_priceslist.php into a new folder called 'cart' in your folder your_template/html).

Since my shop will only use one currency I commented out:
echo $this->currencyDisplay->createPriceDiv('salesPrice','', $this->cart->pricesUnformatted[$pkey],false);
and instead used:

echo "€ ".$prow->product_adjusted_price;


where applicable.
It is better however to formulate the price in the standard VM way. Not sure yet how that is done (if you can use the createPriceDiv object or if you have to write your own).

Hope this helps.

jenkinhill

Imalcom please do not hijack old and unrelated threads.

You have already been told how this can be done. http://forum.virtuemart.net/index.php?topic=122328.msg417684#msg417684
Kelvyn
Lowestoft, Suffolk, UK

Retired from forum life November 2023

Please mention your VirtueMart, Joomla and PHP versions when asking a question in this forum