Jump to content

Save Prestashop Custom fields on “Add to cart”


hugolin69

Recommended Posts

Hi,

I would like to save the customization fields when I click to add a button, hidding the custom field saving button.

I found this tutorial, but it is for the 1.6 version.

I tried many ways, but I can not manage to make it cleanly, as the page need to refresh with the right id_customization.

Any help ?

Tx

Link to comment
Share on other sites

  • 5 months later...
  • 1 year later...

DID you find the solution for this one?

Whenever customer saves his/her custom feild replies.I dont get to see that on my admin panel.

IT dissappears after adding it to the CART. CUstomisation field value doesnt get saved. Please help

Link to comment
Share on other sites

Hi,

Yes, I found the solution, and it is working well. It save without a save button.

Here is a quick workaround for a field "Personal message" for example.

But your problem seems something else.

But here is a working example to have a custom field, without having to "save the textaerea manually.

 

1°/ You need to create for each product a customization field v"TEXT TYPE"  that you name  exaclty like this "{message_personnel}"

2°/ you override the Class Cart


class CartController extends CartControllerCore
{
    public function init()
    {
        parent::init();

        // Send noindex to avoid ghost carts by bots
        header('X-Robots-Tag: noindex, nofollow', true);

        // Get page main parameters
        $this->id_product = (int)Tools::getValue('id_product', null);
        $this->id_product_attribute = (int)Tools::getValue('id_product_attribute', Tools::getValue('ipa'));
        $this->customization_id = (int)Tools::getValue('id_customization');
        $this->qty = abs(Tools::getValue('qty', 1));
        $this->id_address_delivery = (int)Tools::getValue('id_address_delivery');
        $this->preview = ('1' === Tools::getValue('preview'));
		
		
		
		
		$this->message_personnel = (string)Tools::getValue('message_personnel', null);
		
		if($this->message_personnel != ''){

			$product = new Product((int)$this->id_product);

			if (!$field_ids = $product->getCustomizationFieldIds()) {
				
			}else{
				// 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;
				}
			
				$authorized_text_fields = array();
				foreach ($field_ids as $field_id) {
					if ($field_id['type'] == Product::CUSTOMIZE_TEXTFIELD) {
						$field_id_perso = (int) $field_id['id_customization_field'];
						break;
					}
				}

				$this->context->cart->addTextFieldToProduct($product->id, $field_id_perso, Product::CUSTOMIZE_TEXTFIELD, $this->message_personnel);
				
				$find_customization = Db::getInstance()->executeS(
					'SELECT cu.`id_customization`, cd.`index`, cd.`value`, cd.`type` FROM `'._DB_PREFIX_.'customization` cu
					LEFT JOIN `'._DB_PREFIX_.'customized_data` cd
					ON cu.`id_customization` = cd.`id_customization`
					WHERE cu.id_cart = '.(int)$this->context->cookie->id_cart.'
					AND cu.id_product = '.(int)$product->id.' 
					AND cd.type = \''.(string)Product::CUSTOMIZE_TEXTFIELD.'\'  
					AND cd.value = \''.(string)pSQL($this->message_personnel).'\' 
					AND cd.index = '.(int)$field_id_perso.' 
					AND in_cart = 0'
				);
				
				
				
				$this->customization_id = $find_customization[0]['id_customization'];

			}
            
		}

    }
}

3°/ You add manually a field in product-add-to-cart.tpl with something like that

{if $product.is_customizable && count($product.customizations.fields)}
 <section class="product-message-personnel">
      <div class="card card-block">
            <label>{l s='Personal message' d='Shop.Theme.Catalog'}</label>
            <textarea placeholder="{l s='Your message here' d='Shop.Forms.Help'}" class="message-personnel" maxlength="40000" name="message_personnel"></textarea>
    	</div>
  </section>
  {/if}
  

4°/ In product-customization.tpl you do something like that (It is a quick dirty example) that you could use in order-confirmation-table.tpl and cart-detailed-product-line.tpl

{if $field.label == '{personnel}'}
	<label>{l s='Personal message' d='Shop.Theme.Catalog'}</label>
{else}
	<label>{$field.label}</label>
{/if}
                

 

  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...

I defined the question wrong way.

How you are going to get the message_personnel field value in the cart controller if you are using ajax cart feature. I mean you are not sending that data while using ajax.

Link to comment
Share on other sites

On 7/4/2020 at 3:23 PM, J.Sahu said:

I defined the question wrong way.

How you are going to get the message_personnel field value in the cart controller if you are using ajax cart feature. I mean you are not sending that data while using ajax.

The "message_personnel" field is near the "qty" field, that is natively sent by ajax when pushing add to cart. After that, I have the override of the Cart Class.

Which point do you miss ?

  • Thanks 1
Link to comment
Share on other sites

  • 5 weeks later...
  • 9 months later...
  • 7 months later...

@hugolin69 Works really nice, awesome job friend! 😀


(Added some keywords below so if people google search this, it will perhaps be easier for them to find)
Prestashop 1.7 save customization cart remove custom button direct without click add combination

Edited by Kimosabe
added some keywords so if people google search this, it will be easier for them to find (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...