0

How do you get the order information such as user's name, address, e-mail and products he ordered, on the checkout complete page on Drupal Commerce for Drupal 7?

1
0

I've managed to complete this task. First, you must get the order id from session or database using query. Second, you must create an $order object like this

$order = commerce_order_load($orderId);

Define a new order sdtClass

$myOrder = new stdClass();

Wrap the order

$orderWrapper = entity_metadata_wrapper('commerce_order', $order);

And then you can get various order info like this:

// Get order number
$orderNumber = $order->order_number;

// Get user's e-mail address and encode properly encode it
$orderMail = drupal_json_encode($user->mail);

// Prepare an array to store products
$orderPurchasedItems = array();
// Create a foreach to loop through all the products and get the products info
foreach ($items as $item) {
    // Checks if the value is equal to product
    if ($item->type->value() === 'product') {
        // Get the unit price value per product
        $commerceUnitPrice = $item->commerce_unit_price->value();
        $entity = $item->value();
        if ($entity) {
            $orderPurchasedItems[] = array(
                "id" => $entity->nid,
                "quantity" => $item->quantity->value(),
                "price" => commerce_currency_format(
                    $commerceUnitPrice['amount'],
                    $commerceUnitPrice['currency_code']
                ), // this will result in $10
            );
            // Store all the ordered products in a new variable so we can use it later
            $myOrderProducts = $orderPurchasedItems;
        }
    }
}

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.