将新变量添加到Prestashop Cart对象

时间:2019-07-16 09:07:17

标签: javascript php ajax prestashop cart

在购物车(我使用5步购物车)中,我添加了单选框“ stock_action”,并且需要将此值发送到Cart对象,因为我希望根据该值增加一些定购费用。

在购物车替代中,我添加了$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中有更简单的方法? 谢谢。

1 个答案:

答案 0 :(得分:1)

您需要将'stock_action'字段添加到数据库中的cart表中。然后,此属性将在您的购物车类实例以及其他属性中可用。如果不这样做,您会在所有与购物车相关的事件中得到一个错误 Unknown column 'stock_action' in 'field list' ,要查看该错误,只需在config/defines.inc.php设置define('_PS_MODE_DEV_', false);中打开Dev模式真实。

UPD: 要使您的AJAX正常工作,请遵循以下步骤(除了之前的步骤) 1.覆盖/修改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(); }

之后{br1 if (Tools::isSubmit('ajax') && Tools::getValue('method') == 'updateExtraCarrier') { .... }区块
2.转到主题js文件夹,然后将您的AJAX代码添加到global.js

$(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,并且在购物车存在或您将其更改为其他值之前,该值将一直存在。