Search code examples
prestashopprestashop-1.7prestashop-modules

PrestaShop add to cart from custom module


I ma working on PrestaShop module, where the user can select some parameters on front view.

I need to add product to cart, including custom price, and selected parameters.

How should I do this? I don't want to override deafult behavior, as not all products will use my component.

Any help would be awesome. Have a nice day, Bartek.


Solution

  • adding a product to a cart is pretty simple :

    First create an empty cart :

    $cart=new Cart();
    ... here you can set your cart properties id_address / id_carrier etc...
    $cart->add();
    

    Or, if you already have a cart to use just load the Cart object with its ID and perform :

    $cart->updateQty($quantity, $id_product, $id_product_attribute, false);
    $cart->update();
    

    You should repeate the updateQty() method for each product / qties you'd need to add to the cart.

    In order to customize product prices in cart you have to create product "specific prices" using the SpecificPrice object and bind them to that id_cart :

    $mySpecificPrice = new SpecificPrice();
    $mySpecificPrice->id_cart = $cart->id;
    ... add your discount / id_product here on specific price here ...
    $mySpecificPrice->add();
    

    Note that you can only set price discounts with SpecificPrice , surcharges are not allowed and will trigger an exeception.

    If you need to increase your prices without any core modification, you'll forced to generate an Order and edit the order prices afterward.