Prestashop 以编程方式创建/检索购物车
对于我正在编写的模块,在调用链接并传递一些数据之后,我需要为某个用户检索购物车(不需要注册)。Prestashop 以编程方式创建/检索购物车,prestashop,Prestashop,对于我正在编写的模块,在调用链接并传递一些数据之后,我需要为某个用户检索购物车(不需要注册)。 我的想法是接收一个以前传递的id,它可以帮助我识别某个购物车 我最大的问题是,我已经在prestashop中搜索了很多创建代码车的内容。我终于发现了一些东西 /* Cart already exists */ if ((int)$this->context->cookie->id_cart) { $cart = new Cart($this->context->c
我的想法是接收一个以前传递的id,它可以帮助我识别某个购物车 我最大的问题是,我已经在prestashop中搜索了很多创建代码车的内容。我终于发现了一些东西
/* Cart already exists */
if ((int)$this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
if ($cart->OrderExists())
{
unset($this->context->cookie->id_cart, $cart, $this->context->cookie->checkedTOS);
$this->context->cookie->check_cgv = false;
}
/* Delete product of cart, if user can't make an order from his country */
elseif (intval(Configuration::get('PS_GEOLOCATION_ENABLED')) &&
!in_array(strtoupper($this->context->cookie->iso_code_country), explode(';', Configuration::get('PS_ALLOWED_COUNTRIES'))) &&
$cart->nbProducts() && intval(Configuration::get('PS_GEOLOCATION_NA_BEHAVIOR')) != -1 &&
!FrontController::isInWhitelistForGeolocation() &&
!in_array($_SERVER['SERVER_NAME'], array('localhost', '127.0.0.1')))
unset($this->context->cookie->id_cart, $cart);
// update cart values
elseif ($this->context->cookie->id_customer != $cart->id_customer || $this->context->cookie->id_lang != $cart->id_lang || $currency->id != $cart->id_currency)
{
if ($this->context->cookie->id_customer)
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)$currency->id;
$cart->update();
}
/* Select an address if not set */
if (isset($cart) && (!isset($cart->id_address_delivery) || $cart->id_address_delivery == 0 ||
!isset($cart->id_address_invoice) || $cart->id_address_invoice == 0) && $this->context->cookie->id_customer)
{
$to_update = false;
if (!isset($cart->id_address_delivery) || $cart->id_address_delivery == 0)
{
$to_update = true;
$cart->id_address_delivery = (int)Address::getFirstCustomerAddressId($cart->id_customer);
}
if (!isset($cart->id_address_invoice) || $cart->id_address_invoice == 0)
{
$to_update = true;
$cart->id_address_invoice = (int)Address::getFirstCustomerAddressId($cart->id_customer);
}
if ($to_update)
$cart->update();
}
}
if (!isset($cart) || !$cart->id)
{
$cart = new Cart();
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_guest = (int)($this->context->cookie->id_guest);
$cart->id_shop_group = (int)$this->context->shop->id_shop_group;
$cart->id_shop = $this->context->shop->id;
if ($this->context->cookie->id_customer)
{
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_address_delivery = (int)(Address::getFirstCustomerAddressId($cart->id_customer));
$cart->id_address_invoice = $cart->id_address_delivery;
}
else
{
$cart->id_address_delivery = 0;
$cart->id_address_invoice = 0;
}
// Needed if the merchant want to give a free product to every visitors
$this->context->cart = $cart;
CartRule::autoAddToCart($this->context);
}
它包含在FrontController.php
中(似乎每个页面都会调用它)。因此,对我来说,购物车应该在“用户会话”期间始终存在
但是-是的,当我尝试检索购物车(以这种方式,进入我模块的控制器)时,有一个但是
$id\u cart
不存在,因此cart
似乎没有找到。所以我有点困惑
这是怎么回事?有人能给我一些建议吗
附言:
我已尝试复制该功能(仅复制
其他
部分),但它不起作用当用户未登录且购物车中没有产品时,您可以强制生成购物车:
$context = Context::getContext();
if (!$context->cart->id) {
$context->cart->add();
$context->cookie->id_cart = $context->cart->id;
}
$id_cart = $context->cart->id;
查看
controllers/front/CartController.php中的processchangeproductinbot
方法,当用户未登录且购物车中没有产品时,您可以强制生成购物车:
$context = Context::getContext();
if (!$context->cart->id) {
$context->cart->add();
$context->cookie->id_cart = $context->cart->id;
}
$id_cart = $context->cart->id;
看看controllers/front/CartController.php中的ProcessChangeProductInNot
方法,我使用的是Prestashop 1.6,@yenshiraks的答案对我不起作用。我不能使用$context->cart->add()代码>,因为$context->cart
为空
这就是我的情况:
$context = Context::getContext();
$cart_id = null
if($context->cookie->id_cart) {
$cart = new Cart($context->cookie->id_cart);
$cart_id = $cart->id; // just in case the cookie contains an invalid cart_id
}
if(empty($cart_id)) {
$cart = new Cart();
$cart->id_lang = (int)$context->cookie->id_lang;
$cart->id_currency = (int)$context->cookie->id_currency;
$cart->id_guest = (int)$context->cookie->id_guest;
$cart->id_shop_group = (int)$context->shop->id_shop_group;
$cart->id_shop = $context->shop->id;
$cart->add();
$cart_id = $cart->id;
}
$context->cookie->id_cart = $cart_id;
最后回答问题:尽管购物车总是在FrontController
中生成,但它不会保存到数据库中,因此id
为空
如果您在一个上下文中,FrontController
被实例化(前端的任何页面,$context->cart->add();
将足以将一个空的购物车保存到数据库中
另一方面,如果你在一个被直接调用的脚本中(比如prestashop/modules/my_module/script.php
),你必须使用上面的代码。我在使用prestashop 1.6,@yenshiraks answer对我不起作用。我不能使用$context->cart->add();
,因为$context->cart
为空
这就是我的情况:
$context = Context::getContext();
$cart_id = null
if($context->cookie->id_cart) {
$cart = new Cart($context->cookie->id_cart);
$cart_id = $cart->id; // just in case the cookie contains an invalid cart_id
}
if(empty($cart_id)) {
$cart = new Cart();
$cart->id_lang = (int)$context->cookie->id_lang;
$cart->id_currency = (int)$context->cookie->id_currency;
$cart->id_guest = (int)$context->cookie->id_guest;
$cart->id_shop_group = (int)$context->shop->id_shop_group;
$cart->id_shop = $context->shop->id;
$cart->add();
$cart_id = $cart->id;
}
$context->cookie->id_cart = $cart_id;
最后回答问题:尽管购物车总是在FrontController
中生成,但它不会保存到数据库中,因此id
为空
如果您在一个上下文中,FrontController
被实例化(前端的任何页面,$context->cart->add();
将足以将一个空的购物车保存到数据库中
另一方面,如果您在一个脚本中,则直接调用该脚本(如prestashop/modules/my_module/script.php
),您必须使用上述代码。我使用粘贴到模块挂钩中的代码运行了一些测试。它会按预期返回购物车id。没有购物车id的唯一情况是购物车为空且用户未登录,但我认为这是正常行为。@yenshirak:是的,可能是标准行为,但我需要创建购物车即使用户没有登录,产品也不存在。也许我应该深入研究将购物车持久化到db的代码并复制它behaviour@All此psmodules链接看起来像垃圾邮件,请避免单击此处没有任何信息。我使用粘贴到模块挂钩中的代码运行了一些测试。它按预期返回购物车id。唯一的问题是没有购物车id时的评估是购物车为空且用户未登录,但我认为这是正常行为。@yenshirak:是的,可能这是标准行为,但即使用户未登录且产品不存在,我也需要创建购物车。也许我应该深入研究购物车持久化到db并复制到db的代码帽子behaviour@All此psmodules链接看起来像垃圾邮件,请避免单击此处没有任何信息好吧,我正是在说这个,我遗漏了一个我应该检查的小部分,我将按照您的建议进行操作,看看它是否有效。暂时感谢您,我会告诉您何时可以尝试;-)这在我的例子中给出了一个错误:$context->cart
是空的。好吧,我刚才说的正是这个,我遗漏了一个我应该检查的小部分,我将按照你的建议去做,看看它是否有效。谢谢你,我会告诉你什么时候可以尝试;-)这在我的例子中给出了一个错误:$context->cart
为空