Jump to content

How to attach custom text (note or message) to product


3cubes

Recommended Posts

Hi everyone,

 

how can I attach a custom text to a product which will be added into the cart along with the product itself when adding it to cart and furthermore beeing passed through the whole order process like in order page in backoffice or on pdf invoices and so on??

 

I know there are the built-in customization fields but they are not a solution for me because my custom text is not entered by customer. Instead it's gonna be generated by some javascript on product page from a product configuration wizard.

 

Any ideas would be appreciated

Link to comment
Share on other sites

Hi thanks for reply but in fact such funcionality is very rare in prestashop. In Magento you can even pass a custom price to the cart when adding a product to cart. And there are a lot of tutorials on that on the web. But in presta...good luck!

Link to comment
Share on other sites

Hi everyone,

 

how can I attach a custom text to a product which will be added into the cart along with the product itself when adding it to cart and furthermore beeing passed through the whole order process like in order page in backoffice or on pdf invoices and so on??

 

I know there are the built-in customization fields but they are not a solution for me because my custom text is not entered by customer. Instead it's gonna be generated by some javascript on product page from a product configuration wizard.

 

Any ideas would be appreciated

Think about a hidden customization. It can be saved automatically by JavaScript/AJAX.

Link to comment
Share on other sites

Hey Tuk66,

 

thanks for your reply. I already know a solution like your suggestion. But I really would like to get this done without the use of the built-in customization fields.

 

So far I have created a custom module which fetches and saves all my custom data in a custom table in the database once a product is added to cart.

 

My custom table is assigned to product which has been added to cart. So I have the right values for

 

id_cart, id_shop, id_product, id_product_attribute along with my customtext. Everything is now fine in the databse.

 

So I think there must be a simple way to show this customtext in the cart and in the order and in invoices as long as every customtext is always assigned to a single product/cart association!?

Link to comment
Share on other sites

Hi 3cubes,

 

Probably have to add the custom message to the smarty assigned vars list in blockcart.php

        $this->smarty->assign(array(

            'customizedMessage', $customizedMessage,
            'products' => $products,
            'customizedDatas' => Product::getAllCustomizedDatas((int)($params['cart']->id)),
            'CUSTOMIZE_FILE' => _CUSTOMIZE_FILE_,
            'CUSTOMIZE_TEXTFIELD' => _CUSTOMIZE_TEXTFIELD_,
            'discounts' => $cart_rules,
            'nb_total_products' => (int)($nbTotalProducts),
            'shipping_cost' => $shipping_cost,
            'shipping_cost_float' => $shipping_cost_float,
            'show_wrapping' => $wrappingCost > 0 ? true : false,
            'show_tax' => (int)(Configuration::get('PS_TAX_DISPLAY') == 1 && (int)Configuration::get('PS_TAX')),
            'wrapping_cost' => Tools::displayPrice($wrappingCost, $currency),
            'product_total' => Tools::displayPrice($params['cart']->getOrderTotal($useTax, Cart::BOTH_WITHOUT_SHIPPING), $currency),
            'total' => Tools::displayPrice($totalToPay, $currency),
            'order_process' => Configuration::get('PS_ORDER_PROCESS_TYPE') ? 'order-opc' : 'order',
            'ajax_allowed' => (int)(Configuration::get('PS_BLOCK_CART_AJAX')) == 1 ? true : false,
            'static_token' => Tools::getToken(false)
        ));

 

 

 

then in blockcart.tpl add something like this: (For example just above line  <!-- Customizable datas -->   )

            {if isset($customizedMessage)}

                    {l s="some title or header of customized message" mod="blockcart"}{$customizedMessage}

            {/if}
 

similar changes for the cart summary can be made.

See if this helps you and where you get stuck. Maybe we can help you more to the point.

 

pascal

Link to comment
Share on other sites

Hi PascalVG,

 

thank you very much for your reply. I just tried the steps you mentioned but it doesn't work for me somehow!? Maybe I am overseeing something?

 

I just deinstalled my module and put my custom functionality into Cart.php by overriding it. As soon I have things work out I will adapt solutions back into my module. So for now it is working like this:

 

1. When clicking adding to cart, product is beeing added to cart and along with that a customtext is beeing passed to Cart.php and is fetched there by Tools::getValue('customtext') and assigned to a variable like $customtext. Value is then beeing saved in a custom row I created within ps_cart_product.

 

2. Now I edit blockcart.php and assign smarty variable: 'customtext' => $customtext like you mentioned above

3. I edit blockcart.tpl and insert {$customtext} somewhere after product title

 

--> Nothing shows up!

 

Would really appreciate any further assistance to get this done work.

 

Thank you very much in advance

Link to comment
Share on other sites

In blockcart.php, is the variable $customtext already filled with the correct value? i.e., do you fetch the database value somewhere within the blockcart.php already?

 

Try for a moment to see if the $customtext has contents using an echo or so:

 

For example, just before you assign the smarty variable:

 

echo '$customtext ='.$customtext;

and see if you see correct contents on the screen (at the top) when reloading the page.

 

pascal

Link to comment
Share on other sites

I even tried a sql query within blockcart.php like this:

$sql = 'SELECT(`customtext`)
		FROM `'._DB_PREFIX_.'cart_product`
		WHERE `id_product` = '.(int)$id_product.'
		AND `id_product_attribute` = ' . (int)$id_product_attribute . '
		AND `id_cart` = '.(int)$id_cart;
		$customtext = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);

and made the smarty assign with 'customtext' => $customtext

 

Still no value when I echo $customtext on page!??

Link to comment
Share on other sites

Hi 3cubes,

 

I don't expect you need to use SQL directly in blockcart.php.

 

If you set it up correctly, I'd expect the customtext to be an integral part of your current Cart object, something like Cart->customText or so.

 

So it depends a little how you added/integrated this customText into your Cart.php, if it is retrieved from the database automatically when loading the Cart object etc.

 

Maybe attach your Cart.php with the changes...

What PS version do you use?

 

pascal.

Link to comment
Share on other sites

Hi Pascal,

 

sorry for not providing the essential information. I am using Prestashop Version 1.5.6.2

 

In Cart.php in

public function updateQty()

I use this to get value of customtext

$customtext = Tools::getValue('customtext');

Then when product is going to be added to cart within the same function there is a sql query to insert all product data in _cart_product table. Here I just insert my variable like this in the database

$result_add = Db::getInstance()->insert('cart_product', array(
					'id_product' => 		(int)$id_product,
					'id_product_attribute' => 	(int)$id_product_attribute,
					'id_cart' => 			(int)$this->id,
					'id_address_delivery' => 	(int)$id_address_delivery,
					'id_shop' => 			$shop->id,
					'quantity' => 			(int)$quantity,
					'date_add' => 			date('Y-m-d H:i:s'),
					'customtext' =>		        $customtext,
				));

I just dont know where else to fetch my custom data as it is sent to controller when pressing "add_to_cart" button on product page. So I think here in updateQty() function is best place to fetch that data and insert into table. If you have any better idea I would like to read it :)

Edited by 3cubes (see edit history)
Link to comment
Share on other sites

I'm really stucked on this issue. Is there anybody who maybe solved something like this?

 

I mean right now I have everything I need: there is my custom value which I am fetching and it is successfully stored into the database along with the product and the cart it belongs to.

 

So where have I to read and assign this variable to smarty to be able to show it in template files like in blockcart.tpl or shopping-cart-product-line.tpl ???

Edited by 3cubes (see edit history)
Link to comment
Share on other sites

3Cubes,

 

Re-reading the whole thread, I think we're on the wrong path. You said in the original post the custom Message you will add to the product. So that means you can have more custom messages per cart/order, right, as it is a message per product, yes?

 

So the customMessage we should instead save in the PS_product_lang database table. Is that what you have done indeed?

No changes are then needed to cart table and Cart.php file, as it has nothing directly to o with the cart, but with the products within the cart...

 

 

After that, the custom message is an integral part of the product object, so should be available as such with

$myCurrentProduct->customMessage (or whatever you called it exactly)

 

So, when you have your current product available, the customMessage should be available as well... I expect the products to be available in the shopping cart, summary, Email etc. so that should work then...

 

pascal

Link to comment
Share on other sites

Hi Pascal,

 

thank you for your time you are investing to helping me out. I really appreciate it. But I really think that we are on the wrong path. So let me please explain my situation a little more specific for a better understanding:

  1. When customer is on my product page he has the opportunity to "customize" the product wih the help of a configuration wizard I have built with javascript/jquery.
  2. At the end of this process customer can add customized product to cart which now has a custom price and a custom "ordertext" for me as the shop-owner to get alle the customization information
  3. When clicking on add-to-cart-button product is added to cart with new customprice and customtext
  4. I fetch these custom datas in cart.php during the add-to-cart process and insert them to ps_cart_product table as every product in the cart can theoritically have a customprice and a customtext
  5. So now that these custom values are saved in the database correctly I would like to intergrate them through the rest of the order process like in the blockcart, in cart summary, in orders (backoffice), invoices and so on...
  6. My customprice is working fine through the whole process as it is recalculated by an override of the priceStatic function...I am just stucked on this issue with my customtext. I dont get it assigned to show in cart and orders and invoices.

I hope this was clear enough for a better understanding. I really would appreciate any further assistance to get this challenge done.

 

Best regards

Edited by 3cubes (see edit history)
Link to comment
Share on other sites

Hi 3cubes,

Thanks for the more detailed info. So if I understand well, the customized message is now saved and well in the ps_cart_product database table and you want  to use it further.

 

Let's start with blockcart, to see if we're on the right path.

in blockcart.php, the products that are currently in the cart are requested by using the cart->getProducts() function. So, to get the customized message, we need to change this function, to include this ps_cart_product message when requesting the list of cart products.

 

So, looking in Cart.php, we need to modify the SELECT statement of the sql that retrieves the table info: (Let's call the message field: "customCartProductMessage". In your own SQL, replace with the real name you gave it) (N.B. Make backup of all files you modify, just in case...) (N.B. Sample code from PrestaShop 1.5.6.2): Add the red code

 

    public function getProducts($refresh = false, $id_product = false, $id_country = null)
                 ...

        // Build query
        $sql = new DbQuery();

        // Build SELECT
        $sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, cp.`customCartProductMessage` as customCPMsg, pl.`name`, p.`is_virtual`,
                        pl.`description_short`, pl.`available_now`, pl.`available_later`, p.`id_product`, product_shop.`id_category_default`, p.`id_supplier`,
                        p.`id_manufacturer`, product_shop.`on_sale`, product_shop.`ecotax`, product_shop.`additional_shipping_cost`,
                        product_shop.`available_for_order`, product_shop.`price`, product_shop.`active`, product_shop.`unity`, product_shop.`unit_price_ratio`,
                        stock.`quantity` AS quantity_available, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, p.`weight`,
                        p.`date_add`, p.`date_upd`, IFNULL(stock.quantity, 0) as quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category,
                        CONCAT(LPAD(cp.`id_product`, 10, 0), LPAD(IFNULL(cp.`id_product_attribute`, 0), 10, 0), IFNULL(cp.`id_address_delivery`, 0)) AS unique_id, cp.id_address_delivery,
                        product_shop.`wholesale_price`, product_shop.advanced_stock_management, ps.product_supplier_reference supplier_reference');

        // Build FROM
        $sql->from('cart_product', 'cp');

 

 

 

Then, in blockcart.php, we could use the custom message, as we can reach it for example using the $product variable. You see this code :

    public function assignContentVars($params)

                 ...

        $products = $params['cart']->getProducts(true);  // Here we got the carts products info
        $nbTotalProducts = 0;
        foreach ($products as $product) //here each $product has the customCPMsg field (may be empty though)

                 ...

 

 

(so, to get the custom message in blockcart.php, we could use:

                 $customCPMsg = $product['customCPMsg']  )

 

 

But, as we have  the whole {$products} available in blockcart.tpl (the smarty variable is set in blockcart.php:

        $this->smarty->assign(array(
            'products' => $products,
     // here the smarty variable {$products} is defined and can be used in blockcart.tpl)

 

 

We can directly jump to blockcart.tpl, without any changes in blockcart.php. The customCPMsg is automatically 'forwarded' to the smarty variable, through $products.

 

So now we jump to blockcart.tpl and find the code where we traverse through all products that are added to the cart:

        <dl class="products">
        {foreach from=$products item='product' name='myLoop'}

                 ...

                     inside this foreach block, to get to our custom message, we can now use:

           <span class="blockcart_custom_message">{$product.customCPMsg{</span>

 

 

finally, to 'decorate' the custom message, we can add some css code to themes/<your theme folder>/css/global.css

and add (at the end or so):

.blockcart_custom_message { font-size:14px; }

 

and add some useful css code here, like fontsize, font colour or a border etc.

 

Well, let's try this first, and see if this works for you and if this is what you want. If so, we can go further on the path to Orders, Invoices etc.

 

 

pascal.

Link to comment
Share on other sites

Andgna,

What kind of message do you need? For an order, an individual product, shipping, extra comment on packaging, etc. etc. so many possibilities, you need to be more specific.

So please check if it is similar to what we're talking about in this thread, otherwise maybe open a new topic so people can answer specifically on your question.

 

Pascal

Link to comment
Share on other sites

  • 1 year later...

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...