joaofelippe
joaofelippe

Reputation: 59

prestashop 1.6 attribute as textfield

im making a customizable product to sell in prestashop, its a pepper sauce where you can fully customize at your own taste... Im doing good and the attributes options in prestashop are good to set drop down select fields and outers stuff, but, i cant simple add a textbox! im trying to find a solution, but its complicated...

http://pimentaemcasa.com.br/home/8-pimenta-personalizada.html

you guys can see in the middle the custom options, the last one is the sauce´s name, its a textbox but after the user clicks in "comprar" (add to cart) it goes nowhere, i need to post it together with the outer attributes, for now its just a textfield alone that send his value nowhere... anyone can help me attach the name with the order?

(the prestashop customize option in products let you put a textbox, buts its also needs that you hit "save" in the name before you hit "add to cart", awful, hey prestashop team, count the taps! ;p)

thaaanks!

Upvotes: 2

Views: 1989

Answers (1)

tarek fellah
tarek fellah

Reputation: 367

I use the textfield customized data in this example, you should create a customized input data (called nickname in this example) for your product. You can add a textbox and send its value to the ajax-cart.js in blockcart module,

in

add : function(idProduct, idCombination, addedFromProductPage, callerElement, quantity, whishlist)

you can add your input for example

var nickname = $('input[name=nickname]').val();

and send the value in the ajax call

$.ajax({
        type: 'POST',
        headers: { "cache-control": "no-cache" },
        url: baseUri + '?rand=' + new Date().getTime(),
        async: true,
        cache: false,
        dataType : "json",
/*************** added by TAREK ******************************/         
data: 'controller=cart&add=1&ajax=true&qty=' + ((quantity && quantity != 
null) ? quantity : '1') + '&id_product=' + idProduct
 +'&nickname='+nickname+ '&token=' + static_token + ( 
(parseInt(idCombination) && idCombination != null) ? '&ipa=' + 
parseInt(idCombination): ''),
/*************** added by TAREK ******************************/         

in cartController.php (override), in processChangeProductInCart function, after if (!$this->errors && $mode == 'add') you should the code that add the customized data value in the database

$authorized_text_fields = array('nickname');
    foreach ($_POST as $field_name => $value)
        if (in_array($field_name, $authorized_text_fields) && $value != '')
        {
            if (!Validate::isMessage($value))
                $this->errors[] = Tools::displayError('Invalid message');
            else {
                $this->context->cart->deleteCustomizationToProduct((int)$this->id_product, 'nickname');
                $this->context->cart->addTextFieldToProduct($this->id_product, 'nickname', Product::CUSTOMIZE_TEXTFIELD,Tools::getValue('nickname')); 
            }
        }
        else if (in_array($field_name, $authorized_text_fields) && $value == '') 
            $this->context->cart->deleteCustomizationToProduct((int)$this->id_product, 'nickname');

Hope this helps, Cordially.

Upvotes: 1

Related Questions