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()

  • "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 Feb 12 '14 at 8:22
  • it's just interesting to me, what is ym() function in your code? – xurshid29 Feb 12 '14 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 :). – Yusef Mohamadi Feb 12 '14 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 – Yusef Mohamadi Feb 12 '14 at 8:31
  • @xurshid29 but it is very old version of my module, I improve it and add more features :) – Yusef Mohamadi Feb 12 '14 at 8:33
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.

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

Your Answer

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

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