Jump to content

Automatically add or remove item in cart


Guimou

Recommended Posts

Hi,

 

In our ordering process we charge setup fees for each group of 5 items (to choose between disk space only, VPS, Server). So 1 to 5 items => 1 setup fee, 6 to 10 => 2 setup fees, and so on...

As I did not find how to setup "per order" fees, only per products, I tried to code a hook when cart is updated (hook ShoppingCartValidateProductUpdate).

 

Problem is I don't find in the API a way to add (or remove) a single item to a cart, only add a complete separate order.

 

Am I missing something?

 

Thanks.

Link to comment
Share on other sites

The cart is stored in a session variable called $_SESSION["cart"] so you could add an item to the array in $_SESSION["cart"] , products are stored in $_SESSION['cart']['products']. You do a var_dump($_SESSION['cart']) in your hook to get a better idea of what is stored in the cart session.

Link to comment
Share on other sites

It worked perfectly, thanks a lot!

For reference here is the final code. I finally hooked to PreCalculateCartTotals so that it's done only when viewing the cart. Comments should explain everything. Of course it's very customized for my own need and should be updated when adding products. Maybe I'll update it later to find applicable products based on something else than pid. I also had to tweak some templates because I had to enable mutiple quantity for fees but I did not want people to change quantity themselves of course.

 

function add_install_fee($vars) {

//Find if install fees already there and remove them
$produtsSize=sizeOf($_SESSION['cart']['products']);
for($i = 0; $i < $produtsSize;$i++) {
if ($_SESSION['cart']['products'][$i]['pid'] == 4) {
	unset($_SESSION['cart']['products'][$i]);
}
}

//Count products where installation fees apply
$countFee = 0;
$validProducts = array(1,2,5);
//Nedd to update cart size as maybe we removed items previously
$produtsSize=sizeOf($_SESSION['cart']['products']);
for($i = 0; $i < $produtsSize;$i++) {
if (in_array($_SESSION['cart']['products'][$i]['pid'],$validProducts)) {
	$countFee++;
}
}

//Apply fees by group of 5 products
if ($countFee!=0) {
$feeArray = array(
	"pid" => "4",
	"qty" => floor(($countFee-1)/5+1),
	"domain" => "",
	"billingcycle" => NULL,
	"configoptions" => NULL,
	"customfields" => NULL,
	"addons" => NULL,
	"server" => "");
array_push($_SESSION['cart']['products'],$feeArray);
}

}

add_hook("PreCalculateCartTotals",1,"add_install_fee");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated