Jump to content

Edit History

Prestafan33

Prestafan33

Thank you very much, guys. You've helped me a lot, even when your solutions were not fully working in my case, because you forgot some part explained at link from first message about 1.6 solution:

We need also to override ProductController.php. The code explained at link is for PS 1.6, but we can adapt it to 1.7 version pretty easy. In my case, I made this override:

class ProductController extends ProductControllerCore
{
    public function postProcess()
    {
        if (Tools::getValue('ajax') && Tools::isSubmit('submitCustomizedData'))
        {
            // If cart has not been saved, we need to do it so that customization fields can have an id_cart
            // We check that the cookie exists first to avoid ghost carts
            
            if (!$this->context->cart->id && isset($_COOKIE[$this->context->cookie->getName()])) {
                $this->context->cart->add();
                $this->context->cookie->id_cart = (int) $this->context->cart->id;
            }
            $this->pictureUpload();
            $this->textRecord();
            $customization_datas = $this->context->cart->getProductCustomization($this->product->id, null, true);
     
            if($this->errors) {
                $error_list = implode('; ', $this->errors);
                die(Tools::jsonEncode(array('errors' => $error_list)));
            } else {
                die(Tools::jsonEncode(array('success' => true, 'id_customization' => empty($customization_datas) ? null : $customization_datas[0]['id_customization'])));
            }
        }
    }
}

This way, I get id_customization, that is necessary to have ajax cart working, at least in my theme, because it doesn't work (or does, but incorrectly) if you doesn't update #product_customization_id hidden input.

So my AJAX call to save customization without reloading page is:

var customizationContainers = $(".product-customization-item");
customizationContainers.each(function() {
  if ($($(this).find("label")[0]).text().trim()=='Custom pack') {		// My custom field has always this name
    var formActionAttribute_url = $(".product-customization form").attr('action');
    var formActionAttribute_name_field = $(this).find(".product-message").attr("name");
    var data = {};
    data[formActionAttribute_name_field] =  $(this).find(".product-message").val();
    data['submitCustomizedData'] = 1;
    data['ajax'] = 1;
    $.post(formActionAttribute_url, data, null, 'json').done(function(data) {
      $(".product-actions #product_customization_id").val(data.id_customization);
    });
    return false;
  }
});

For simplicity, I'm not taking into account in this code possible errors coming from AJAX data result.

Prestafan33

Prestafan33

Thank you very much, guys. You've helped me a lot, even when your solutions are not fully working in my case, because you forgot some part explained at link from first message about 1.6 solution:

We need also to override ProductController.php. The code explained at link is for PS 1.6, but we can adapt it to 1.7 version pretty easy. In my case, I made this override:

class ProductController extends ProductControllerCore
{
    public function postProcess()
    {
        if (Tools::getValue('ajax') && Tools::isSubmit('submitCustomizedData'))
        {
            // If cart has not been saved, we need to do it so that customization fields can have an id_cart
            // We check that the cookie exists first to avoid ghost carts
            
            if (!$this->context->cart->id && isset($_COOKIE[$this->context->cookie->getName()])) {
                $this->context->cart->add();
                $this->context->cookie->id_cart = (int) $this->context->cart->id;
            }
            $this->pictureUpload();
            $this->textRecord();
            $customization_datas = $this->context->cart->getProductCustomization($this->product->id, null, true);
     
            if($this->errors) {
                $error_list = implode('; ', $this->errors);
                die(Tools::jsonEncode(array('errors' => $error_list)));
            } else {
                die(Tools::jsonEncode(array('success' => true, 'id_customization' => empty($customization_datas) ? null : $customization_datas[0]['id_customization'])));
            }
        }
    }
}

This way, I get id_customization, that is necessary to have ajax cart working, at least in my theme, because it doesn't work (or does, but incorrectly) if you doesn't update #product_customization_id hidden input.

So my AJAX call to save customization without reloading page is:

var customizationContainers = $(".product-customization-item");
customizationContainers.each(function() {
  if ($($(this).find("label")[0]).text().trim()=='Custom pack') {		// My custom field has always this name
    var formActionAttribute_url = $(".product-customization form").attr('action');
    var formActionAttribute_name_field = $(this).find(".product-message").attr("name");
    var data = {};
    data[formActionAttribute_name_field] =  $(this).find(".product-message").val();
    data['submitCustomizedData'] = 1;
    data['ajax'] = 1;
    $.post(formActionAttribute_url, data, null, 'json').done(function(data) {
      $(".product-actions #product_customization_id").val(data.id_customization);
    });
    return false;
  }
});

For simplicity, I'm not taking into account in this code possible errors coming from AJAX data result.

×
×
  • Create New...