VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: bitstomper on May 28, 2012, 19:04:39 PM

Title: custom price calculation how to change cart price
Post by: bitstomper on May 28, 2012, 19:04:39 PM
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!!!



Title: Re: custom price calculation how to change cart price
Post by: sadrem on May 28, 2012, 19:26:13 PM
same here, very pure developing documentation.  Also want to know how to develop plugins, what classes to use ...
Title: Re: custom price calculation how to change cart price
Post by: 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.


Title: Re: custom price calculation how to change cart price
Post by: shone32 on March 13, 2013, 10:06:10 AM
(http://C:%5CUsers%5CFurnex%5CDesktop%5C2.jpg)
Title: Re: custom price calculation how to change cart price
Post by: priyank2512 on June 28, 2013, 13:05:05 PM
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
Title: Re: custom price calculation how to change cart price
Post by: Milbo on June 28, 2013, 15:37:48 PM
Interesting. But why not just using the value of the customfield? load the sampeldata and look at the chain saw
Title: Re: custom price calculation how to change cart price
Post by: spk063 on August 19, 2013, 14:12:12 PM
@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
Title: Re: custom price calculation how to change cart price
Post by: Maxim Pishnyak on August 19, 2013, 16:36:30 PM
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?
Title: Re: custom price calculation how to change cart price
Post by: spk063 on August 21, 2013, 07:53:25 AM
i tried but it doesn't works.. Please explain me clearly!!
Title: Re: custom price calculation how to change cart price
Post by: Maxim Pishnyak on August 21, 2013, 16:12:50 PM
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'.
Title: Re: custom price calculation how to change cart price
Post by: lmalcom on February 24, 2014, 17:41:42 PM
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.
Title: Re: custom price calculation how to change cart price
Post by: jenkinhill on February 24, 2014, 17:59:44 PM
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