0

I need to create pdf of user's cart items with custom header & footer. I decide to create custom module in which I can get cart items details. But I have no idea about how to do this? I have tried:

$store_id = 1;
$order_type = 'default';
$cart_manager = \Drupal::service('commerce_cart.cart_manager');
$cart_provider = \Drupal::service('commerce_cart.cart_provider');
$entity_manager = \Drupal::entityManager();
$store = $entity_manager->getStorage('commerce_store')->load($store_id); 
$cart = $cart_provider->getCart($order_type, $store);
foreach ($cart-> getItems() as $order_item) {
    foreach($order_item->getPurchasedEntity() as $purchased_entity){
        print_r($purchased_entity);
    }    
}

But it gives an array like

Drupal\commerce_product\Entity\ProductVariation Object
(
[values:protected] => Array
    (
        [variation_id] => Array
            (
                [x-default] => 432
            )

        [type] => Array
            (
                [x-default] => product_variations
            )

        [uuid] => Array
            (
                [x-default] => 87d27a77-4bc1-40ff-b3cf-ad3e949ce1b3
            )

        [langcode] => Array
            (
                [x-default] => en
            )

        [status] => Array
            (
                [x-default] => 1
            )

        [uid] => Array
            (
                [x-default] => 9
            )

        [product_id] => Array
            (
                [x-default] => 65
            )

        [sku] => Array
            (
                [x-default] => xx-uua
            )
        [price] => Array
            (
                [x-default] => Array
                    (
                        [number] => 22.000000
                        [currency_code] => USD
                    )

            )
        [field_order_no] => Array
            (
                [x-default] => Array
                    (
                        [0] => Array
                            (
                                [value] => PE-5070
                            )

                    )

            )

And many more protected objects. I know I can get quantity, title & price easily using drupal function but how can I get few custom details from above array like field_order_no in above array, I have some unique attribute assigned to variation that I need to display in pdf?

0

As the top line suggests $purchased_entity is of of type "Drupal\commerce_product\Entity\ProductVariation" which is basically an entity.

All entities have a ->get('field_name') method.

So for your example to get 'field_order_no' you would do

$order_no = $purchased_entity->get('field_order_no')->getValue();

You can retrieve the value of any field via the ->getValue() method which returns an array.

  • Thanks! I had spent 6-7 hrs!! – softech May 17 at 11:05

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.