I'm working on D8 site with Drupal Commerce. I have to create a dynamic product variation and add it to cart for the user checkout.

The problem I have, is when you land on the cart page, the line item doesn't have a label (title) it replaced with the price of the item. Please refer to the attached image.

Here's my code:

   //create variation
            $variation = ProductVariation::create([
                'type' => 'fk_payment',
                'sku' => 'FK'.$faid,
                'status' => TRUE,
                'price' => new Price($trans->getTotal(), $trans->getCurrency()),
                'title' => "Example item label title",


            //Load the product
            $product_id = 4;
            $product = Product::load($product_id);

            //load store
            $storeId = $product->get('stores')->getValue()[0]['target_id'];
            $store = \Drupal::entityTypeManager()
            //prepare cart
            $cart = \Drupal::service('commerce_cart.cart_provider');
            $cart = $cart->getCart('default', $store);
            if (!$cart) {
                $cart = \Drupal::service('commerce_cart.cart_provider');
                $cart = $cart->createCart('default', $store);

            // Process to place order programatically.
            $cart_manager = \Drupal::service('commerce_cart.cart_manager');

            //empty the cart
            if (!empty($cart)) {

            $cart_manager->addEntity($cart, $variation);

enter image description here

1 Answer 1


Just found the answer. I have to edit the cart "Views" and change the field "item" to display label. That's it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.