Get the categories

  • Posts: 98
  • Thank you received: 4
  • Hikashop Business Hikashop Essential
3 weeks 6 days ago #366845

How would I get the categories of all the products in the basket with this Checkout API: onCheckoutStepDisplay($layoutName, &$html, &$view)

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

  • Posts: 83827
  • Thank you received: 13571
  • MODERATOR
3 weeks 6 days ago #366847

Hi,

In onCheckoutStepDisplay you can get the cart data with such code:

$cart = $view->checkoutHelper->getCart();
Once you have the $cart object, you can access the data of the products in it in $cart->products which is an array of product objects.
So, you want to loop on this array, get the product_id of each product and if the product_type of the product is equal to "variant", you want to use product_parent_id instead.
Then, you can use such code to load the id of the categories of a product:
$productClass = hikashop_get('class.product');
$categoryIds = $productClass->getCategories($product_id);
And then, if you need the data of the category, you can load it like this:
$categoryClass = hikashop_get('class.category');
$category = $categoryClass->get($category_id);

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

  • Posts: 98
  • Thank you received: 4
  • Hikashop Business Hikashop Essential
3 weeks 5 days ago #366868

Ok - a few more questions.

$productClass = hikashop_get('class.product');
$categoryIds = $productClass->getCategories($product_id);

How do I get the "hikashop_get" in a plugin ?? and $categoryIds - will this be an array ?

Last edit: 3 weeks 5 days ago by rbuelund.

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

  • Posts: 83827
  • Thank you received: 13571
  • MODERATOR
3 weeks 5 days ago #366870

Hi,

If you're developing a plugin implementing the event onCheckoutStepDisplay, I suppose the group of the plugin is "hikashop". In that case, before your plugin is loaded by HikaShop, HikaShop's main functions are already loaded, hikashop_get being one of them.
So you don't need anything to be able to use it.
If you're writing code for a Joomla website outside of a HikaShop plugin, and there is a possibility HikaShop itself is not loaded, you can load it by calling its main helper, as explained at the beginning of our "PHP code samples" documentation:
www.hikashop.com/support/documentation/6...umentation.html#code

$categoryIds will indeed be an array of category ids.

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

  • Posts: 98
  • Thank you received: 4
  • Hikashop Business Hikashop Essential
3 weeks 4 days ago #366879

Thank you, I got it working.

The following user(s) said Thank You: nicolas

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

  • Posts: 98
  • Thank you received: 4
  • Hikashop Business Hikashop Essential
1 week 6 days ago #367038

And now another question for this. I need to show alle the hikashop categorynames in a multiselectbox in the settings of the plugin. The custom dropdown field itself is no problem, but how do I populate it with the Hikashop categories with parent categories, sub category 1, sub category 2... etc and then if you select a parent all of the sub should also be selected?

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

  • Posts: 83827
  • Thank you received: 13571
  • MODERATOR
1 week 6 days ago #367040

Hi,

There is no ready made element for this exactly.

In HikaShop Business, you have the "Google Products" plugin which you can configure via the Joomla plugins manager.
If you edit it, you'll see an option "categories" where you can select categories so that the XML file generated by the plugin will only contain products from the selected categories.
Using that selector in your own plugin is easy. If you look at the XML of the Google Products plugin ( folder plugins/hikashop/google_products/ ), you'll see this option implemented like this:

<field name="categories" multiple="true" namebox_type="category" type="hikanamebox" default="" label="PRODUCT_CATEGORIES" description="GOOGLE_PRODUCTS_CATEGORIES" />
Joomla has a list of standard field types here: docs.joomla.org/standard_form_field_types
You can see that the type "hikanamebox" is not among them. It's a custom type HikaShop makes available so that you can use the advanced selectors available in the interface of HikaShop here and there.
Note also that to be able to use it, the plugin XML file also has this line:
<fields name="params" addfieldpath="/components/com_hikashop/fields">
The folder /components/com_hikashop/fields/ is where HikaShop has its custom field types, including the file hikanamebox.php hich defines that type. If you check that file, you'll see that it loads HikaShop and uses the type.namebox of HikaShop and follows the recommendations here: docs.joomla.org/Creating_a_custom_form_field_type

So, the quick solution is to add in your plugin XML the same addfieldpath attribute, and you'll be able to use the type "hikanamebox" with the "namebox_type" attribute set to "category" and the "multiple" attribute set to true. That way, you'll get the same category selector.
And, building on all of this, you can also have your own fields folder in your plugin, and have your own custom type file in it, so that your plugin can reference it in its XML. With your own type, you can run the MySQL queries you want, run the PHP code you want, and output the option with checkboxes, the auto select of the children radios with some javascript code, etc. It will be quite a lot more work than reusing the hikanamebox type, but maybe it's worth it.

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

  • Posts: 98
  • Thank you received: 4
  • Hikashop Business Hikashop Essential
1 week 6 days ago #367046

Ok thank you - that was easy to implement. But now I need the piece of code that checks each selected categori, so that if it is a parent, all of the sub, and sub sub, and so on.. categories musty also be added to my array? Do you have an idea for that ? I will have to implement that in my main plugin code.

Last edit: 1 week 6 days ago by rbuelund.

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

  • Posts: 83827
  • Thank you received: 13571
  • MODERATOR
1 week 6 days ago #367051

You can use this code to get the data of the children categories:

$categoryClass = hikashop_get('class.category');
$children = $categoryClass->getChildren($id, true);
The second parameter defines if you want only the direct sub categories (with false) or all the sub categories regardless of the depth level (with true).

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

Time to create page: 0.067 seconds
Powered by Kunena Forum