Check if a product is in cart on the checkout

  • Posts: 42
  • Thank you received: 2
9 years 7 months ago #199473

-- HikaShop version -- : 2.4.0

Hi All!

I have added a custom add to cart link on my checkout that allows the client to donate R10 to a children at risk fund. At the moment I am using cookie values to check if the product was added, which then toggles the display of the donate link. This is sometimes producing weird results but works overall. I would prefer to be able to check if the product is in the cart, and then use that info to toggle the display link.

Thanks,
Bruce Atkinson

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

  • Posts: 13201
  • Thank you received: 2322
9 years 7 months ago #199492

Hi,

I think that the best way for you will be to use the function "onAfterCartProductsLoad()" in a plugin to see the content of the cart.

www.hikashop.com/support/support/documen...fterCartProductsLoad

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

  • Posts: 42
  • Thank you received: 2
9 years 7 months ago #199598

AH, and that would return cart object? How would I check the products in the cart, would it be contained in something like
$cart->products[]
?

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

  • Posts: 13201
  • Thank you received: 2322
9 years 7 months ago #199601

Hi,

Yes exactly, the products will be in $cart->products.

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

  • Posts: 42
  • Thank you received: 2
9 years 4 months ago #206922

For those interested, I wrote the below code and put it under the checkout->cart override. It checks to see if a custom product is in the cart - in this case product id 723 "R10 donation to Children at Risk Centres".

If not, it displays a link and notice that read the following:
"Click here to add R10.00 to your order for the Outreach Mission Fund for support of the Children at Risk Centres in Africa."
If the customer has clicked the donation link, the cart reads as follows:
"Thank you! Your donation to the Outreach Mission Fund is greatly appreciated."

Any comments to refactor/refine?

<?php
	$cartClass = hikashop_get('class.cart'); 
	$cart = $cartClass->loadFullCart(true);
    if ($cart) {
	    $numberOfProducts = count($cart->products);
	    for ($i=0; $i < $numberOfProducts; $i++) {
	    	$productName[$i] = $cart->products[$i]->product_name;
	    }
		if (in_array("R10 donation to Children at Risk Centres", $productName)) { ?>
		  <div class="alert alert-success"><strong>Thank you!</strong> Your donation to the Outreach Mission Fund is greatly appreciated.</div><?php
		}
		else { ?>
		  <div class="alert alert-warning"><strong><a href="/index.php?option=com_hikashop&ctrl=product&task=updatecart&product_id=723&quantity=1&checkout=2">Click here to add R10.00</a></strong> to your order for the Outreach Mission Fund for support of the Children at Risk Centres in Africa.</div><?php
		 }
	}
?>

The following user(s) said Thank You: Mohamed Thelji

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

  • Posts: 12953
  • Thank you received: 1778
9 years 4 months ago #206931

Hello,
Thanks for your feedback !
The only comment that I'll add will be that using the product ID instead of the product name to find your custom product will be better. It will enable you to freely change its product name (the product ID won't be something that your customers will see).

The following user(s) said Thank You: GSW00d

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

  • Posts: 42
  • Thank you received: 2
9 years 4 months ago #207029

For those interested, the updated code goes as follows:

<?php
$cartClass = hikashop_get('class.cart');
$cart      = $cartClass->loadFullCart(true);
if ($cart) {
    $numberOfProducts = count($cart->products);
    for ($i = 0; $i < $numberOfProducts; $i++) {
        $productID[$i] = $cart->products[$i]->product_id;
    }
    if (in_array("723", $productID)) {
?>
         <div class="alert alert-success"><strong>Thank you!</strong> Your donation to the Outreach Mission Fund is greatly appreciated.</div><?php
    } else {
?>
         <div class="alert alert-warning"><strong><a href="/index.php?option=com_hikashop&ctrl=product&task=updatecart&product_id=723&quantity=1&checkout=2">Click here to add R10.00</a></strong> to your order for the Outreach Mission Fund for support of the Children at Risk Centres in Africa.</div><?php
    }
}
?>

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

Time to create page: 0.096 seconds
Powered by Kunena Forum