Guimou Posted July 29, 2013 Share Posted July 29, 2013 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. 0 Quote Link to comment Share on other sites More sharing options...
jclarke Posted July 30, 2013 Share Posted July 30, 2013 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. 0 Quote Link to comment Share on other sites More sharing options...
Guimou Posted July 31, 2013 Author Share Posted July 31, 2013 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"); 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.