向 Prestashop 购物车对象添加新变量
Add new variable to Prestashop Cart object
在购物车(我使用 5 步购物车)中,我添加了单选框 "stock_action",我需要将此值发送到购物车对象,因为根据该值,我想添加一些额外的订购费用.
在购物车覆盖中,我添加了 $stock_action
变量:
public $stock_action;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'cart',
'primary' => 'id_cart',
'fields' => array(
'id_shop_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_address_delivery' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_address_invoice' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_carrier' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_guest' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'stock_action' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'recyclable' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'gift' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'gift_message' => array('type' => self::TYPE_STRING, 'validate' => 'isMessage'),
'mobile_theme' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'delivery_option' => array('type' => self::TYPE_STRING),
'secure_key' => array('type' => self::TYPE_STRING, 'size' => 32),
'allow_seperated_package' =>array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
),
);
但是在点击购物车中的下一步按钮后,stock_action 收音机的值没有发送。
然后我尝试使用 AJAX 发送该值,但我不确定如何正确发送它并在另一端捕获它以分配给 Cart 对象。我在 cart-summary.js
中尝试了其他函数的各种组合,我想出了这样的东西:
function setStockAction() {
var val = $('input[name=stock_action]:checked').val();
$.ajax({
type: 'POST',
headers: {'cache-control': 'no-cache'},
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType: 'json',
data: 'controller=cart'
+ '&ajax=true'
+ '&stock_action=' + val
+ '&token=' + static_token
+ '&allow_refresh=1',
success: function(jsonData) {
alert('ok!');
}
});
}
我走在正确的轨道上还是在 Prestashop 中有更简单的方法?
谢谢。
您需要将 'stock_action'
字段添加到数据库中的 cart
table。然后这个 属性 将在您的购物车 class 实例以及其他属性中可用。如果你不这样做,你会在所有 cart-related 事件 Unknown column 'stock_action' in 'field list'
中收到错误消息,只需在 config/defines.inc.php
设置中打开开发模式即可查看define('_PS_MODE_DEV_', false);
为真。
更新:
要使您的 AJAX 作品遵循后续步骤(除了之前的步骤)
1. Override/modify controllers/front/OrderController.php 方法 initContent() 并添加下一个代码
if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'updateStockAction') {
$this->context->cart->stock_action = (int)Tools::getValue('stock_action');
$this->context->cart->save();
}
就在
if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'updateExtraCarrier') {
....
}
块
之后
2. 转到您的主题 js 文件夹并添加到 global.js 您的 AJAX 代码
$(document).on('change', 'input[name="stock_action"]', function() {
var val = $('input[name=stock_action]:checked').val();
$.ajax({
type: 'POST',
headers: {'cache-control': 'no-cache'},
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType: 'json',
data: 'controller=order'
+ '&ajax=true'
+ '&stock_action=' + val
+ '&method=updateStockAction'
+ '&token=' + static_token
+ '&allow_refresh=1',
success: function(jsonData) {
alert('ok!');
}
});
});
3。转到您的主题模板文件 shopping-cart.tpl 并在某处添加您的收音机输入
<input type="radio" name="stock_action" value="1" />
<input type="radio" name="stock_action" value="0" />
我检查了一下,它有效!在接下来的所有页面上,您将拥有 stock_action
的正确值,并且在您的购物车存在或您将其更改为不同的
之前,它一直是相关的
在购物车(我使用 5 步购物车)中,我添加了单选框 "stock_action",我需要将此值发送到购物车对象,因为根据该值,我想添加一些额外的订购费用.
在购物车覆盖中,我添加了 $stock_action
变量:
public $stock_action;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'cart',
'primary' => 'id_cart',
'fields' => array(
'id_shop_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_address_delivery' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_address_invoice' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_carrier' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_guest' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'stock_action' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'recyclable' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'gift' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'gift_message' => array('type' => self::TYPE_STRING, 'validate' => 'isMessage'),
'mobile_theme' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'delivery_option' => array('type' => self::TYPE_STRING),
'secure_key' => array('type' => self::TYPE_STRING, 'size' => 32),
'allow_seperated_package' =>array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
),
);
但是在点击购物车中的下一步按钮后,stock_action 收音机的值没有发送。
然后我尝试使用 AJAX 发送该值,但我不确定如何正确发送它并在另一端捕获它以分配给 Cart 对象。我在 cart-summary.js
中尝试了其他函数的各种组合,我想出了这样的东西:
function setStockAction() {
var val = $('input[name=stock_action]:checked').val();
$.ajax({
type: 'POST',
headers: {'cache-control': 'no-cache'},
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType: 'json',
data: 'controller=cart'
+ '&ajax=true'
+ '&stock_action=' + val
+ '&token=' + static_token
+ '&allow_refresh=1',
success: function(jsonData) {
alert('ok!');
}
});
}
我走在正确的轨道上还是在 Prestashop 中有更简单的方法? 谢谢。
您需要将 'stock_action'
字段添加到数据库中的 cart
table。然后这个 属性 将在您的购物车 class 实例以及其他属性中可用。如果你不这样做,你会在所有 cart-related 事件 Unknown column 'stock_action' in 'field list'
中收到错误消息,只需在 config/defines.inc.php
设置中打开开发模式即可查看define('_PS_MODE_DEV_', false);
为真。
更新:
要使您的 AJAX 作品遵循后续步骤(除了之前的步骤)
1. Override/modify controllers/front/OrderController.php 方法 initContent() 并添加下一个代码
if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'updateStockAction') {
$this->context->cart->stock_action = (int)Tools::getValue('stock_action');
$this->context->cart->save();
}
就在
if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'updateExtraCarrier') {
....
}
块
之后
2. 转到您的主题 js 文件夹并添加到 global.js 您的 AJAX 代码
$(document).on('change', 'input[name="stock_action"]', function() {
var val = $('input[name=stock_action]:checked').val();
$.ajax({
type: 'POST',
headers: {'cache-control': 'no-cache'},
url: baseUri + '?rand=' + new Date().getTime(),
async: true,
cache: false,
dataType: 'json',
data: 'controller=order'
+ '&ajax=true'
+ '&stock_action=' + val
+ '&method=updateStockAction'
+ '&token=' + static_token
+ '&allow_refresh=1',
success: function(jsonData) {
alert('ok!');
}
});
});
3。转到您的主题模板文件 shopping-cart.tpl 并在某处添加您的收音机输入
<input type="radio" name="stock_action" value="1" />
<input type="radio" name="stock_action" value="0" />
我检查了一下,它有效!在接下来的所有页面上,您将拥有 stock_action
的正确值,并且在您的购物车存在或您将其更改为不同的