Prestashop, check which action is triggered on a cart (add / update), from hookActionCartSave

Viewed 691

I am creating a Prestashop custom hook to a client to send information to an API when a product is added to a cart. I am using hookActionCartSave but this hook is fired when the cart is updated or a new product is added. So my question is, How can I check which action is triggered and execute my code only when a new product is added. You will find my code bellow. Thank you for your help.

public function hookActionCartSave($params)
{
    if (!Validate::isLoadedObject($this->context->cart) || !Validate::isLoadedObject($params['cart'])) {
        return false;
    } else {
        $cart = $params['cart'] ; 
        $products = $cart->getProducts();
        $product_count = 0;
        //get products details
        $products_details = array();
        foreach($products as $item){
            $products_details[] = array(
                  "name" => $item->product_name,
                  "product_id" => $item->product_id,
                  "price" => $item->total_price_tax_incl,
                  "quantity" => $item->product_quantity
                );
            $product_count = $product_count + $item->product_quantity ;
        }
        $productsJsonData = json_encode($products_details);

        $last_cart_product = $cart->getLastProduct();
        $last_added_product = new Product($last_cart_product['id_product']);

        // send data to my API 

    }
1 Answers

I think you can use the actionObjectCartAddAfter hook to do what you want.

If you want more information about this hook, you can see it in /classes/ObjectModel.php file on the add function.

Related