以编程方式创建/检索购物车

时间:2014-04-09 22:11:55

标签: prestashop

对于我正在编写的模块,我需要在调用链接并传递一些数据后检索某个用户的购物车(不需要注册)。
我的想法是收回以前传递的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中(似乎在每个页面中调用)。所以,对我来说,在“用户会话”期间,应始终存在

但是 - 是的,有一个但是 - 当我尝试检索购物车时(以这种方式,进入我模块的控制器)

$context=Context::getContext();
$id_cart=$context->cart->id;

$id_cart不存在,所以cart似乎错过了。所以我有点困惑。

这里有什么?有人可以给我一些指示吗?

PS:

我试图复制该功能(只有else部分),但它不起作用

2 个答案:

答案 0 :(得分:5)

当用户未登录且购物车中没有产品时,您可以强制购物车生成:

$context = Context::getContext();
if (!$context->cart->id) {
  $context->cart->add();
  $context->cookie->id_cart = $context->cart->id;
}
$id_cart = $context->cart->id;

查看processChangeProductInCart

中的controllers/front/CartController.php方法

答案 1 :(得分:0)

我正在使用Prestashop 1.6,@ yyshiraks的回答对我不起作用。我无法使用$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),则必须使用上述代码。