Php 如何将新产品包添加到购物车?

Php 如何将新产品包添加到购物车?,php,prestashop,prestashop-1.6,Php,Prestashop,Prestashop 1.6,我想通过编程将包装产品添加到购物车,我在ModuleFrontController中的操作如下所示: public function initPack(){ $product = new Pack(); $product->tax_name = 'tva1'; $product->tax_rate = '10%'; $product->id_manufacturer = 1; $product->id_supplier = 1;

我想通过编程将包装产品添加到购物车,我在
ModuleFrontController
中的操作如下所示:

public function initPack(){
    $product = new Pack();

    $product->tax_name = 'tva1';
    $product->tax_rate = '10%';
    $product->id_manufacturer = 1;
    $product->id_supplier = 1;
    $product->id_category_default = 1;
    $product->id_shop_default = 1;
    $product->manufacturer_name = 'rrr';
    $product->supplier_name = 'rrr';
    $product->name =  array((int)Configuration::get('PS_LANG_DEFAULT') =>  'coffret-4');;
    $product->description = 'description 1';
    $product->description_short = 'description short';
    $product->quantity = 20;
    $product->on_sale = false;
    $product->online_only = true;
    $product->reference = '1234567889';
    $product->ean13     = 12233;
    $product->upc = 12233;
    $product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') =>  'coffret-4');
    $product->meta_description = 'fff';
    $product->meta_keywords = 'ff';
    $product->meta_title  = 'fff';
    $product->visibility= 'both';

    $product->date_add = date("j, n, Y");
    $product->location = '';
    $product->category = 16;
    $product->text_fields = 1;
    $product->condition = 'new';
    $product->on_sale = true;

    if($product->save())
        $product->add();
    else{
        echo 'erooooooor';
        die();
    }

    Pack::addItem($product->id, 1, 1);
    Pack::addItem($product->id, 3, 1);

    $attribute = $product->addProductAttribute(12, 234, 2344, 12, 20, 1, 222,
    $id_supplier = null, 222, 1);

    $cart->add($product, array('qty' => 1));
    $cart->save();
}
public function initPack(){
    $id_lang = Context::getContext()->language->id;
    // New product with right language id
    $product = new Pack($id_lang);

    // I'm not sure that is the right way to assign the tax
    // $product->tax_name = 'tva1';
    // $product->tax_rate = '10%';
    $product->id_tax_rules_group = // Here you have to write the right id of tax_rules_group, you can find in Localization -> Tax Rules

    $product->id_manufacturer = 1; // Not mandatory, check if this id exists really
    $product->id_supplier = 1; // Not mandatory, check if this id exists really
    // $product->id_category_default = 1; // Corrected after
    $product->id_shop_default = 1;
    $product->manufacturer_name = 'rrr';
    $product->supplier_name = 'rrr';
    $product->name =  'coffret-4';
    $product->description = 'description 1';
    $product->description_short = 'description short';
    $product->quantity = 20;
    $product->on_sale = false; // True or false? :)
    $product->online_only = true;
    $product->reference = '1234567889'; // Not mandatory
    $product->ean13     = 12233; // Not mandatory
    $product->upc = 12233; // Not mandatory
    // Link rewrite generated by Tools class
    $product->link_rewrite = Tools::link_rewrite($product->name);
    $product->meta_description = 'fff';
    $product->meta_keywords = 'ff';
    $product->meta_title  = 'fff';
    $product->visibility= 'both';

    // $product->date_add = date("j, n, Y"); // Useless, it's 'automatic' :)
    // $product->location = ''; // Useless
    // You have to set the category field like an array
    $product->category[0] = 16;
    // And give also the default category id
    $product->id_category_default = 16;
    $product->text_fields = 1;
    $product->condition = 'new';
    $product->on_sale = true; // True or false? :)
    // We have to tell to prestashop that product is a 'pack'
    $product->cache_is_pack = 1;

    if($product->save())
        $product->add();
    else{
        echo 'erooooooor';
        die();
    }

    // After save the product we must update categories associations
    if ( isset( $product->id_category ) && is_array($product->id_category) ) {
        $product->updateCategories(array_map('intval', $product->id_category));
    }

    Pack::addItem($product->id, 1, 1);
    Pack::addItem($product->id, 3, 1);

    // I don't think so you can add the product attribute to a pack
    // $attribute = $product->addProductAttribute(12, 234, 2344, 12, 20, 1, 222, null, 222, 1);

    $cart->add($product, array('qty' => 1));
    $cart->save();
}

购物车中的产品数量增加,但没有名称、价格。。。如图所示,Prestashop将包装视为产品,因此您必须为该包装指定价格,例如

$product->price = 10.00;
否则,我建议您按如下方式编辑代码:

public function initPack(){
    $product = new Pack();

    $product->tax_name = 'tva1';
    $product->tax_rate = '10%';
    $product->id_manufacturer = 1;
    $product->id_supplier = 1;
    $product->id_category_default = 1;
    $product->id_shop_default = 1;
    $product->manufacturer_name = 'rrr';
    $product->supplier_name = 'rrr';
    $product->name =  array((int)Configuration::get('PS_LANG_DEFAULT') =>  'coffret-4');;
    $product->description = 'description 1';
    $product->description_short = 'description short';
    $product->quantity = 20;
    $product->on_sale = false;
    $product->online_only = true;
    $product->reference = '1234567889';
    $product->ean13     = 12233;
    $product->upc = 12233;
    $product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') =>  'coffret-4');
    $product->meta_description = 'fff';
    $product->meta_keywords = 'ff';
    $product->meta_title  = 'fff';
    $product->visibility= 'both';

    $product->date_add = date("j, n, Y");
    $product->location = '';
    $product->category = 16;
    $product->text_fields = 1;
    $product->condition = 'new';
    $product->on_sale = true;

    if($product->save())
        $product->add();
    else{
        echo 'erooooooor';
        die();
    }

    Pack::addItem($product->id, 1, 1);
    Pack::addItem($product->id, 3, 1);

    $attribute = $product->addProductAttribute(12, 234, 2344, 12, 20, 1, 222,
    $id_supplier = null, 222, 1);

    $cart->add($product, array('qty' => 1));
    $cart->save();
}
public function initPack(){
    $id_lang = Context::getContext()->language->id;
    // New product with right language id
    $product = new Pack($id_lang);

    // I'm not sure that is the right way to assign the tax
    // $product->tax_name = 'tva1';
    // $product->tax_rate = '10%';
    $product->id_tax_rules_group = // Here you have to write the right id of tax_rules_group, you can find in Localization -> Tax Rules

    $product->id_manufacturer = 1; // Not mandatory, check if this id exists really
    $product->id_supplier = 1; // Not mandatory, check if this id exists really
    // $product->id_category_default = 1; // Corrected after
    $product->id_shop_default = 1;
    $product->manufacturer_name = 'rrr';
    $product->supplier_name = 'rrr';
    $product->name =  'coffret-4';
    $product->description = 'description 1';
    $product->description_short = 'description short';
    $product->quantity = 20;
    $product->on_sale = false; // True or false? :)
    $product->online_only = true;
    $product->reference = '1234567889'; // Not mandatory
    $product->ean13     = 12233; // Not mandatory
    $product->upc = 12233; // Not mandatory
    // Link rewrite generated by Tools class
    $product->link_rewrite = Tools::link_rewrite($product->name);
    $product->meta_description = 'fff';
    $product->meta_keywords = 'ff';
    $product->meta_title  = 'fff';
    $product->visibility= 'both';

    // $product->date_add = date("j, n, Y"); // Useless, it's 'automatic' :)
    // $product->location = ''; // Useless
    // You have to set the category field like an array
    $product->category[0] = 16;
    // And give also the default category id
    $product->id_category_default = 16;
    $product->text_fields = 1;
    $product->condition = 'new';
    $product->on_sale = true; // True or false? :)
    // We have to tell to prestashop that product is a 'pack'
    $product->cache_is_pack = 1;

    if($product->save())
        $product->add();
    else{
        echo 'erooooooor';
        die();
    }

    // After save the product we must update categories associations
    if ( isset( $product->id_category ) && is_array($product->id_category) ) {
        $product->updateCategories(array_map('intval', $product->id_category));
    }

    Pack::addItem($product->id, 1, 1);
    Pack::addItem($product->id, 3, 1);

    // I don't think so you can add the product attribute to a pack
    // $attribute = $product->addProductAttribute(12, 234, 2344, 12, 20, 1, 222, null, 222, 1);

    $cart->add($product, array('qty' => 1));
    $cart->save();
}
我假设您没有启用多跳。我没有测试这段代码,但我猜它是有效的;),试着让我知道;)