4

I want to programmatically add line item of commerce_doscount type to my order, I test below code,it is correctly add a record to line items array but not show in order list even I refresh the order I dont see anything in order table .

$amount = trim($amount);
  $basePrice = commerce_price_field_data_auto_creation();
  $basePrice['amount'] = $amount;
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
if ($amount) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     $line_item = entity_create('commerce_line_item', array(
    'type' => 'commerce_discount',
    'line_item_label' => 'my test',
    'quantity' => 1,
));
    // Wrap the line item and product to easily set field information.
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// Set the unit price on the line item object.
$basePrice = commerce_price_field_data_auto_creation();
$basePrice['amount'] = $amount;
  $line_item_wrapper->commerce_unit_price = $basePrice;
// Save the line item now so we get its ID.
  commerce_line_item_save($line_item);

      //  $line_item = commerce_line_item_save($line_item);
        // Add it to the order's line item reference value.
$order_wrapper->commerce_line_items[] = $line_item;
     //commerce_order_save($wrapper);
      commerce_order_save($order);
$order = commerce_order_load($order->order_id);

        // Recalculate discounts.
      //  ym($order);
        commerce_cart_order_refresh($order);

also I look at discount module and try below code

 module_load_include('inc', 'commerce_discount', 'commerce_discount.rules');
   $amount = trim($amount);
  $basePrice = commerce_price_field_data_auto_creation();
  $basePrice['amount'] = $amount;
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  commerce_discount_add_line_item($order_wrapper,'commerce_discount',$basePrice);

but return me EntityMetadataWrapperException: Unable to get the data property commerce_total as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue()

5
  • "it is correctly add a record to line items array" - if record is not displayed, then either you are wrong and it adds it incorrectly, or there is some kind of cache that prevents you from seeing changes.
    – Mołot
    Commented Feb 12, 2014 at 8:22
  • it's just interesting to me, what is ym() function in your code?
    – xurshid29
    Commented Feb 12, 2014 at 8:23
  • 2
    ym in my short name :). I have tiny developing module to provide some jobs like dsm and some tiny features, sometimes I dont need heavy devel module :).
    – Yuseferi
    Commented Feb 12, 2014 at 8:28
  • @Mołot I dont think the cache is problem, because discount and coupon module do it , I look discount module and want to use it but return me error, I come code in update question
    – Yuseferi
    Commented Feb 12, 2014 at 8:31
  • @xurshid29 but it is very old version of my module, I improve it and add more features :)
    – Yuseferi
    Commented Feb 12, 2014 at 8:33

1 Answer 1

2

Don't reinvent the wheel, here. Discounts provides functions for apply discounts to orders:

// Pass in an $order or $product that has been entity_metadata_wrapper()ed.
// And pass in a discount name.
commerce_discount_fixed_amount(EntityDrupalWrapper $wrapper, $discount_name);

// Same thing, but a percentage.
commerce_discount_percentage(EntityDrupalWrapper $wrapper, $discount_name);

If you must reinvent the wheel, use these functions as examples to guide what you are doing.

3
  • I didn't find this functions in discount module when I ask this question :D .
    – Yuseferi
    Commented Jan 8, 2016 at 7:13
  • Make sure you're using the latest version. Also, check in the commerce_discount.rules.inc file for these functions.
    – nvahalik
    Commented Jan 8, 2016 at 12:02
  • look at the question date, I resolve it in that time, tnx for your attention.
    – Yuseferi
    Commented Jan 9, 2016 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.