Author Topic: How to dynamically change product price before adding to cart? (no variants)  (Read 1185 times)

aerdna

  • Beginner
  • *
  • Posts: 29
  • A beginner
I'm running joomla 3.6.2 and VM 3.0.16, php5.6.

I just need to know where in a vmcustom plugin like textinput can I get access to the price that is being used for the final product price and at what point could I change the value so it would take effect in the cart? 

Here's the details: 
I have jewelry made from precious metals and I'd like my product prices to update dynamically based on the current market prices for the metals.  I currently have a plugin that is built off of the vmcustom/textinput plugin (I'm an extreme beginner here so this was the easiest way I could figure out how to do this) that I use to dynamically update the price based on the customer's selected ring size and selected gemstones, engraving, etc.  (I query for compatible gemstone inventory dynamically from the manufacturer's RESTful endpoint and so I couldn't use a variant for these things.)  The front end uses javascript to configure the product and the backend uses the plugin. It uses the plgVmPrepareCartProduct() function to add on the additional cost for the configured items like ring sizing, gemstones, etc.  I know I should have probably used a plugin for the FE too but I didn't know how.

The way I'm doing it now assumes that the product price is correct and just adds on to it additional costs using the "modificatorSum".  Unfortunately, the product price will fluctuate with the market. I don't want to set up a cron job to update all the prices in my store every night based on the market, I'd rather just update the price on the fly for the products that are configurable and will be manufactured based on the customer's specifications.  The customer expects to see the price change for those items.

So...having said all that...I don't want to change the price in the database, just the final price that would go into the cart. 

It would also be nice if I knew where I could plug in something to check a person's cart to see if a price has changed (like if they keep something in their cart for a long time then decide to checkout). I don't know how to get access to cart contents but it would be nice to be able to do that when they navigate to the cart. (Maybe I should post a separate question for this?)

Could someone point me in the right direction for these?  I realize I'm asking a lot of specifics here but even hints help me :-) 

Thanks,
Andrea

kishoreonwork

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 56
  • Free Virtuemart Consulting For Non Profits
    • virtuemart expert
  • Skype Name: kishore2607
  • VirtueMart Version: 3

You can do this with product override.
in the function do it like that
$product->allPrices[0]['override']=1;
$product->allPrices[0]['product_override_price']={new_override_price};

Let say the price is increased by 10% then do it
$discount=10;
$product->allPrices[0]['override']=1;
$product->allPrices[0]['product_override_price']=($product->allPrices[0]['product_price'] )*(100+$discount)/100;


Let say the price is decrease by 5% then do it
$discount=5;
$product->allPrices[0]['override']=1;
$product->allPrices[0]['product_override_price']=($product->allPrices[0]['product_price'] )*(100-$discount)/100;

Let say the you want to set price to $200
$product->allPrices[0]['override']=1;
$product->allPrices[0]['product_override_price']=200;


So there are endless possibilities

Thanks
Kishore

I am available for paid joomla and virtuemart consulting.
http://www.kishoreweblabs.com/
skype kishore2607

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 3466
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
If you hacked the plgVmPrepareCartProduct core function , it's hard to help you.
And you should update VIrtuemart and Joomla.
What i dont understand, is why you dont use modificatorSum in your plugin to update the price ?

jenkinhill

  • UK Web Developer & Consultant
  • Global Moderator
  • Super Hero
  • *
  • Posts: 27043
  • Always on vacation
    • Jenkin Hill Internet
Patrick the original post is from 2 years ago, hence the old versions. Don't know why it should get a reply now, the OP has not visted the forum since that time.
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 VM 3.4.0.9935 on Joomla 3.8.12 PHP 7.0.31
Testing VM 3.4.0.9941 on Joomla 3.8.12

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 3466
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Thanks Jenkinhill, I did not pay attention.