obtain discount/price information for a product in an external PHP file

  • Posts: 32
  • Thank you received: 0
6 years 4 months ago #284191

-- HikaShop version -- : 2.2.3
-- Joomla version -- : 2.5.18
-- PHP version -- : 5.3
-- Browser(s) name and version -- : Firefox
-- Error-message(debug-mod must be tuned on) -- : I only obtain the product information - not the price/discount information

Hi,

How can i programmatically obtain all of the data for a product (including the discount and prices for that product) in an external php file. When i use the following code, i can obtain the product information but unfortunately i cannot obtain the price/discount information (which i really need). All i can obtain is general information about the product...

define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..'));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php');

$productClass = hikashop_get('class.product');
$product=$productClass->get(4893);

Thanks in advance

Please Log in or Create an account to join the conversation.

  • Posts: 81539
  • Thank you received: 13069
  • MODERATOR
6 years 4 months ago #284197

Hi,

You can add:

$currencyClass = hikashop_get('class.currency');
$config = hikashop_config();
$ids = array($product->product_id);
$currencyClass->getPrices($product, $ids, hikashop_getCurrency(), $config->get('main_currency'), hikashop_getZone(), $config->get('discount_before_tax'));
You will then have the prices in $product->prices

Please Log in or Create an account to join the conversation.

  • Posts: 1027
  • Thank you received: 11
  • Hikashop Business
3 years 5 months ago #326065

Hello!
I tried the code and it works for tha main product. But when there are subproducts I get wrong calculation. Does this work for subproducts too? Or only for the main products?

Here is an example of price and discount of a product object

[prices] => Array
        (
            [0] => stdClass Object
                (
                    [price_id] => 186
                    [price_currency_id] => 1
                    [price_product_id] => 870
                    [price_value] => 69.84
                    [price_min_quantity] => 0
                    [price_access] => all
                    [price_users] => 
                    [price_site_id] => 
                    [price_start_date] => 0
                    [price_end_date] => 0
                    [price_value_with_tax] => 69.84
                    [taxes_added] => 1
                    [taxes] => Array
                        (
                        )

                    [price_value_without_discount] => 87.30000
                    [price_value_without_discount_with_tax] => 87.30000
                    [taxes_without_discount] => Array
                        (
                        )

                )

        )

    [discount] => stdClass Object
        (
            [discount_id] => 6
            [discount_type] => discount
            [discount_start] => 0
            [discount_end] => 0
            [discount_flat_amount] => 0.00000
            [discount_percent_amount] => 20.000
            [discount_minimum_order] => 0.00000
            [discount_quota] => 0
            [discount_used_times] => 0
            [discount_code] => BLACK FRIDAY
            [discount_published] => 1
            [discount_product_id] => 
            [discount_category_id] => Array
                (
                    [0] => 171
                    [1] => 24
                )

            [discount_zone_id] => 
            [discount_currency_id] => 1
            [discount_category_childs] => 1
            [discount_auto_load] => 0
            [discount_access] => all
            [discount_tax_id] => 0
            [discount_minimum_products] => 0
            [discount_quota_per_user] => 0
            [discount_coupon_nodoubling] => 0
            [discount_coupon_product_only] => 0
            [discount_affiliate] => -1
            [discount_site_id] => 
            [discount_user_id] => 
            [discount_tax] => 0
            [discount_shipping_percent] => 0.000
        )
At the front end i have this price

At the back end price of the same product

Tthis data is from a variant of a product

Best Regards!

Attachments:
Last edit: 3 years 5 months ago by verzevoul. Reason: To add more details

Please Log in or Create an account to join the conversation.

  • Posts: 81539
  • Thank you received: 13069
  • MODERATOR
3 years 5 months ago #326076

Hi,

By "subproducts" I suppose that you're talking about variants, is that right ?
This code could work for some variants. It depends on how the variants and the main product is configured, and if you have discounts, taxes, etc.
This code is not complete enough to take into account all the cases.
The proper way to do it would be to load the main product and the variant data, feed them both in $ids and add the variant in the variants array of the product: $product->variants = array($variant);


Something like that:

$currencyClass = hikashop_get('class.currency');
$config = hikashop_config();
$parent = $productClass->get(($product->product_parent_id);
$ids = array($product->product_id, $parent->product_id);
$parent->variants = array($product);
$currencyClass->getPrices($parent, $ids, hikashop_getCurrency(), $config->get('main_currency'), hikashop_getZone(), $config->get('discount_before_tax'));
And then you'll get the prices of the main product in $parent->prices and the prices of the variant in $parent->variants[0]->prices and if the variant doesn't have any prices configured but the main product has prices, you need to use the prices from the parent.

Please Log in or Create an account to join the conversation.

  • Posts: 1027
  • Thank you received: 11
  • Hikashop Business
3 years 5 months ago #326116

Hi,
By subproduct yes i mean Variants
the problem still exists .

parent stdClass Object
( 
...
[variants] => Array
        (
            [0] => stdClass Object
                ( ...
                  [prices] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [price_id] => 186
                                    [price_currency_id] => 1
                                    [price_product_id] => 870
                                    [price_value] => 69.84
                                    [price_min_quantity] => 0
                                    [price_access] => all
                                    [price_users] => 
                                    [price_site_id] => 
                                    [price_start_date] => 0
                                    [price_end_date] => 0
                                    [price_value_with_tax] => 69.84
                                    [taxes_added] => 1
                                    [taxes] => Array
                                        (
                                        )

                                    [price_value_without_discount] => 87.30000
                                    [price_value_without_discount_with_tax] => 87.30000
                                    [taxes_without_discount] => Array
                                        (
                                        )

                                )

                        )

                    [discount] => stdClass Object
                        (
                            [discount_id] => 6
                            [discount_type] => discount
                            [discount_start] => 0
                            [discount_end] => 0
                            [discount_flat_amount] => 0.00000
                            [discount_percent_amount] => 20.000
                            [discount_minimum_order] => 0.00000
                            [discount_quota] => 0
                            [discount_used_times] => 0
                            [discount_code] => BLACK FRIDAY
                            [discount_published] => 1
                            [discount_product_id] => 
                            [discount_category_id] => Array
                                (
                                    [0] => 171
                                    [1] => 24
                                )

                            [discount_zone_id] => 
                            [discount_currency_id] => 1
                            [discount_category_childs] => 1
                            [discount_auto_load] => 0
                            [discount_access] => all
                            [discount_tax_id] => 0
                            [discount_minimum_products] => 0
                            [discount_quota_per_user] => 0
                            [discount_coupon_nodoubling] => 0
                            [discount_coupon_product_only] => 0
                            [discount_affiliate] => -1
                            [discount_site_id] => 
                            [discount_user_id] => 
                            [discount_tax] => 0
                            [discount_shipping_percent] => 0.000
                        )

                )
)
if you see the price_value its ok but price_value_with_tax is the same with price_value insted of 86.60
the tax amout missing 24% of the price
Thanks in advance

Please Log in or Create an account to join the conversation.

  • Posts: 81539
  • Thank you received: 13069
  • MODERATOR
3 years 5 months ago #326130

Hi,

That would indicate that the product_tax_id of the main product is not taken into account for the calculations of the taxes for the variant I suppose.
However, I'm not able to reproduce the problem on my end.
I just tried that code:

$productClass = hikashop_get('class.product');
$currencyClass = hikashop_get('class.currency');
$product=$productClass->get(64);
$config = hikashop_config();
$parent = $productClass->get($product->product_parent_id);
$ids = array($product->product_id, $parent->product_id);
$parent->variants = array($product);
$currencyClass->getPrices($parent, $ids, hikashop_getCurrency(), $config->get('main_currency'), hikashop_getZone(), $config->get('discount_before_tax'));
var_dump($parent);
where 64 is the id of a variant of the "star stilleto" product of the sample data you get when you install HikaShop. And I do get the taxes applied on the price of the variant:
object(stdClass)#928 (70) {
  ["product_id"]=>
  string(1) "2"
  ...
  ["variants"]=>
  array(1) {
    [0]=>
    object(stdClass)#1075 (69) {
      ["product_id"]=>
      string(2) "64"
      ["product_parent_id"]=>
      string(1) "2"
      ...
      ["prices"]=>
      array(1) {
        [0]=>
        object(stdClass)#1093 (16) {
          ["price_id"]=>
          string(2) "63"
          ["price_currency_id"]=>
          string(1) "1"
          ["price_product_id"]=>
          string(2) "64"
          ["price_value"]=>
          float(55)
          ["price_min_quantity"]=>
          string(1) "0"
          ["price_access"]=>
          string(3) "all"
          ["price_users"]=>
          string(0) ""
          ["price_site_id"]=>
          string(0) ""
          ["price_start_date"]=>
          string(1) "0"
          ["price_end_date"]=>
          string(1) "0"
          ["price_value_with_tax"]=>
          float(60.5)
          ["taxes_added"]=>
          bool(true)
          ["taxes"]=>
          array(1) {
            ["0000_00000002"]=>
            object(stdClass)#1098 (19) {
              ["taxation_id"]=>
              string(1) "2"
              ["zone_namekey"]=>
              string(0) ""
              ["category_namekey"]=>
              string(11) "default_tax"
              ["tax_namekey"]=>
              string(2) "10"
              ["taxation_published"]=>
              string(1) "1"
              ["taxation_type"]=>
              string(0) ""
              ["taxation_access"]=>
              string(3) "all"
              ["taxation_cumulative"]=>
              string(1) "0"
              ["taxation_post_code"]=>
              string(0) ""
              ["taxation_date_start"]=>
              string(1) "0"
              ["taxation_date_end"]=>
              string(1) "0"
              ["taxation_internal_code"]=>
              string(0) ""
              ["taxation_note"]=>
              string(0) ""
              ["taxation_site_id"]=>
              string(0) ""
              ["taxation_ordering"]=>
              string(1) "0"
              ["tax_rate"]=>
              string(7) "0.10000"
              ["tax_ratio"]=>
              int(1)
              ["tax_amount"]=>
              float(5.5)
              ["amount"]=>
              float(55)
            }
          }
          ["price_value_without_discount"]=>
          float(110)
          ["price_value_without_discount_with_tax"]=>
          float(121)
          ["taxes_without_discount"]=>
          array(1) {
            ["0000_00000002"]=>
            object(stdClass)#1085 (19) {
              ["taxation_id"]=>
              string(1) "2"
              ["zone_namekey"]=>
              string(0) ""
              ["category_namekey"]=>
              string(11) "default_tax"
              ["tax_namekey"]=>
              string(2) "10"
              ["taxation_published"]=>
              string(1) "1"
              ["taxation_type"]=>
              string(0) ""
              ["taxation_access"]=>
              string(3) "all"
              ["taxation_cumulative"]=>
              string(1) "0"
              ["taxation_post_code"]=>
              string(0) ""
              ["taxation_date_start"]=>
              string(1) "0"
              ["taxation_date_end"]=>
              string(1) "0"
              ["taxation_internal_code"]=>
              string(0) ""
              ["taxation_note"]=>
              string(0) ""
              ["taxation_site_id"]=>
              string(0) ""
              ["taxation_ordering"]=>
              string(1) "0"
              ["tax_rate"]=>
              string(7) "0.10000"
              ["tax_ratio"]=>
              int(1)
              ["tax_amount"]=>
              float(11)
              ["amount"]=>
              float(110)
            }
          }
        }
      }
      ["discount"]=>
      object(stdClass)#1097 (29) {
        ["discount_id"]=>
        string(1) "2"
        ["discount_type"]=>
        string(8) "discount"
        ["discount_start"]=>
        string(1) "0"
        ["discount_end"]=>
        string(1) "0"
        ["discount_flat_amount"]=>
        string(7) "0.00000"
        ["discount_percent_amount"]=>
        string(6) "50.000"
        ["discount_minimum_order"]=>
        string(7) "0.00000"
        ["discount_quota"]=>
        string(1) "0"
        ["discount_used_times"]=>
        string(1) "0"
        ["discount_code"]=>
        string(7) "stileto"
        ["discount_published"]=>
        string(1) "1"
        ["discount_product_id"]=>
        array(4) {
          [0]=>
          string(0) ""
          [1]=>
          string(2) "64"
          [2]=>
          string(2) "65"
          [3]=>
          string(0) ""
        }
        ["discount_category_id"]=>
        string(0) ""
        ["discount_zone_id"]=>
        string(0) ""
        ["discount_currency_id"]=>
        string(1) "1"
        ["discount_category_childs"]=>
        string(1) "0"
        ["discount_auto_load"]=>
        string(1) "0"
        ["discount_access"]=>
        string(3) "all"
        ["discount_tax_id"]=>
        string(1) "0"
        ["discount_minimum_products"]=>
        string(1) "0"
        ["discount_quota_per_user"]=>
        string(1) "0"
        ["discount_coupon_nodoubling"]=>
        string(1) "0"
        ["discount_coupon_product_only"]=>
        string(1) "0"
        ["discount_affiliate"]=>
        string(2) "-1"
        ["discount_site_id"]=>
        string(0) ""
        ["discount_user_id"]=>
        string(0) ""
        ["discount_tax"]=>
        string(1) "0"
        ["discount_shipping_percent"]=>
        string(5) "0.000"
        ["discount_target_vendor"]=>
        string(1) "0"
      }
    }
  }
  ["prices"]=>
  array(1) {
    [0]=>
    object(stdClass)#1090 (16) {
      ["price_id"]=>
      string(3) "142"
      ["price_currency_id"]=>
      string(1) "1"
      ["price_product_id"]=>
      string(1) "2"
      ["price_value"]=>
      float(111.555)
      ["price_min_quantity"]=>
      string(1) "0"
      ["price_access"]=>
      string(3) "all"
      ["price_users"]=>
      string(0) ""
      ["price_site_id"]=>
      string(0) ""
      ["price_start_date"]=>
      string(1) "0"
      ["price_end_date"]=>
      string(1) "0"
      ["price_value_with_tax"]=>
      float(122.7105)
      ["taxes_added"]=>
      bool(true)
      ["taxes"]=>
      array(1) {
        ["0000_00000002"]=>
        object(stdClass)#1099 (19) {
          ["taxation_id"]=>
          string(1) "2"
          ["zone_namekey"]=>
          string(0) ""
          ["category_namekey"]=>
          string(11) "default_tax"
          ["tax_namekey"]=>
          string(2) "10"
          ["taxation_published"]=>
          string(1) "1"
          ["taxation_type"]=>
          string(0) ""
          ["taxation_access"]=>
          string(3) "all"
          ["taxation_cumulative"]=>
          string(1) "0"
          ["taxation_post_code"]=>
          string(0) ""
          ["taxation_date_start"]=>
          string(1) "0"
          ["taxation_date_end"]=>
          string(1) "0"
          ["taxation_internal_code"]=>
          string(0) ""
          ["taxation_note"]=>
          string(0) ""
          ["taxation_site_id"]=>
          string(0) ""
          ["taxation_ordering"]=>
          string(1) "0"
          ["tax_rate"]=>
          string(7) "0.10000"
          ["tax_ratio"]=>
          int(1)
          ["tax_amount"]=>
          float(11.1555)
          ["amount"]=>
          float(111.555)
        }
      }
      ["price_value_without_discount"]=>
      float(123.95)
      ["price_value_without_discount_with_tax"]=>
      float(136.345)
      ["taxes_without_discount"]=>
      array(1) {
        ["0000_00000002"]=>
        object(stdClass)#1094 (19) {
          ["taxation_id"]=>
          string(1) "2"
          ["zone_namekey"]=>
          string(0) ""
          ["category_namekey"]=>
          string(11) "default_tax"
          ["tax_namekey"]=>
          string(2) "10"
          ["taxation_published"]=>
          string(1) "1"
          ["taxation_type"]=>
          string(0) ""
          ["taxation_access"]=>
          string(3) "all"
          ["taxation_cumulative"]=>
          string(1) "0"
          ["taxation_post_code"]=>
          string(0) ""
          ["taxation_date_start"]=>
          string(1) "0"
          ["taxation_date_end"]=>
          string(1) "0"
          ["taxation_internal_code"]=>
          string(0) ""
          ["taxation_note"]=>
          string(0) ""
          ["taxation_site_id"]=>
          string(0) ""
          ["taxation_ordering"]=>
          string(1) "0"
          ["tax_rate"]=>
          string(7) "0.10000"
          ["tax_ratio"]=>
          int(1)
          ["tax_amount"]=>
          float(12.395)
          ["amount"]=>
          float(123.95)
        }
      }
    }
  }
  ["discount"]=>
  object(stdClass)#1096 (29) {
    ["discount_id"]=>
    string(1) "4"
    ["discount_type"]=>
    string(8) "discount"
    ["discount_start"]=>
    string(1) "0"
    ["discount_end"]=>
    string(1) "0"
    ["discount_flat_amount"]=>
    string(7) "0.00000"
    ["discount_percent_amount"]=>
    string(6) "10.000"
    ["discount_minimum_order"]=>
    string(7) "0.00000"
    ["discount_quota"]=>
    string(1) "0"
    ["discount_used_times"]=>
    string(2) "12"
    ["discount_code"]=>
    string(13) "test-discount"
    ["discount_published"]=>
    string(1) "1"
    ["discount_product_id"]=>
    string(0) ""
    ["discount_category_id"]=>
    string(0) ""
    ["discount_zone_id"]=>
    string(0) ""
    ["discount_currency_id"]=>
    string(1) "1"
    ["discount_category_childs"]=>
    string(1) "0"
    ["discount_auto_load"]=>
    string(1) "0"
    ["discount_access"]=>
    string(3) "all"
    ["discount_tax_id"]=>
    string(1) "0"
    ["discount_minimum_products"]=>
    string(1) "0"
    ["discount_quota_per_user"]=>
    string(1) "0"
    ["discount_coupon_nodoubling"]=>
    string(1) "0"
    ["discount_coupon_product_only"]=>
    string(1) "0"
    ["discount_affiliate"]=>
    string(2) "-1"
    ["discount_site_id"]=>
    string(0) ""
    ["discount_user_id"]=>
    string(0) ""
    ["discount_tax"]=>
    string(1) "0"
    ["discount_shipping_percent"]=>
    string(5) "0.000"
    ["discount_target_vendor"]=>
    string(1) "0"
  }
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.089 seconds
Powered by Kunena Forum