-- HikaShop version -- : 4.6.1
-- Joomla version -- : 4.1.5
-- PHP version -- : 8.0
-- Browser(s) name and version -- : FireFox 100.0.2 (64-bit)
-- Error-message(debug-mod must be tuned on) -- : trim(): Argument #1 ($string) must be of type string, array given
Hi Nicolas,
Before explaining the problem I encounter, let me first explain what I am trying to do.
In the onBeforeCalculateProductPriceForQuantity() event handler of a system plugin, I try to construct a product object, including all related information. I need such an object, including any associated custom fields and other data, to do some non standard price calculations. The way I try to obtain that product object is as follows:
- Get a reference to the product class: $productClass = hikashop_get('class.product')
- Load the product using the product_id in the method parameter: $productClass->getProducts([$product->product_id])
- Load additional product data using the now available product(s) in $productClass: $productClass->loadProductsListingData([$productClass->products[(string) $product->product_id]], )
- Load product options in a similar fashion: $productClass->loadProductOptions(([$productClass->products[(string) $product->product_id]])
I feel very uncomfortable with the way I managed to get what I need, but in the old, complicated and confusing, non-modular codebase that is HikaShop is, I saw no other way. It gave me the product object I need though and everything seems to work just fine, up to the point that I go to the checkout page and change the ordered amount of a product. This also triggers my onBeforeCalculateProductPriceForQuantity() method, which should be fine, but apparently isn't. This is what I see happening:
- When changing the product quantity, an AJAX call is made which eventually calls hikashopCartClass::loadFieldsForProducts(), which in turn calls hikashopFieldClass::getData().
- That getData() method fills a static variable with product data. Items in that data collection contain an attribute field_categories, which contains a comma separated string of category id's.
- Somewhat later, my onBeforeCalculateProductPriceForQuantity() event handler is called.
- The call I make to hikashopProductClass::loadProductsListingData() (see 2. above) in turn calls hikashopProductClass::loadCustomItemFieldsForProductsListing().
That last function call is where the problem starts. For some reason, it turns the comma separated string of category id's mentioned above into an array. Then, when hikashopCartClass::loadFieldsForProducts() is called a second time around as part of the same AJAX call, an error occurs because an attempt is made to trim() an array, which obviously is not allowed. This results in the following message in the shopping cart area:
trim(): Argument #1 ($string) must be of type string, array given.
What must I do to resolve this?