I have a product line item that has an additional field, which is a entity reference and is set to a select list. Any ideas on how to include this on the form?

Comments

spacetaxi’s picture

I'm following up on my own question, hopefully for the help of others.

First, Commerce Examples provides an add to cart form that by default searches both sku and product/name title. I ended up using it as the basis of my own custom module.

Second, in answering my main question, Drupal 7 provides the EntityFieldQuery API which is a pretty straight forward way of getting a list of entities, but would require doing another database query to get field values. There is a useful module, EntityFIeldQuery Extra Fields which will allow you to load these fields with the same query, which I ended up using.

Third, in terms of handing additional product line item fields with the add to cart, you can access these through the line item object, which is similar to accessing fields in a node (or other entity). This is best explained by showing an add to cart example:

function example_add_cart_submit($form, &$form_state) {
  $quantity = $form_state['values']['quantity'];
  
   if ($product = commerce_product_load($form_state['values']['addcart_product']) ) {
        global $user;
        $uid = $user->uid;
        $line_item = commerce_product_line_item_new($product, $quantity);
        $line_item->field_entityreference['und'][]['target_id'] = $form_state['values']['entityreference']; // entity reference field from form
        $line_item = commerce_cart_product_add($uid, $line_item, FALSE);
        
    }    
  
}

Many thanks to the module author for posting this module.