3

With Drupal Commerce, when visitor adds a product to his cart, system creates commerce line item instance. The order consists of line items, and these line items have references to particular products. It is possible to omit products and create only line items?

I am generating orders dynamically with my custom code, and I do not need to have products in my database. As I can see, the only missing information in line item is price. It is set for commerce product. Thatswhy I create product entities for now.

But it is possible to set price directly in line items? Then I do not need to create and load products. Line items would be representing the buyed items itself.

For now, I am creating custom line items without products as follows:

<?php 
global $user;
$order_id = commerce_cart_order_id($user->uid);
$order = commerce_order_load($order_id);
$line_item = commerce_line_item_new('custom_line_item', $order_id);
$line_item->commerce_price = array();
$line_item->line_item_label = 'Custom..';
$line_item->commerce_unit_price[LANGUAGE_NONE][0]['currency_code'] = 'EUR';
$line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'] = 100;
//entity_save('commerce_line_item', $line_item);  dpm($line_item, 'item');
commerce_line_item_save($line_item);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Add it to the order's line item reference value.
$order_wrapper->commerce_line_items[] = $line_item;

?>

The problem is, this item is not showing in cart or checkout, and the price is not including in total. It is possible to add "product" to the cart as line item without referencing to commerce product?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.