如何在Prestashop中的表中的自定义字段中插入值?

时间:2017-01-13 11:16:40

标签: mysql override prestashop prestashop-1.6

我添加了一个名为&#34的自定义字段; deldate" in" ps_orders"表,我在" OPC"上添加了一个文本框。结帐页面。现在,当我点击订单确认按钮时,文本框中的值应该保存在" deldate"领域" ps_orders"表。 文本框显示完美但我需要在哪些文件中进行更改以保存表格中的文本框值? (主题是默认主题。)

snap of textbox

snap of custom field in table.

类/顺序/ order.php

class OrderCore extends ObjectModel 
{ 
public $deldate; 
} 

public static $definition = array( 
'fields' => array( 
'deldate'=> array('type' => self::TYPE_STRING),
),
)

购物-cart.tpl

<div class="box">
<div class="required form-group">
<form method="post">
<label for="Fecha de entrega deseada">{l s='Desired delivery date' mod='deldate'}</label>
<input type="text" id="deldate" name="deldate" class="form-control" value="hello" />
</form>
</div>

</div>

3 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案...... 如果您想在结帐过程中向订单添加一些信息,您必须在其他地方保存此信息,如果您看起来购物车表与订单表非常相似。 你为什么要这样做?由于您在客户确认之前没有订单,因此在结帐未完成之前,信息无法保存在订单表中。

首先,在数据库中创建字段,在这种情况下,您必须添加ps_ordersps_cart。 (在你的情况下,我建议使用DATETIME字段)

其次,覆盖Order类:

class Order extends OrderCore
{
    public function __construct($id = null, $id_lang = null)
    {
        self::$definition['fields']['deldate'] = array('type' => self::TYPE_DATE);

        parent::__construct($id, $id_lang);
    }
}

Cart类:

class Cart extends CartCore
{
    public function __construct($id = null, $id_lang = null)
    {
        self::$definition['fields']['deldate'] = array('type' => self::TYPE_DATE);

        parent::__construct($id, $id_lang);
    }
}

现在我们必须在结帐过程中保存字段,因此我们覆盖OrderController

class OrderController extends OrderControllerCore
{
    public function processAddress()
    {
        parent::processAddress();

        // Here we begin our story
        if(Tools::getIsset('deldate')) // Check if the field isn't empty
        {
            $deldate = Tools::getValue('deldate');

            // Here you must parse and check data validity (I leave to you the code)
            /* ... */

            // Assign the data to context cart
            $this->context->cart->deldate = $deldate;
            // Save information
            $this->context->cart->update();
        }
    }
}

现在你必须运输&#39;从购物车到订单的这些信息,这将通过PaymentModule类完成,特别是使用validateOrder方法。

所以,另一个覆盖:

class PaymentModule extends PaymentModuleCore
{
    public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method = 'Unknown', $message = null, $extra_vars = array(), $currency_special = null, $dont_touch_amount = false, $secure_key = false, Shop $shop = null)
    {
        $result = parent::validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method, $message, $extra_vars, $currency_special, $dont_touch_amount, $secure_key, $shop);

        if($result)
        {
            $oldcart = new Cart($id_cart);
            $neworder = new Order($this->currentOrder);
            $neworder->deldate = $oldcart->deldate;
            $neworder->update();
            return true; // important
        } 
        else 
        {
            return $result;
        }
    }
}

完成所有这些后,您已保存deldate字段。但是,我绝对不建议使用这种方法,使用模块和钩子更安全,更简单...但这是另一个故事:)

这仅适用于结帐的五个步骤。

对于下一个代码行,上帝救了我......

如果你想使用OPC,你必须用JS弄脏手并覆盖OrderOpcController。 从JS开始,编辑已启用主题的js文件夹中的order-opc.js,找到bindInputs函数并附加以下代码行:

function bindInputs()
{
    /* ... */

    $('#deldate').on('change', function(e){
        updateDelDateInput(); // custom function to update deldate
    });
}

然后将自定义函数追加到文件中:

function updateDelDateInput()
{
    $.ajax({
            type: 'POST',
            headers: { "cache-control": "no-cache" },
            url: orderOpcUrl + '?rand=' + new Date().getTime(),
            async: false,
            cache: false,
            dataType : "json",
            data: 'ajax=true&method=updateDelDate&deldate=' + encodeURIComponent($('#deldate').val()) + '&token=' + static_token ,
            success: function(jsonData)
            {
                if (jsonData.hasError)
                {
                    var errors = '';
                    for(var error in jsonData.errors)
                        //IE6 bug fix
                        if(error !== 'indexOf')
                            errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
                    alert(errors);
                }
                // Here you can add code to display the correct updating of field
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                if (textStatus !== 'abort')
                    alert("TECHNICAL ERROR: unable to save message \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
            }
        });
}

然后覆盖OrderOpcController,复制所有init方法并更改代码行,如下所示:

class OrderOpcController extends OrderOpcControllerCore
{
    public function init()
    {
        // parent::init(); // comment or delete this line
        FrontController::init(); // Very important!
        // Then in this switch `switch (Tools::getValue('method'))` add your case
        /* ... */
        case 'updateDelDate':
            if(Tools::isSubmit('deldate'))
            {
                $deldate = urldecode(Tools::getValue('deldate'));

                // Here you must parse and check data validity (I leave to you the code)
                /* ... */

                // Assign the data to context cart
                $this->context->cart->deldate = $deldate;
                // Save information
                $this->context->cart->update();
                $this->ajaxDie(true);
            }
        break;
        /* ... */
    }
}

显然,有必要覆盖OrderCartPaymentModule

PS:我希望我没有忘记任何事情。

答案 1 :(得分:1)

答案 2 :(得分:0)

在Order类

的覆盖中尝试此操作
$order = new Order();
var_dump(ObjectModel::getDefinition($order));exit;

需要Cache :: clean,因为getDefinition尝试从缓存中检索,而缓存是在父::__构造

上没有覆盖的情况下设置的

然后我尝试创建一个新的空Order并获取定义字段并显示在那里,所以它应该保存到mysql

X of Y stars