Php Prestashop-通过extarnal脚本创建购物车

Php Prestashop-通过extarnal脚本创建购物车,php,prestashop,external,script,Php,Prestashop,External,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 => "Ac

我有一个用于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.
然后我在db上检查这个id_客户的购物车是否已经存在,如果我找到一个我只使用的普通购物车

$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);
它有点工作,我可以在我的db上看到这个购物车,当然,我可以在我的后台面板上看到它,但是用户看不到它,因为“安全密钥”和来宾id。 我试图在db中手动插入正确的secury键,但什么都没有。 我尝试手动添加一个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();
当然,用户需要再次登录才能看到购物车,但prestashop似乎会在用户每次登录时“自动创建”购物车(对于x时间),因此此过程仅适用于用户未登录网站或由于某种原因没有购物车的情况