Jump to content

retrieve products and prices through the product model with currency and name localized


bellafronte

Recommended Posts

Hello WHMCS Community!

I'm building a custom page on WHMCS that needs to retrieve all the products on a specific product group. In this step so far so good.

But, these products have their names translated into several languages using the dynamic translation and different currency prices, looking into WHMCS Classes Model, I found on Product Model the methods that can retrieve this information in the current language and currency set.

I build a simple loop that catches all the products ID:

  $command = 'GetProducts';
  $productgroupid = 12;
  $postData = array(

    'gid' => $productgroupid,

  );

  $results = localAPI($command, $postData);
  $productsId = array();
  foreach ($results['products']['product'] as $product) {
    $productsId[] = $product['pid'];
  }

After this, I passed this products ID's to Product Model to get the information that I need:

foreach ($productsId as $key => $value) {
  $products[$key] = [
    'name' => Product::find($value)->getNameAttribute(),
    'description' => Product::find($value)->getDescriptionAttribute(),
    'price' => Product::find($value)->pricing(),
  ];
}

This works perfectly well, but the  princing() method return only the default currency. I make a test outside the loop to get only one product and tested with different currencies, and the result is that what I expected:

  $userid = (isset($_SESSION['uid']) ? $_SESSION['uid'] : "");
  $currencyid = (isset($_SESSION['currency']) ? $_SESSION['currency'] : "");
  // $currencyid = 5;
  $currency = getCurrency($userid, $currencyid);
  var_dump($currency);
  var_dump(Product::find(1))->pricing();

(I have try without the currency variable, and after this passing the ID of currency, and the both cases works good)

Only on the loop the princig() method do not retrieve the correct currency. I'm making a mistake in the loop that is not allowing the currency to recover properly?

Edited by bellafronte
I forgot a link
Link to comment
Share on other sites

  • 3 weeks later...
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated