sakeferret
sakeferret

Reputation: 315

How can I programmatically set the value for a field in a Commerce Product custom Line Item Type?

I've set up a product which uses a custom line item type called "Custom Notes" (created through the UI at Configuration > Line Item Types). One of the fields in "Custom Notes", that is exposed at checkout, is a "field_notes" textarea field. It works as intended... in a product display I can add some custom text, click ORDER and the note carries through to checkout.

However, I need to create these orders programmatically. I've gotten to the point where i'm using a hook_menu to submit an order and it works great. The only problem is that I can't seem to set the value for the "field_notes".

// Load whatever product represents the item the customer will be
// paying for and create a line item for it.    
$line_item = commerce_product_line_item_new($product, $quantity, $order->order_id);

// Save the line item to get its ID.
commerce_line_item_save($line_item);


//***this is the part that's not working. trying to set the field_notes field***
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->field_notes->set("Does this work??");


// Save the line item to get its ID.
commerce_line_item_save($line_item);

// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;

// Save the order again to update its line item reference field.
commerce_order_save($order);

What am i doing wrong? Thanks!

Upvotes: 1

Views: 2796

Answers (1)

Favio
Favio

Reputation: 1009

I think it could be because you are not passing the correct line item type to commerce_product_line_item_new. For example, this is how I would do it:

$line_item = commerce_product_line_item_new(
  $product,
  $quantity = 1,
  $order_id = 0,
  $data = array(),
  $line_item_type = 'my_custom_line_item_type'
);

Upvotes: 1

Related Questions