[Skip to main content](#vessel-content)

#   Struggling with enhanced custom quantity/price plugin

##

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago [\#340031](#340031) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  *[Struggling with enhanced custom quantity/price plugin](/forum/31-development/904099-struggling-with-enhanced-custom-quantity-price-plugin#340031 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.1 -- PHP version -- : 7.4  Hi,  Inspired by the &quot;HikaShopCustom Quantity plugin&quot; plugin, I'm working on an enhanced version. I am implementing an onBeforeCalculateProductPriceForQuantity event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:  - I have a product with several variations and associated prices. When I add the product to the cart, the onBeforeCalculateProductPriceForQuantity event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between &quot;variant&quot; and &quot;main&quot; product. This seems rather odd and I have no clue what to do at each of those event invocations.  - I also have a few custom text fields for the product. How can I access the input of those fields in the onBeforeCalculateProductPriceForQuantity event handler? On some invocations they appear to be in the POST data&#91;item&#93;, but not on all invocations.  The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?")* was created by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")*

-- HikaShop version -- : 4.4.5
-- Joomla version -- : 4.1
-- PHP version -- : 7.4

Hi,

Inspired by the "HikaShopCustom Quantity plugin" plugin, I'm working on an enhanced version. I am implementing an **onBeforeCalculateProductPriceForQuantity** event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:

- I have a product with several variations and associated prices. When I add the product to the cart, the **onBeforeCalculateProductPriceForQuantity** event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between "variant" and "main" product. This seems rather odd and I have no clue what to do at each of those event invocations.

- I also have a few custom text fields for the product. How can I access the input of those fields in the **onBeforeCalculateProductPriceForQuantity** event handler? On some invocations they appear to be in the POST data[item], but not on all invocations.

The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50LzkwNDA5OS1zdHJ1Z2dsaW5nLXdpdGgtZW5oYW5jZWQtY3VzdG9tLXF1YW50aXR5LXByaWNlLXBsdWdpbj90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago -  4 years 8 months ago [\#340034](#340034) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[Struggling with enhanced custom quantity/price plugin](/forum/31-development/904099-struggling-with-enhanced-custom-quantity-price-plugin#340034 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.1 -- PHP version -- : 7.4  Hi,  Inspired by the &quot;HikaShopCustom Quantity plugin&quot; plugin, I'm working on an enhanced version. I am implementing an onBeforeCalculateProductPriceForQuantity event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:  - I have a product with several variations and associated prices. When I add the product to the cart, the onBeforeCalculateProductPriceForQuantity event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between &quot;variant&quot; and &quot;main&quot; product. This seems rather odd and I have no clue what to do at each of those event invocations.  - I also have a few custom text fields for the product. How can I access the input of those fields in the onBeforeCalculateProductPriceForQuantity event handler? On some invocations they appear to be in the POST data&#91;item&#93;, but not on all invocations.  The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?")*

This post should have been in the Development section. I have no idea why it ended up here and I don't see a way to move it to the proepr place.

Last edit: 4 years 8 months ago by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50LzkwNDA5OS1zdHJ1Z2dsaW5nLXdpdGgtZW5oYW5jZWQtY3VzdG9tLXF1YW50aXR5LXByaWNlLXBsdWdpbj90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

4 years 8 months ago [\#340033](#340033) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Struggling with enhanced custom quantity/price plugin](/forum/31-development/904099-struggling-with-enhanced-custom-quantity-price-plugin#340033 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.1 -- PHP version -- : 7.4  Hi,  Inspired by the &quot;HikaShopCustom Quantity plugin&quot; plugin, I'm working on an enhanced version. I am implementing an onBeforeCalculateProductPriceForQuantity event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:  - I have a product with several variations and associated prices. When I add the product to the cart, the onBeforeCalculateProductPriceForQuantity event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between &quot;variant&quot; and &quot;main&quot; product. This seems rather odd and I have no clue what to do at each of those event invocations.  - I also have a few custom text fields for the product. How can I access the input of those fields in the onBeforeCalculateProductPriceForQuantity event handler? On some invocations they appear to be in the POST data&#91;item&#93;, but not on all invocations.  The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?")*

Hi,

The event onBeforeCalculateProductPriceForQuantity is not triggered when a product is added to the cart.
It is triggered for each product in the cart when the cart is loaded. So during an add to cart, you'll have it triggered for each product in the cart before the product is added to the cart (becaue to add a product to the cart, the cart must first be loaded), and then after the product is added, the cart will be reloaded.
So it's normal that you don't see the input of the custom fields in the POST, since the onBeforeCalculateProductPriceForQuantity event will be called everywhere where the cart is loaded, regardless of whether a product is being added or not.

In that event, the data of the product will be in $product.
So if you have a custom item field with the column name "my_field", you'll have the value entered by the user in $product-&gt;myfield
And to know if the current $product is a variant or a main product, you can check $product-&gt;product_type which can be either "main" or "variant".

The following user(s) said Thank You: [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50LzkwNDA5OS1zdHJ1Z2dsaW5nLXdpdGgtZW5oYW5jZWQtY3VzdG9tLXF1YW50aXR5LXByaWNlLXBsdWdpbj90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago -  4 years 8 months ago [\#340074](#340074) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[Struggling with enhanced custom quantity/price plugin](/forum/31-development/904099-struggling-with-enhanced-custom-quantity-price-plugin#340074 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.1 -- PHP version -- : 7.4  Hi,  Inspired by the &quot;HikaShopCustom Quantity plugin&quot; plugin, I'm working on an enhanced version. I am implementing an onBeforeCalculateProductPriceForQuantity event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:  - I have a product with several variations and associated prices. When I add the product to the cart, the onBeforeCalculateProductPriceForQuantity event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between &quot;variant&quot; and &quot;main&quot; product. This seems rather odd and I have no clue what to do at each of those event invocations.  - I also have a few custom text fields for the product. How can I access the input of those fields in the onBeforeCalculateProductPriceForQuantity event handler? On some invocations they appear to be in the POST data&#91;item&#93;, but not on all invocations.  The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?")*

Thanx for your reply. It helps, but I am still confused about many things. From your explanation I gather that I must take another approach. Not entirely different, because I will still need the **onBeforeCalculateProductPriceForQuantity** event to do custom calculations. But there are circumstances in which I simply have to replace a product with another. More specifically I must sometime replace a variant with the main product. I assume this can be done in an **onBeforeCartSave** event handler. However, there is something confusing about the cart data I receive in that handler. When the cart is empty, **onBeforeCartSave** receives an **element** in which attribute **cart\_products** is an array., containing a single product object with very limited attributes. It does not have a &#96;product_type&#96; attribute for instance. When adding exactly the same product to the cart, the contents of **$element-&gt;cart\_products** is quite different. Not only is it no longer an array but an object, but the products within have more attributes. They all have a &#96;product_type&#96; for instance.

Is there a specific reason for these inconsistencies in the HikaShop API? Do you have any advice how best to approach my challenge?

Last edit: 4 years 8 months ago by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50LzkwNDA5OS1zdHJ1Z2dsaW5nLXdpdGgtZW5oYW5jZWQtY3VzdG9tLXF1YW50aXR5LXByaWNlLXBsdWdpbj90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

4 years 7 months ago [\#340085](#340085) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Struggling with enhanced custom quantity/price plugin](/forum/31-development/904099-struggling-with-enhanced-custom-quantity-price-plugin#340085 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.1 -- PHP version -- : 7.4  Hi,  Inspired by the &quot;HikaShopCustom Quantity plugin&quot; plugin, I'm working on an enhanced version. I am implementing an onBeforeCalculateProductPriceForQuantity event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:  - I have a product with several variations and associated prices. When I add the product to the cart, the onBeforeCalculateProductPriceForQuantity event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between &quot;variant&quot; and &quot;main&quot; product. This seems rather odd and I have no clue what to do at each of those event invocations.  - I also have a few custom text fields for the product. How can I access the input of those fields in the onBeforeCalculateProductPriceForQuantity event handler? On some invocations they appear to be in the POST data&#91;item&#93;, but not on all invocations.  The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?")*

Hi,

onBeforeCartSave is called by the save function in class.cart
If I write such code somewhere: Code:

$cart = new stdClass(); $cartClass-&gt;save($cart);

it will save a new cart to the database with nothing in it.
And when the onBeforeCartSave event will be called by the save function, you will have an object with attributes added automatically to it, like the session_id or user_id, the cart_type, etc but you won't have an cart_products attribute at all since the code calling the save function won't provide it.
That doesn't mean that there is a problem with the code triggering onBeforeCartSave in the save function of class.cart, or even with the code calling the save function.
It's just that you can't trust that you'll always get the data in $element so you need to add extra checks for those cases.
For example, if you're missing the product_type and you need it, you can run such code: Code:

$productClass = hikashop_get('class.product'); $productData = $productData = $productClass-&gt;get($cart_product-&gt;product_id); echo $productData-&gt;product_type;

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50LzkwNDA5OS1zdHJ1Z2dsaW5nLXdpdGgtZW5oYW5jZWQtY3VzdG9tLXF1YW50aXR5LXByaWNlLXBsdWdpbj90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 7 months ago [\#340111](#340111) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[Struggling with enhanced custom quantity/price plugin](/forum/31-development/904099-struggling-with-enhanced-custom-quantity-price-plugin#340111 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.1 -- PHP version -- : 7.4  Hi,  Inspired by the &quot;HikaShopCustom Quantity plugin&quot; plugin, I'm working on an enhanced version. I am implementing an onBeforeCalculateProductPriceForQuantity event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:  - I have a product with several variations and associated prices. When I add the product to the cart, the onBeforeCalculateProductPriceForQuantity event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between &quot;variant&quot; and &quot;main&quot; product. This seems rather odd and I have no clue what to do at each of those event invocations.  - I also have a few custom text fields for the product. How can I access the input of those fields in the onBeforeCalculateProductPriceForQuantity event handler? On some invocations they appear to be in the POST data&#91;item&#93;, but not on all invocations.  The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?")*

Thanx again Nicholas. Bit by bit I'm getting closer ![:)](/media/kunena/emoticons/3.png ":)")

Just to be sure: is there no problem with me swapping one product for another in the **onBeforeCartSave** event handler? If not, is there a specific HikaShop way of doing that, some kind of API call, or should I simply manipulate the cart object and the product objects within it directly?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50LzkwNDA5OS1zdHJ1Z2dsaW5nLXdpdGgtZW5oYW5jZWQtY3VzdG9tLXF1YW50aXR5LXByaWNlLXBsdWdpbj90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

4 years 7 months ago [\#340114](#340114) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Struggling with enhanced custom quantity/price plugin](/forum/31-development/904099-struggling-with-enhanced-custom-quantity-price-plugin#340114 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.1 -- PHP version -- : 7.4  Hi,  Inspired by the &quot;HikaShopCustom Quantity plugin&quot; plugin, I'm working on an enhanced version. I am implementing an onBeforeCalculateProductPriceForQuantity event handler to adjust price and/or quantity values, but run into all sorts of problems. The two most important things I'm currently struggling with are:  - I have a product with several variations and associated prices. When I add the product to the cart, the onBeforeCalculateProductPriceForQuantity event is triggered multiple times, 6 to be exact, but each time with a different product, alternating between &quot;variant&quot; and &quot;main&quot; product. This seems rather odd and I have no clue what to do at each of those event invocations.  - I also have a few custom text fields for the product. How can I access the input of those fields in the onBeforeCalculateProductPriceForQuantity event handler? On some invocations they appear to be in the POST data&#91;item&#93;, but not on all invocations.  The general idea is to either use the price of the select variant or, if no variant is chosen, use the manual input from a width and length field to calculate the price. Do you have any suggestions how to approach this?")*

Hi,

You can directly manipulate $element, yes. And make sure you have the &amp; in the function parameters. That way, what you change in $element will be seen by the save function and HikaShop will save the products the way you want them.

The following user(s) said Thank You: [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzMxLWRldmVsb3BtZW50LzkwNDA5OS1zdHJ1Z2dsaW5nLXdpdGgtZW5oYW5jZWQtY3VzdG9tLXF1YW50aXR5LXByaWNlLXBsdWdpbj90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.081 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
