Load product data with right prices based on shoppergroup

Started by JeremyAcy, September 01, 2015, 11:51:57 AM

Previous topic - Next topic

JeremyAcy

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

Milbo

To use the shoppergroup you need this "appendShopperGroups" before getting the product,

please use VmModel::getModel('product')
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

JeremyAcy

Thanks, I'll take a look at the "appendShopperGroups" and keep you informed ;)

JeremyAcy

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

JeremyAcy

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

Milbo

$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.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Alan428

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