如何通过在shopping-cart.tpl中创建变量来修改PaymentModule.php中$ order-> id_customer的值?

如何通过在shopping-cart.tpl中创建变量来修改PaymentModule.php中$ order-> id_customer的值?

问题描述:

I have the following code in shopping-cart.tpl:

    $(document).ready(function(){
        $.ajax({
            url: document.location.origin+"/univers/themes/leostyl/shopping-cart.php",
            type: 'get',
            success: function(data){
                var array = $.parseJSON(data);
               ch='<select class="form-control" id="customer-id" onchange="myFunction()">';
               for (var i=0;i<array['results'].length;i++) {
                if(array['results'][i].id_default_group== 3)
                  ch=ch+'<option id='+array['results'][i].id_customer+'> '+array['results'][i].firstname+' '+array['results'][i].lastname+'</option>';
                  }
                  ch=ch+'</select>';
                  $( ".customer" ).append(ch);                    
            },
            error: function (xhr, ajaxOptions, thrownError) {
              }
        });

    });

how can i modify the value of $order->id_customer in PaymentModule.php by creating a variable in shopping-cart.tpl?

$order->id_customer = (int)$this->context->cart->id_customer;

you can't change it from a simple assignement in a template. The customer id is retrieved from the context (Cookie / Session) in the validateOrder() function...

Assign an order to a different customer is quite dangerous, but if you really need this, I see 2 workarounds :

  • Override the PaymentModule::validateOrder() function to set the customer id you want instead of using the one of the context, assuming you stored it somewhere else before in your process: Cookie, db table
  • Use a hook (actionObjectOrderAddBefore, actionValidateOrder, ...) to set or modify all the order related data in database after validation (be careful to subprocesses like email confirmation that could be sent to the previous customer according to the hook you use), and also assuming you stored the good customer id somewhere before

Good luck