3

I am using Commerce Userpoints to award points based on purchases and then customers can use those rewards on future purchases for a discount.

I'd like to customize the checkout pane if the user has already applied a discount. Commerce Userpoints Discount creates a line item type of userpoints. My initial idea was to check if the current order contains a line item of type userpoints, but I'm not sure how to do that. Any help would be appreciated!

3

You can check for a certain type of line item on an order using one of the two following methods.

Given an order, let's look for a shipping line item.

// Load your order.
$order = commerce_order_load(/* Order ID */);

Without Metadata Wrappers

if (!empty($order->commerce_line_items)) {
  // Iterate over the line items.
  foreach ($order->commerce_line_items[LANGUAGE_NONE] as $line_item_data) {
    $line_item = commerce_line_item_load($line_item_data['line_item_id']);
    // Check the type.
    if ($line_item->type == 'shipping') {
      // Shipping line item found. Do the things.
    }
  }
}

With Wrappers

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Iteration is a bit simpler here.
foreach ($order_wrapper->commerce_line_items as $line_item) {
  // We have to access it using ->value().
  if ($line_item->type->value() == 'shipping') {
    // Shipping line item found. Do the things.
  }
}

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.