【问题标题】:Prestashop - create a cart by extarnal scriptPrestashop - 通过外部脚本创建购物车
【发布时间】: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


    【解决方案1】:

    试试

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

    【讨论】:

      【解决方案2】:

      我实际上找到了一种解决方法,可能太长或不够优雅,但它确实有效。

      阅读真实代码的工作原理我知道在为用户制作实际购物车之前我需要一个会话(登录),所以我写:

      $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 次)时“自动创建”一个购物车,所以此过程仅适用于用户未登录网站时或者由于某种原因没有购物车。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-19
        • 1970-01-01
        • 2013-11-02
        • 1970-01-01
        相关资源
        最近更新 更多