This CodeLet allows to add products to cart from code. The current product uses an extra text field. The cart view has been modified to accomodate extra fields from the product

Drupal commerce Domain look up search
CodeLet
<?php
/**
 * domain_lookup_submit_wishlist
 *
 * @param unknown $form
 * @param unknown $form_values
 * @author DrupalD
 */
function domain_lookup_submit_wishlist($form, $form_values) {
  global
$user;

//   $am__selected_domain = array_filter($form_values['values']['avilable_domain']);
//   foreach ($am__selected_domain as $domain) {
//     db_query("INSERT INTO domain_register VALUES(id, :domain, :user)", array(':domain' => $domain, ':user' => $user->uid));
//     drupal_set_message(t('!domain was added to your wishlist', array('!domain' => $domain)));
//   }

  // Add selected domains to cart and check out
 
 
$as__domain_tld = array('nl', 'co', 'info', 'net', 'org', 'edu', 'co.uk', 'de', 'eu', 'be', 'tv', 'nu', 'xxx', 'com');
 
 
$am__selected_domain = array_filter($form_values['values']['avilable_domain']);
 
$ss__domain = $form_values['values']['domain_string'];
 
$am__selected_tld = array_keys($am__selected_domain);
 
 
$am__product_id = db_query('SELECT product_id, sku FROM {commerce_product} WHERE sku IN (:sku)', array(':sku' => $am__selected_tld))->fetchAll();
  foreach (
$am__product_id as $om__product_id) {
   
//Load the product using the product id
   
$product = commerce_product_load($om__product_id->product_id);
   
   
//Prepare line item
   
$data['context'] = array();
   
$line_item = commerce_product_line_item_new($product, 1, 0, $data);
   
   
//Add line itme to cart
   
$line_item = commerce_cart_product_add($user->uid, $line_item);
   
   
//Prepare the domain name and associate with current line item
   
$am__domain_data = array(
       
':entity_type' => 'commerce_line_item',
     
':bundle' => 'product',
     
':entity_id' => $line_item->line_item_id,
     
':revision_id' => $line_item->line_item_id,
     
':language' => 'und',
     
':domain_name' =>  $ss__domain .'.'. drupal_strtolower($om__product_id->sku),        
    );
   
db_query('INSERT INTO {field_data_field_domain_name} VALUES(:entity_type, :bundle, 0, :entity_id, :revision_id, :language, 0, :domain_name, NULL)', $am__domain_data);
  }
 
drupal_goto('cart');
}

//Only the function which adds products to cart, has been used here.
?>
Info file details
name = Domain look up
description = Lookup for avaialble domain. Created by DrupalD
dependencies[] = libraries
dependencies[] = commerce
dependencies[] = commerce_cart
core = 7.x
Install file details
<?php
/**
 * @file
 *
 * Create schema for user's choise for domain
 *
 * @author DrupalD
 *
 */


/**
 * Impelementation of hook_scheama
 */
function domain_lookup_schema() {
 
$schema = array();
 
$schema['domain_register'] = array(
     
'description' => t('Store domains that users whish to buy'),
     
'fields' => array(
         
'id' => array(
             
'type' => 'serial',
             
'length' => 11,
             
'not null' => TRUE,
          ),
         
'domain_name' => array(
             
'type' => 'varchar',
             
'length' => 255,
             
'not null' => TRUE,
          ),
         
'uid' => array(
             
'type' => 'int',
             
'length' => 11,
             
'not null' => TRUE,
          ),
      ),
     
'primary key' => array('id'),
  );
  return
$schema;
}
?>
Submitted by DrupalD on