VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Coding Central => Topic started by: JeremyAcy on September 01, 2015, 11:51:57 AM

Title: Load product data with right prices based on shoppergroup
Post by: JeremyAcy on September 01, 2015, 11:51:57 AM
Hi, hope it's the right forum section for this question,

We developed a plugin that inserts VirtueMart products in a newsletter, but we have troubles getting the correct prices based on a specific shoppergroup  :'(

We're currently using this code:


$productId = 524;
$shoppergroup = array(2);

$vmProductModel = new VirtueMartModelProduct();
$product = $vmProductModel->getProduct($productId, false, true, false, 1, false, $shoppergroup);
$vmProductModel->addImages($product);

$price = $product->product_price;
if($includeTax == 1 && !empty($product->prices['basePriceWithTax'])) $price = $product->prices['basePriceWithTax'];


But it does not seem to take the discount and rules into account for VM2 and VM3  :(

Do you have any API to load the product with the right prices? Ideally we would like to be able to get the price either with or without the tax.
Our script can be executed from the Front-end and the Back-end, and when it's executed there could be no user logged-in (executed with a cron task).


Thanks for any hint you could give us  ;D

Jeremy
Title: Re: Load product data with right prices based on shoppergroup
Post by: Milbo on September 07, 2015, 11:52:48 AM
To use the shoppergroup you need this "appendShopperGroups" before getting the product,

please use VmModel::getModel('product')
Title: Re: Load product data with right prices based on shoppergroup
Post by: JeremyAcy on September 08, 2015, 10:09:07 AM
Thanks, I'll take a look at the "appendShopperGroups" and keep you informed ;)
Title: Re: Load product data with right prices based on shoppergroup
Post by: JeremyAcy on September 10, 2015, 16:54:57 PM
Hello,

I found my mistake in the previous post and removed the "false" parameter before adding the $shoppergroup parameter in this line:
$product = $vmProductModel->getProduct($productId, false, true, false, 1, false, $shoppergroup);

Now I get all the good prices with the good tax / discounts based on the shoppergroups I put in parameter except for one thing:
If I create a new "Tax & Calculation Rule" that applies -8% from the price for the Shopper group "Wholesale" for example, it won't be taken into account  :-\

Here is the code I use now:
$shoppergroups = array(2);

$user = new stdClass();
$user->guest = false;

$shopperModel = VmModel::getModel('ShopperGroup');
$shopperModel ->appendShopperGroups($shoppergroups,$user,false);

$vmProductModel = VmModel::getModel('product');
$product = $vmProductModel->getProduct(524, false, true, false, 1, $shoppergroups);

$price = $product->prices['product_price'];


Thanks for your help and patience,
Jeremy
Title: Re: Load product data with right prices based on shoppergroup
Post by: JeremyAcy on September 29, 2015, 09:50:12 AM
Do you have any advise please? Shouldn't the getproduct function return the price by taking into account a global rule on the specified shoppergroup?

Regards,
Jeremy
Title: Re: Load product data with right prices based on shoppergroup
Post by: Milbo on September 29, 2015, 11:13:19 AM
$shopperModel ->appendShopperGroups($shoppergroups,$user,false);

There is an inconsistency within the calculationhelper, which keeps its own shoppergroup ids. You may try to change them here, setShopperGroupIds.

Of course when you find a nice solution, I am interested.
Title: Re: Load product data with right prices based on shoppergroup
Post by: Alan428 on January 06, 2020, 11:53:43 AM
Hello there,

I've used the following code to get the price of a product for a specific shoppergroup for some time in my extension:


$productId = 425;
$shopperGroupId = 3;

$vmProductModel = VmModel::getModel('product');
$product = $vmProductModel->getProduct($productId, false, true, false, 1, [$shopperGroupId]);


Depending on the VirtueMart version, the price will be in either $product->prices or $product->product_price. This is however no longer the case and $vmProductModel->getProduct will now ignore the provided shoppergroup IDs.
Is this a bug or has the method been changed to get the correct price for a specific shoppergroup?

This shoppergroup has this discount:
https://ibb.co/rp6FC1K

And on the product page, this price is displayed:
https://ibb.co/ZNPSD8Z


Thank you for any input, I'll continue searching but if someone has any clue on how to make it work with the latest version of VirtueMart that would be really appreciated