Author Topic: custom price calculation how to change cart price  (Read 10413 times)

bitstomper

  • Beginner
  • *
  • Posts: 19
custom price calculation how to change cart price
« 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!!!




sadrem

  • Jr. Member
  • **
  • Posts: 172
Re: custom price calculation how to change cart price
« Reply #1 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 ...
p.s. Do not forget to change message icon to Solved.

bitstomper

  • Beginner
  • *
  • Posts: 19
Re: custom price calculation how to change cart price
« Reply #2 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:
Code: [Select]
$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:

Code: [Select]
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:
Code: [Select]
echo $this->currencyDisplay->createPriceDiv('salesPrice','', $this->cart->pricesUnformatted[$pkey],false);and instead used:

Code: [Select]
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.



shone32

  • Beginner
  • *
  • Posts: 1
  • Skype Name: furnex.veleprodaja.okova
Re: custom price calculation how to change cart price
« Reply #3 on: March 13, 2013, 10:06:10 am »

priyank2512

  • Beginner
  • *
  • Posts: 2
  • No text
Re: custom price calculation how to change cart price
« Reply #4 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
no signature

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10144
  • VM3.9 Eagle Owl
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: custom price calculation how to change cart price
« Reply #5 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
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

  • Beginner
  • *
  • Posts: 16
Re: custom price calculation how to change cart price
« Reply #6 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

Maxim Pishnyak

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2664
Re: custom price calculation how to change cart price
« Reply #7 on: August 19, 2013, 16:36:30 pm »
So you can manipulate prices here: admin/components/com_vm/helpers/calculationh.php
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

  • Beginner
  • *
  • Posts: 16
Re: custom price calculation how to change cart price
« Reply #8 on: August 21, 2013, 07:53:25 am »
i tried but it doesn't works.. Please explain me clearly!!

Maxim Pishnyak

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2664
Re: custom price calculation how to change cart price
« Reply #9 on: August 21, 2013, 16:12:50 pm »
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

  • Beginner
  • *
  • Posts: 5
Re: custom price calculation how to change cart price
« Reply #10 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?

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:
Code: [Select]
$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:

Code: [Select]
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:
Code: [Select]
echo $this->currencyDisplay->createPriceDiv('salesPrice','', $this->cart->pricesUnformatted[$pkey],false);and instead used:

Code: [Select]
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

  • UK Web Developer & Consultant
  • Global Moderator
  • Super Hero
  • *
  • Posts: 28225
  • Always on vacation
    • Jenkin Hill Internet
Re: custom price calculation how to change cart price
« Reply #11 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
Kelvyn

Jenkin Hill Internet,
Lowestoft, Suffolk, UK

Unsolicited PMs/emails will be ignored.

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

Currently using VirtueMart 3.8.9.10473 on Joomla 3.9.28 PHP 7.4.14