LemonStand Documentation

Allowing customers to provide order item specific information

LemonStand allows you to add item-specific form controls to the product list on the Cart page. Using these controls your customers can customize items. For example, you you can add controls allowing visitors to enter engraving text for each order items.

To implement item specific controls you need to develop a simple module, which will extend the cart item and order item models. We created a module template, which adds two fields to the models - "Add Engraving" and "Engraving Text". You can replace these fields with any other fields, and add new fields to the module. You can read about LemonStand module development in this article.

Download the module template archive: democartdata.zip

Installing the module template

Extract the module template archive to the modules directory of your LemonStand installation.

Before using the module, please rename the module directory. According the module naming rules, the module name should contain a name of your company, and should not contain underscores. After renaming the module directory, rename the democartdata_module.php script (you will find it in the classes directory) - replace the "democartdata" prefix with the name of your module. Inside this script you will find a PHP class declaration which should also be renamed. Its name should match the script name, but could contain capital letters.

Inside the updates directory of the module class you will find an SQL script (1.sql) which adds new fields to the customer shopping cart item (shop_customer_cart_items) and order (shop_order_items) item tables. The custom field should be added to the both of the tables, and their names should match in the both of the tables.

By the naming convention, fields added by third-party modules should have the x_ prefix. You can use any MySQL data types suitable for your fields. Use the tinyint type for checkbox fields.

After updating the 1.sql script, log out from the Administration Area and log in again. At this point LemonStand will create new fields in the database.

Updating the module code

The module classes directory contains the module information class. It defines form columns and form fields. You need to update PHP code in the extend_order_item and extend_order_item_model functions in accordance with the field names you created in the database. Change the column names in the first argument of the define_column() and add_form_field() functions and update the field titles. If you have more or less custom fields than the module template contains, remove or add corresponding define_column() and add_form_field() function calls.

Updating the cart page

LemonStand does not create any controls on the Cart page automatically. You need to update the partial which displays the list of products in the cart manually. If you used the demo store template as a basement for your store, you need to update the shop:cart_table partial.

This partial contains a table which displays items the customer added to the cart. You can the new controls to the first table column, or create a separate table row for each cart item. Below is a code which displays a checkbox and a text field below a name of each cart item.

<label>Add Engraving</label>
<input type="checkbox" value="1" 
  name="item_data[<?= $item->key ?>][x_add_engraving]" 
  <?= checkbox_state($item->get_data_field('x_add_engraving')) ?>/>

<textarea 
  name="item_data[<?= $item->key ?>][x_engraving_text]"><?= h($item->get_data_field('x_engraving_text')) ?></textarea>

Please note that the name attribute of the custom controls has the following format: item_data[key][x_field_name]. To load a value of a custom field from the cart item object use the $item->get_data_field() call, passing the field name as a function parameter.

LemonStand saves custom field values to the database each time when the visitor clicks the Apply Changes button.

Viewing custom data in the Administration Area

In the Administration Area you can view the list of custom fields by clicking an order item on the Order Preview page:

You can also edit custom fields on in the Create/Edit Order page, when you are adding a product to the order.

Adding custom order item data to email notifications

You can alter the order_content compound email variable and add item-specific information provided by your customers to email notifications. To change the existing compound email variables, or create new variables please go to the Settings/Email Templates/Manage compound variables page.

In the order_content variable code you have access to the $items array. Each element of this array contains a reference to the Shop_OrderItem object. This object has all fields which you defined in your custom module. Below is a code example which adds a value of the x_engraving_text field for each order item, below the product name:

<td>
    <?= $item->output_product_name() ?>
    <? if ($item->x_add_engraving): ?>
      <br/>Engraving text: <?= h($item->x_engraving_text) ?>
    <? endif ?>
</td>

Making product-specific custom fields

If you do not need to display custom fields for some products, or if you need the custom fields to be product-specific, you can extend the product model and add some field which value you can set in the Create/Edit Product form in the Administration Area. For example, you can add the "Allows engraving" field and then use a value of this field in the shop:cart_table partial to hide or show the custom fields.

The process of extending the product model and product form is explained on this page. You can download a module template for extending the product model on this page. After adding a field to the product model, you can access it in the shop:cart_table partial. For example, of you added the x_allows_engraving field to the product model you can use it in the following way:

<? if ($item->x_allows_engraving): ?>
  <label>Add Engraving</label>
  <input type="checkbox" value="1" 
    name="item_data[<?= $item->key ?>][x_add_engraving]" 
    <?= checkbox_state($item->get_data_field('x_add_engraving')) ?>/>

  <textarea 
    name="item_data[<?= $item->key ?>][x_engraving_text]"><?= h($item->get_data_field('x_engraving_text')) ?></textarea>
<? endif ?>

Next: Allowing visitors to customize products
Previous: Displaying products on sale
Return to Tips and Tricks

Comments

Ben

Wednesday, June 01, 2011

Could you continue the example in more detail about the 'Making product-specific custom fields' section. I've read through the page that is linked to and I'm none the wiser - I do more front end code than back end and need more details than provided.

Could you show which file the code needs to be added to, what code is to make the example work and where to add it in to the file? I've modified the code example to work as I require it to and all I need to do is enable it only for select products, but I'm lost on this last bit!

Aleksey Bobkov

Wednesday, June 01, 2011

@Ben - I added a link to the module template. The template archive contains a module which adds two fields to the Product model. You should extract the archive to the /modules directory and rename it (it is highly recommended). First, you should rename the module directory name. The new name should reflect the module purpose and include your company name (especially if you are going to share this module). The name should not include spaces or underscores - only Latin characters. Example: benengravingext. After that you should rename the module information class script (in the classes subdirectory): the class name prefix should match the module name - benengravingext_module.php. And after that you should rename the module class name (in the benengravingext_module.php) - it should match the script name: BenEngravingExt_Module.
Look into the extend_product_model() and extend_product_form() methods. They add two fields. If you need only a single new field, delete one of the define_column() and extend_product_form() calls. The first parameter in those calls (x_extra_description, for example) is the database column name.
Now look into the /updates directory. There are 3 files - the version information file and two SQL files which add new columns to the shop_products table. You can delete the SQL files and update the version.dat file - empty and and add the following line:
#1 Added new columns to the products model

Add the 1.sql file and put a SQL query for creating the column into it. For example:
alter table shop_products add column x_allows_engraving tinynit(4);

The column name (x_allows_engraving) should match the first parameter value in the define_column and extend_product_form() calls in the module information class. You can use other column names of course, but they should match - in the database and in the module information class code.

The column types (tinynit(4) in my example) define the way how columns are rendered on the Create/Edit Product form in the Administration Area. tinynit(4) will be rendered as a checkbox. If you need a text field use the varchar type:
alter table shop_products add column x_engraving _text varchar(255);

If you need a textarea, use the text type:
alter table shop_products add column x_engraving _text text;

Luke

Wednesday, September 21, 2011

The 'x_engraving_text' textarea contents are removed if the customer clicks "Apply Changes" to the cart. How do we save the data as part of 'apply changes'?

Aleksey Bobkov

Wednesday, September 21, 2011

@Luke - the text should not disappear if you did everything right. Please submit a question to the forum if you need help.

theo

Sunday, October 09, 2011

i've added the fields as you described and i'm able to populate data following this flow but i'm wondering if i've collected the data already from the customer, is there some way to programmatically add this to the product at the time i'm adding the product to the shopping cart (rather than as part of review cart/checkout flow)

thanks,
theo

Aleksey Bobkov

Sunday, October 09, 2011

@Theo - you can try to assign custom cart data in the shop:onOrderBeforeCreate event handler (http://lemonstandapp.com/docs/shop_onorderbeforecreate_event/). You can assign data to cart items with the following code:

$custom_data = post('item_data', array()); // Here you can generate your custom data instead of using the POSTed array.
foreach ($custom_data as $key=>$data)
Shop_Cart::set_custom_data($key, $data, 'main');

Thank you

Add your comment

Loading form...