Prestashop - 通过外部脚本创建购物车

Prestashop - create a cart by extarnal script

我有一个用于 prestashop 1.7 的脚本(有效),用于在我的数据库中添加产品

require_once('config/config.inc.php');
require_once('init.php');

$default_lang = Configuration::get("PS_LANG_DEFAULT");
$product = new Product();
$product->name = [$default_lang => "Acquisto cumulativo diretta online"];
$product->link_rewrite = "acquisto_online";
$product->price = 95.90;
$product->quantity = 10;
$product->id_category = [10];
$product->id_category_default = 10;
$bool = $product->add();
if ($bool)
{
    $product->updateCategories($product->id_category);
    
    StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);
    }

它工作正常,我可以将我的产品添加到我设置为“不可见”的类别中,我可以正常使用该产品。

然后我需要为特定用户创建(无需登录)购物车(可能还有他购物车中已有的其他产品)

所以我在 id_customer 的数据库上写了我的检查,并创建了一个 customer

的实例
$customer = new Customer(3); // where 3 in this example is the correct id of the customer.

然后我检查数据库是否已经存在用于此 id_customer 的购物车,如果我找到普通购物车我就使用

$cart = new Cart(ID_CART_I_FIND_ON_DB);
$cart->updateQty(1, 122); // where 1 is the quantity, 122 the id of my product

完美运行,用户刷新购物车可以看到新产品。

现在的问题:

如果我没有找到常规购物车,我需要先创建一个新购物车

$cart = new Cart();
$cart->id_customer = (int)($customer->id);
$cart->id_address_delivery = (int)  (0);
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)(2);
$cart->id_currency = (int)(2);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$cart->update();
$cart->save();
$cart->updateQty(1, 122);

它有点管用,我可以在我的数据库上看到这个购物车,当然,我可以在我的后台面板中看到它,但是由于“安全密钥”和 guest_id. 我尝试在数据库中手动插入正确的安全密钥,但什么也没有。 我尝试手动添加 guest_id 然后强制该用户登录,有时会出现购物车。

我确定我遗漏了什么。 有人可以帮我吗? :)

也许有更好的方法从外部脚本(因此没有他的 cookie)为特定用户“创建”新购物车(如果不存在)

谢谢

尝试

$cart = new Cart();
$cart->id_customer = (int)($customer->id);
$cart->id_address_delivery = (int)  (0);
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)(2);
$cart->id_currency = (int)(2);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->secure_key = $customer->secure_key;
$cart->save();
$cart->updateQty(1, 122);

我实际上找到了一个解决方法,可能太长或不够优雅,但它有效。

阅读真实代码的工作原理我明白我在为用户制作实际购物车之前需要一个会话(需要登录)所以我写:

$customer = new Customer(ID_USER);


$context=Context::getContext(); //I set a context manually

$context->customer = $customer;
    //create a cookie
    $context->smarty->assign('confirmation', 1);
    $context->cookie->id_customer = (int)$customer->id;
    $context->cookie->customer_lastname = $customer->lastname;
    $context->cookie->customer_firstname = $customer->firstname;
    $context->cookie->passwd = $customer->passwd;
    $context->cookie->logged = 1;
    
    if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE'))
    $context->cookie->account_created = 1;
    $customer->logged = 1;
    $context->cookie->email = $customer->email;

    $context->cookie->is_guest = !Tools::getValue('is_new_customer', 1);
    // Update cart address
    $context->cart->secure_key = $customer->secure_key;

那么如果购物车还不存在当然:

$cart = new Cart();
    $cart->id_customer = (int)($customer->id);
    $cart->id_address_delivery = (int)  (0);
    $cart->id_address_invoice = $cart->id_address_delivery;
    $cart->id_lang = (int)(2);
    $cart->id_currency = (int)(2);
    $cart->id_carrier = 1;
    $cart->recyclable = 0;
    $cart->secure_key = $customer->secure_key;
    $cart->gift = 0;
    $cart->add();
    $cart->id_guest = $context->cart->id_guest;  
    $cart->update();
  $cart->save();

当然,用户需要再次登录才能看到购物车,但是每次用户登录(x 次)时,prestashop 似乎都会“自动创建”一个购物车,所以此过程仅在用户未登录时执行在网站上或出于某种原因没有购物车。