prestashop - Prestashop 1.6 - 我的产品如何从我的模块的 php 页面购物
问题描述
我有一个带有自定义模块的 php 代码。如何通过代码将产品添加到购物车。我有 id_number(产品 id)和 id_customer(客户 id)
<?php
$product id = 1167;
// how to proceed to add information to cart
需要将此产品(id_number)更新到我的购物车(登录用户)
解决方案
我在我的模块中使用此代码段将产品添加到购物车,但您可以在覆盖的控制器中使用它:
public function add2cart(){
$id = 1167;
$qty = 1;
if($id!=''){
$this->createCart();
$this->context->cart->updateQty($qte, $id, null, '');
$this->context->cart->save();
$this->context->cookie->__set('id_cart', $this->context->cart->id);
echo 'Product added to cart!';
exit;
}
}
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);
}
}
在我的 initContent 函数中,我调用了函数 add2cart() :
$this->add2cart();
推荐阅读
- python - 如何通过Django导出多个文件
- python - Python 3 中的堆实现
- python - 如何在 Python 中列出所有类 cached_properties?
- html-table - HTML 表格在 Open Office Writer 中无法正确显示
- azure - 如何在 Python 中从 YAML 文件创建 Azure ML Inference_Config 和 Deployment_Config 类对象?
- arrays - 当我的索引是变量时无法访问数组值
- java - 如何在 java 中为 hashcode() 方法编写 ptest(mutation test)
- angular - 为什么在 Angular 中不显示输出(浏览器预览)的变化
- kotlin - 一起使用 akka actor 模型和 kotlin 协程
- ssl - javax.net.ssl 警告“user_canceled”是什么意思?