1
Answers
Vote up!
0
Vote down!

Deleting a Line Item Programmatically

Hi

I am getting the error message below when trying to use commerce_cart_order_product_line_item_delete in the commerce_cart_line_item_refresh hook.

EntityMetadataWrapperException: Unable to get the data property type as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 442 of C:\repo\docroot\profiles\commerce_kickstart\modules\contrib\entity\includes\entity.wrapper.inc).

I have also included my code below. Please help?

function htconference_commerce_cart_line_item_refresh($line_item, $order_wrapper) {
  //dprint_r($line_item);
  if (isset($line_item->field_discount_flag)) {
    /*
     * Check if a discount has been applied (this occurs if the product has a discount applied
     * to the field_discount line item attribute (see below)
     */
    if ($line_item->field_discount_flag[LANGUAGE_NONE][0]['value'] ==  5){
      $qty = $line_item->quantity;
      $price = $line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'] - $line_item->field_discount_flag[LANGUAGE_NONE][0]['value']*100;

      $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'][0]['price']['amount'] = $price;
      $line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'] = $price;
      $line_item->commerce_total[LANGUAGE_NONE][0]['data']['components'][0]['price']['amount'] = $qty*$price;
      $line_item->commerce_total[LANGUAGE_NONE][0]['amount'] = $qty*$price;
      commerce_line_item_save($line_item);
      entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id));
    }
  }

  //Remove discount flag product and set new product with discount field_discount attribute set to 5
  //htconference_product_add(1);
  $line_item_new = '';
  //dprint_r($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
  global $user;
  // Load the order and empty the cart
  $order = commerce_cart_order_load($user->uid);
  //commerce_cart_order_empty($order);

  if($line_item->quantity != 0){
    switch ($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']){
      case WORKSHOP_TUES_MORN_1_DISC:
        //dprint_r($line_item);
        commerce_cart_order_product_line_item_delete($order, $line_item->line_item_id);
        //$line_item->quantity = 0;
        //$line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'] = $price;
        $line_item_new = commerce_cart_product_add_by_id(WORKSHOP_TUES_MORN_1);
        $line_item_new->field_discount_flag[LANGUAGE_NONE][0]['value'] = 5;
        commerce_line_item_save($line_item_new);
        entity_get_controller('commerce_line_item')->resetCache(array($line_item_new->line_item_id));
        break;
      case WORKSHOP_TUES_MORN_2_DISC:
        commerce_cart_order_product_line_item_delete($order, $line_item->line_item_id);
        $line_item_new = commerce_cart_product_add_by_id(WORKSHOP_TUES_MORN_2);
        $line_item_new->field_discount_flag[LANGUAGE_NONE][0]['value'] = 5;
        commerce_line_item_save($line_item_new);
        entity_get_controller('commerce_line_item')->resetCache(array($line_item_new->line_item_id));
        break;
      case WORKSHOP_TUES_AFT_1_DISC:
        commerce_cart_order_product_line_item_delete($order, $line_item->line_item_id);
        $line_item_new = commerce_cart_product_add_by_id(WORKSHOP_TUES_AFT_1);
        $line_item_new->field_discount_flag[LANGUAGE_NONE][0]['value'] = 5;
        commerce_line_item_save($line_item_new);
        entity_get_controller('commerce_line_item')->resetCache(array($line_item_new->line_item_id));
        break;
      case WORKSHOP_TUES_AFT_2_DISC:
        commerce_cart_order_product_line_item_delete($order, $line_item->line_item_id);
        $line_item_new = commerce_cart_product_add_by_id(WORKSHOP_TUES_AFT_2);
        $line_item_new->field_discount_flag[LANGUAGE_NONE][0]['value'] = 5;
        commerce_line_item_save($line_item_new);
        entity_get_controller('commerce_line_item')->resetCache(array($line_item_new->line_item_id));
        break;
    }
  }
  entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id));
}
Asked by: obs
on June 18, 2013

1 Answer

Vote up!
0
Vote down!

I worked out that the issue was being caused as I had a redirect to shopping cart rule 'After adding a product to the cart'. This was causing the above error in my particular setup which had a number of custom rules which were adding products to an order on the fly depending on the line item criteria in a product added by a visitor.

I suspect this might have been causing some conflict in the order of events and the redirect to the shopping cart at the same time. I guess weighting the rules might have corrected the issue.

I just disabled my redirect to shopping cart rule assuming that it is non-standard behaviour with unexpected results.

Answer by: obs
Posted: Jun 24, 2013