Jump to content

Error on deleting products


Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...