在Prestashop中,我创建了一个自定义表单,其中显示包含所有产品的列表,用户可以填写相应的数量。通过提交表单,我清除购物车并用新值填充它,最后重定向到结帐页面。
一切正常,但只有当购物车已经存在时。如果是空车(cart_id == null),我无法添加产品。我尝试了几种方法来创建$ cart但我没有设法这样做。
我没有任何例外,代码执行没有错误,只是在最后,在结帐页面,购物车仍然是空的;我再说一遍,只有当车已经空了。如果购物车中有产品,那么这个过程就是完美的!
我希望得到一些帮助。
这是我的小型控制器,它从表单中获取产品和数量,并将它们添加到购物车中:
include('../../config/config.inc.php');
include('../../header.php');
// Filling the array of products
$products = array();
foreach ($_POST as $key => $value)
if (substr($key, 0, 9) === 'quantity_')
$products[substr($key, 9)] = $value;
// First of all, we remove all products
$prods = $cart->getProducts();
foreach ($prods as $prod)
$cart->deleteProduct($prod['id_product']);
// Adding the new products
foreach ($products as $product_id => $quantity)
if ($quantity > 0)
$cart->updateQty($quantity, $product_id);
// Redirecting to the checkout page.
header("Location: " . $_POST['redirect']);
exit();`
提前谢谢!
答案 0 :(得分:8)
我遇到了同样的问题,Prestashop在以多种不同方式调用CartCore时未正确创建新购物车 - 上下文或直接调用都没有成功。
从其他人over here找到此宝石:
// get cart id if exists
if ($this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
}
// create new cart if needed
if (!isset($cart) OR !$cart->id)
{
$cart = new Cart();
$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;
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$this->context->cookie->id_cart = (int)($cart->id);
$cart->update();
}
现在这对我有用。如您所见,它比仅仅要求上下文检索或创建新购物车更深入。第h
答案 1 :(得分:4)
当你在课堂外时,你可能需要包括这一行
$context = Context::getContext();
然后添加一个定义购物车属性的购物车变量(在个案中分配一个ID)
$cart = $context->cart; // gets the cart id or creates a new one
现在应该可以正常工作
BR(不要忘记接受答案,如果它可以帮助你:))
答案 2 :(得分:1)
作为之前帖子的补充。
如果您设置了该配置:Configuration::updateValue('PS_CART_FOLLOWING', 1)
(显示客户的最后一个购物车,而不是每次都创建一个新购物车)。
当您尝试使用以下代码在客户首次登录时(创建新帐户后)创建新购物车时,您将始终在$this->context->cart->id
获取购物车ID NULL
:
if (is_null($this->context->cart)) {
$this->context->cart =
new Cart($this->context->cookie->id_cart);
}
我设法通过在无效的购物车创建后添加一些代码来解决问题。
/**
* Manage the possible errors when trying to create
* a new cart for a customer.
*
* The new cart is saved in the current context ($this->context->cart).
*/
private function createCart()
{
if (is_null($this->context->cart)) {
$this->context->cart =
new Cart($this->context->cookie->id_cart);
}
if (is_null($this->context->cart->id_lang)) {
$this->context->cart->id_lang = $this->context->cookie->id_lang;
}
if (is_null($this->context->cart->id_currency)) {
$this->context->cart->id_currency = $this->context->cookie->id_currency;
}
if (is_null($this->context->cart->id_customer)) {
$this->context->cart->id_customer = $this->context->cookie->id_customer;
}
if (is_null($this->context->cart->id_guest)) {
if (empty($this->context->cookie->id_guest)){
$this->context->cookie->__set(
'id_guest',
Guest::getFromCustomer($this->context->cookie->id_customer)
);
}
$this->context->cart->id_guest = $this->context->cookie->id_guest;
}
if (is_null($this->context->cart->id)) {
$this->context->cart->add();
$this->context->cookie->__set('id_cart', $this->context->cart->id);
}
}
以这种方式初步推车后,我现在可以毫无问题地将产品添加到购物车。
每个人都很成功