【发布时间】:2021-05-09 11:34:22
【问题描述】:
我有一个用于 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 = new Customer(3); // where 3 in this example is the correct id of the customer.
然后我检查 db 是否已经存在此 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,用户看不到它。 我尝试在 db 中手动插入正确的安全密钥,但什么也没有。 我尝试手动添加一个guest_id,然后强制该用户登录,有时会出现购物车。
我确定我错过了什么。 有人可以帮我吗? :)
也许有更好的方法可以从外部脚本(因此没有他的 cookie)为特定用户“创建”新购物车(如果不存在)
谢谢
【问题讨论】:
标签: php prestashop external