예제 #1
0
 /**
  * Tests ability to expose line item fields on the add to cart form.
  */
 public function testExposedLineItemFields()
 {
     /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $line_item_form_display */
     $line_item_form_display = EntityFormDisplay::load('commerce_line_item.product_variation.add_to_cart');
     $line_item_form_display->setComponent('quantity', ['type' => 'number']);
     $line_item_form_display->save();
     // Get the existing product page and submit Add to cart form.
     $this->postAddToCart($this->variation->getProduct(), ['quantity[0][value]' => 3]);
     // Check if the quantity was increased for the existing line item.
     $this->cart = Order::load($this->cart->id());
     $line_items = $this->cart->getLineItems();
     /** @var \Drupal\commerce_order\Entity\LineItemInterface $line_item */
     $line_item = $line_items[0];
     $this->assertEqual($line_item->getTitle(), $this->variation->getLineItemTitle());
     $this->assertTrue($line_item->getQuantity() == 3, t('The product @product has been added to cart.', ['@product' => $line_item->getTitle()]));
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function addLineItem(OrderInterface $cart, LineItemInterface $line_item, $combine = TRUE, $save_cart = TRUE)
 {
     $purchased_entity = $line_item->getPurchasedEntity();
     $quantity = $line_item->getQuantity();
     $matching_line_item = NULL;
     if ($combine) {
         $matching_line_item = $this->lineItemMatcher->match($line_item, $cart->getLineItems());
     }
     $needs_cart_save = FALSE;
     if ($matching_line_item) {
         $new_quantity = $matching_line_item->getQuantity() + $quantity;
         $matching_line_item->setQuantity($new_quantity);
         $matching_line_item->save();
     } else {
         $line_item->save();
         $cart->addLineItem($line_item);
         $needs_cart_save = TRUE;
     }
     $event = new CartEntityAddEvent($cart, $purchased_entity, $quantity, $line_item);
     $this->eventDispatcher->dispatch(CartEvents::CART_ENTITY_ADD, $event);
     if ($needs_cart_save && $save_cart) {
         $cart->save();
     }
     return $line_item;
 }