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

#   Adding to Cart using PHP

##

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

Less More

- Posts: 11
- Thank you received: 0
-
-

3 years 6 months ago -  3 years 6 months ago [\#351751](#351751) by [pqas](/forum/user/32448-pqas "View pqas's Profile")  *[Adding to Cart using PHP](/forum/27-checkout/906400-adding-to-cart-using-php#351751 "-- url of the page with the problem -- : localhost -- HikaShop version -- : 4.7.2 -- Joomla version -- : 4.2.9 -- PHP version -- : 8.1.12  I'm using Chronoforms v7 form to collect user choice of product and quantity to add to the Hikashop cart. There are two additional charges to include in the cart with the main item. Currently, these additional charges are optional (rely on the customer to include either one, both, or none in the cart).  The problem is that only the main item gets added to the cart when the user clicks on the custom &quot;Add to Cart&quot; button which is achieved via a PHP script (see below).  I guess, I may be wrong but it seems the 'update' function only works in this instance when the cart is empty. So I tried to see if an 'addProduct' function will do (see below snippet' but can't seem to get that to do anything. With a very limited Php knowledge and relying on code snippets from previous forum posts, I'm sure I'm doing something wrong.  Please have a look at both code snippets and point me in the right direction.  CODE SNIPPET ONE -----  CODE SNIPPET TWO -----")* was created by *[pqas](/forum/user/32448-pqas "View pqas's Profile")*

-- url of the page with the problem -- : localhost
-- HikaShop version -- : 4.7.2
-- Joomla version -- : 4.2.9
-- PHP version -- : 8.1.12

I'm using Chronoforms v7 form to collect user choice of product and quantity to add to the Hikashop cart. There are two additional charges to include in the cart with the main item. Currently, these additional charges are optional (rely on the customer to include either one, both, or none in the cart).

The problem is that only the main item gets added to the cart when the user clicks on the custom "Add to Cart" button which is achieved via a PHP script (see below).

I guess, I may be wrong but it seems the 'update' function only works in this instance when the cart is empty. So I tried to see if an 'addProduct' function will do (see below snippet' but can't seem to get that to do anything. With a very limited Php knowledge and relying on code snippets from previous forum posts, I'm sure I'm doing something wrong.

Please have a look at both code snippets and point me in the right direction.

CODE SNIPPET ONE

---

Code:

require_once(rtrim(JPATH_ADMINISTRATOR, DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php'); $class = hikashop::get('class.cart'); //below code updates the cart as intended $CourseID=$this-&gt;data("english_language_courses", 9); $Duration=$this-&gt;data("duration_number_of_weeks_english", 9); if ($Duration &lt; 1) { $Duration=1; } $class-&gt;update($CourseID, $Duration,true, true); // below code does not updating cart with the additional fees $IncludeRegFee=$this-&gt;data("include_registration_fee_english",9); $IncludeAdmFee=$this-&gt;data("include_administration_fee_english",9); $Duration=1; if ($IncludeRegFee==1) { $RegFee = $this-&gt;data("english_registration_fee",9); $class-&gt;update($RegFee, $Duration,true, true); } if ($IncludeAdmFee==1) { $AdmFee = $this-&gt;data("english_administration_fee",9); $class-&gt;update($AdmFee, $Duration, true, true); } exit();

CODE SNIPPET TWO

---

Code:

require_once(rtrim(JPATH_ADMINISTRATOR, DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php'); $class = hikashop::get('class.cart'); $cart_id = $class-&gt;getCurrentId(); $cart_type = hikaInput::get()-&gt;getString('cart_type', 'cart'); // $cart_id = hikaInput::get()-&gt;getInt($cart_type.'_id', 0); if(empty($cart_id)) { //$cart_id = $class-&gt;getCurrentCartId($cart_type); echo ("Cart_ID is empty"); } $qty = 1; $RegFee = $this-&gt;data("english_registration_fee",9); $class-&gt;addProduct($cart_id,array('id' =&gt; $RegFee, 'qty' =&gt; $qty)); exit();

Last edit: 3 years 6 months ago by [nicolas](/forum/user/63-nicolas "View nicolas's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI3LWNoZWNrb3V0LzkwNjQwMC1hZGRpbmctdG8tY2FydC11c2luZy1waHA/dG1wbD1tYXJrZG93bg==) 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: 86587
- Thank you received: 14325
-
-

3 years 6 months ago [\#351755](#351755) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Adding to Cart using PHP](/forum/27-checkout/906400-adding-to-cart-using-php#351755 "-- url of the page with the problem -- : localhost -- HikaShop version -- : 4.7.2 -- Joomla version -- : 4.2.9 -- PHP version -- : 8.1.12  I'm using Chronoforms v7 form to collect user choice of product and quantity to add to the Hikashop cart. There are two additional charges to include in the cart with the main item. Currently, these additional charges are optional (rely on the customer to include either one, both, or none in the cart).  The problem is that only the main item gets added to the cart when the user clicks on the custom &quot;Add to Cart&quot; button which is achieved via a PHP script (see below).  I guess, I may be wrong but it seems the 'update' function only works in this instance when the cart is empty. So I tried to see if an 'addProduct' function will do (see below snippet' but can't seem to get that to do anything. With a very limited Php knowledge and relying on code snippets from previous forum posts, I'm sure I'm doing something wrong.  Please have a look at both code snippets and point me in the right direction.  CODE SNIPPET ONE -----  CODE SNIPPET TWO -----")*

Hi,

For the first code, I've never done several calls to update in one PHP script. When there are several products to add, HikaShop will add them all at once.
So maybe that's the reason the other products are not added ?
You could do it like that: Code:

$data = array( $CourseID =&gt; $Duration ); if($IncludeRegFee==1) { $RegFee = $this-&gt;data("english_registration_fee",9); $data[$RegFee] = $Duration; } $class-&gt;update($data, $Duration, true, true);

In the second code, the issue is that you need to provide it an array of arrays: Code:

$class-&gt;addProduct($cart_id,array(array('id' =&gt; $RegFee, 'qty' =&gt; $qty)));

That way, you can pass several products: Code:

$class-&gt;addProduct($cart_id,array(array('id' =&gt; $RegFee, 'qty' =&gt; $qty), array('id' =&gt; $AdmFee, 'qty' =&gt; $qty)));

Also, I'm thinking the issue might be something else completely and you don't know about it because you don't check the messages provided by the system.
if you do: Code:

$cart = $class-&gt;get($cart_id); var_dump($cart-&gt;messages);

before the exit, you'll get the potential error messages.
One possibility I can think of is that you didn't set any price in your options, and you have the "Display add to cart button for free products" setting turned off in the Hikashop configuration. In that case, the system won't allow adding to the cart products without a price set.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI3LWNoZWNrb3V0LzkwNjQwMC1hZGRpbmctdG8tY2FydC11c2luZy1waHA/dG1wbD1tYXJrZG93bg==) 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.060 seconds

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