Adding to Cart using PHP

  • Posts: 10
  • Thank you received: 0
1 year 6 months ago #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 "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


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->data("english_language_courses", 9);
$Duration=$this->data("duration_number_of_weeks_english", 9);
if ($Duration < 1) {  $Duration=1; }
$class->update($CourseID, $Duration,true, true);

// below code does not updating cart with the additional fees

$IncludeRegFee=$this->data("include_registration_fee_english",9);
$IncludeAdmFee=$this->data("include_administration_fee_english",9);
$Duration=1;

if ($IncludeRegFee==1) {
    $RegFee = $this->data("english_registration_fee",9);
    $class->update($RegFee, $Duration,true, true);
}

if ($IncludeAdmFee==1) {
    $AdmFee = $this->data("english_administration_fee",9);
    $class->update($AdmFee, $Duration, true, true); 
}
exit();
CODE SNIPPET TWO
require_once(rtrim(JPATH_ADMINISTRATOR, DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php');
$class = hikashop::get('class.cart');
$cart_id = $class->getCurrentId();

$cart_type = hikaInput::get()->getString('cart_type', 'cart');
// $cart_id = hikaInput::get()->getInt($cart_type.'_id', 0);

if(empty($cart_id)) {
    //$cart_id = $class->getCurrentCartId($cart_type);	
    echo ("Cart_ID is empty");
}

$qty = 1;
$RegFee = $this->data("english_registration_fee",9);
$class->addProduct($cart_id,array('id' => $RegFee, 'qty' => $qty));

exit();

Last edit: 1 year 6 months ago by nicolas.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 6 months ago #351755

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:

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

In the second code, the issue is that you need to provide it an array of arrays:
$class->addProduct($cart_id,array(array('id' => $RegFee, 'qty' => $qty)));
That way, you can pass several products:
$class->addProduct($cart_id,array(array('id' => $RegFee, 'qty' => $qty), array('id' => $AdmFee, 'qty' => $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:
$cart = $class->get($cart_id);
var_dump($cart->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 or Create an account to join the conversation.

Time to create page: 0.055 seconds
Powered by Kunena Forum