Author Topic: Load product data with right prices based on shoppergroup  (Read 2419 times)

JeremyAcy

  • Beginner
  • *
  • Posts: 6
  • A beginner
Load product data with right prices based on shoppergroup
« 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:

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

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10070
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Load product data with right prices based on shoppergroup
« Reply #1 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')
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

JeremyAcy

  • Beginner
  • *
  • Posts: 6
  • A beginner
Re: Load product data with right prices based on shoppergroup
« Reply #2 on: September 08, 2015, 10:09:07 am »
Thanks, I'll take a look at the "appendShopperGroups" and keep you informed ;)

JeremyAcy

  • Beginner
  • *
  • Posts: 6
  • A beginner
Re: Load product data with right prices based on shoppergroup
« Reply #3 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:
Code: [Select]
$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:
Code: [Select]
$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

  • Beginner
  • *
  • Posts: 6
  • A beginner
Re: Load product data with right prices based on shoppergroup
« Reply #4 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

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10070
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Load product data with right prices based on shoppergroup
« Reply #5 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.
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Alan428

  • Beginner
  • *
  • Posts: 1
  • A beginner
Re: Load product data with right prices based on shoppergroup
« Reply #6 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:

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