Matteo Posted September 12 Share Posted September 12 Hello, I have developed a custom module that allows purchasing specific products individually. Every time a product from that group is added to the cart, I check for it and remove any other products already in the cart. This works fine for most products, but when I add a product with combinations (like 1 or 2 variations, so it's not too large), it leads to high memory consumption and possibly an infinite loop. I'm not sure why this is happening. Here's my code: public function hookActionCartSave() { $arrayMembership = array(151029, 150995, 150996, 150997, 150998, 150999, 151000, 151001, 151077); // get cart id if exists if(isset($this->context->cart)){ //$this->context->cart = $this->context->cart; //$objectCartCustom = $this->context->cart->getProducts(); $objCart = new Cart($this->context->cookie->id_cart, $this->context->cookie->id_lang); $has_membership_in_cart = false; foreach ($arrayMembership as $id_membership) { $check_qnt = $objCart->containsProduct($id_membership); if(isset($check_qnt['quantity']) && $check_qnt['quantity'] > 0){ $has_membership_in_cart = true; } } //echo $has_membership_in_cart;die(); //print_r($this->context->cart->containsProduct($arrayMembership[1]));die(); if($has_membership_in_cart){ $has_deleted = false; foreach ($objCart->getProducts() as $cart_row) { if(!in_array($cart_row["id_product"], $arrayMembership)){ $objCart->deleteProduct($cart_row["id_product"]); $has_deleted = true; } } return $e; } } } Link to comment Share on other sites More sharing options...
Matteo Posted September 12 Author Share Posted September 12 p.s. Oh, it's not $e ust because I removed the $e variable from the code. Link to comment Share on other sites More sharing options...
Prestashop Addict Posted September 13 Share Posted September 13 If you have a combination you need to pass the id attribute to method public function deleteProduct( $id_product, $id_product_attribute = 0, $id_customization = 0, $id_address_delivery = 0 1 Link to comment Share on other sites More sharing options...
ps8modules Posted September 13 Share Posted September 13 If you are doing some development of your own modules, you need to view the add, update, delete functions in the classes or controllers folder... and also look in the database to see how the indexes are set for the records. Then development will be simple. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now