- Posts: 45
- Thank you received: 6
[PHP] Get product category
3 years 1 month ago #353988
by NTV
[PHP] Get product category was created by NTV
-- HikaShop version -- : 4.7.3
-- Joomla version -- : 4.3.3
-- PHP version -- : 7.4.33
-- Browser(s) name and version -- : Firefox 115.0.2 (64-bit)
Hi,
I have PHP code that runs everytime an order is created - onAfterOrderCreate($order). I want to access the value "category_parent_id". My code so far:
$productClass = hikashop_get('class.product');
$categoryClass = hikashop_get('class.category');
foreach ($order->cart->full_products as $product) {
$productAlt = $productClass->get($product->product_id);
$category = $categoryClass->get($productAlt->product_category_id);
echo $category->category_parent_id;
}
This fails, because the value in bold doesn't seem to exist. Yet if I read the HikaShop database model correctly, it should.
What am I doing wrong?
-- Joomla version -- : 4.3.3
-- PHP version -- : 7.4.33
-- Browser(s) name and version -- : Firefox 115.0.2 (64-bit)
Hi,
I have PHP code that runs everytime an order is created - onAfterOrderCreate($order). I want to access the value "category_parent_id". My code so far:
$productClass = hikashop_get('class.product');
$categoryClass = hikashop_get('class.category');
foreach ($order->cart->full_products as $product) {
$productAlt = $productClass->get($product->product_id);
$category = $categoryClass->get($productAlt->product_category_id);
echo $category->category_parent_id;
}
This fails, because the value in bold doesn't seem to exist. Yet if I read the HikaShop database model correctly, it should.
What am I doing wrong?
Please Log in or Create an account to join the conversation.
3 years 1 month ago #353999
by nicolas
Replied by nicolas on topic [PHP] Get product category
Hi,
No. The product_category_id column is in the hikashop_product_category table.
But the get function of class.product only loads the data in the hikashop_product table which doesn't contain that column.
So it's normal this doesn't work.
You can do it like this:
Note that one product can be linked to several categories.
No. The product_category_id column is in the hikashop_product_category table.
But the get function of class.product only loads the data in the hikashop_product table which doesn't contain that column.
So it's normal this doesn't work.
You can do it like this:
Code:
$productClass = hikashop_get('class.product');
foreach ($order->cart->full_products as $product) {
$categories = $productClass->getCategories($product->product_id);
foreach($categories as $category_id) {
echo $category_id;
}
}
The following user(s) said Thank You: NTV
Please Log in or Create an account to join the conversation.
3 years 1 month ago #354080
by NTV
Replied by NTV on topic [PHP] Get product category
Hi nicolas,
I got it working like a charm. All I needed to add to your code is getting the actual category object via this line of code:
$category = $categoryClass->get($category_id);
Then I was able to access the parent category:
echo $category->category_parent_id;
Case Closed!
I got it working like a charm. All I needed to add to your code is getting the actual category object via this line of code:
$category = $categoryClass->get($category_id);
Then I was able to access the parent category:
echo $category->category_parent_id;
Case Closed!
The following user(s) said Thank You: nicolas
Please Log in or Create an account to join the conversation.
Time to create page: 0.172 seconds