I'm trying to substitute cart line items on the checkout page based on a custom input element, say a radio button. The requirement is to refresh the checkout page dynamically to reflect the new line item. Here's my approach:

  • Alter form commerce_checkout_form_checkout
  • Add custom input element with an #ajax setting
  • In ajax callback, substitute order line items using the following:

    $order = commerce_order_load($form_state['order']->order_id);
    $product_replacement = commerce_product_load_by_sku($sku);
    $line_item_replacement = commerce_cart_product_add_by_id($product_replacement->product_id);
    commerce_cart_order_product_line_item_delete($order, $line_item_wrapper->line_item_id->value());
    $form_state['order'] = commerce_order_status_update($order, 'cart', TRUE);
  • Update order contents pane using the following:

    $pane_id = 'cart_contents';
    $checkout_pane = commerce_checkout_pane_load($pane_id);
    if ($callback = commerce_checkout_pane_callback($checkout_pane, 'checkout_form')) {
      $pane_form = $callback($form, $form_state, $checkout_pane, $order);
      if ($pane_form) {
        $form['cart_contents'] = $pane_form + array(
          '#type' => $checkout_pane['fieldset'] ? 'fieldset' : 'container',
          '#title' => check_plain($checkout_pane['title']),
          '#collapsible' => $checkout_pane['collapsible'],
          '#collapsed' => $checkout_pane['collapsed'],
          '#attributes' => array('class' => array($pane_id)),
          '#tree' => TRUE,

Although the order contents pane does refresh to reflect the new product, an error occurs when I click "Continue to next step". Watchdog reports a commerce_order error:

EntityMetadataWrapperException: Unable to get the data property commerce_total as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 442 of /var/www/mysite/sites/all/modules/entity/includes/entity.wrapper.inc).

Any idea what I'm doing wrong, or can you suggest an alternate approach?

  • I have the same problem, do u find solution? – Yusef Mohamadi Feb 12 '14 at 9:01

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.