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



我有一个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.

然后我在数据库上检查这个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可以看到新产品。

现在的问题是:

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

$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();

当然,用户需要再次登录才能看到购物车,但prestashop似乎要"登录";"自动创建";每次用户登录(x次(都会有一个购物车,所以这个过程只适用于用户没有登录网站或由于某种原因没有购物车的情况。

相关内容

最新更新