简体   繁体   中英

How to add a custom product to cart in Prestashop 1.7?

I want to create a specific module to add custom product. In this way, I developed the next source code (it's working) :

/* Data */
$id_product = (int)Tools::getValue('id_product');
$id_customization = (int)Tools::getValue('id_customization'); // = 0
$price_product = (int)Tools::getValue('priceform'); // nouveau prix

/* Create the cart */
if(!$this->context->cart->id) {

    /* Detect the user logged */
    if($this->context->cookie->id_guest) {
        $guest = new Guest($this->context->cookie->id_guest);
        $this->context->cart->mobile_theme = $guest->mobile_theme;
    }
    $this->context->cart->add();

    /* Create the cookie */
    if($this->context->cart->id) {
        $this->context->cookie->id_cart = (int)$this->context->cart->id;
    }
}

/* Select the cart */
$cart = $this->context->cart;

/* Select the product */
$product = new Product($id_product, true, (int)($this->context->cookie->id_lang));
$id_product_attribute = Product::getDefaultAttribute($id_product);

/* Add to cart */
$cart->updateQty(1, $id_product, $id_product_attribute, $id_customization);

But I don't know, how to change the price, add the declination ($_POST['group[X]']) and add any more informations (string/varchar) ?

to change the price you can use :

$product->price = your price here;
$product->update();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM