commerce_product_bundles-8.x-1.0/src/Form/AddToCartBundleForm.php
src/Form/AddToCartBundleForm.php
<?php namespace Drupal\commerce_product_bundles\Form; use Drupal\commerce\Context; use Drupal\commerce_cart\CartManagerInterface; use Drupal\commerce_cart\CartProviderInterface; use Drupal\commerce_cart\Form\AddToCartFormInterface; use Drupal\commerce_order\Resolver\OrderTypeResolverInterface; use Drupal\commerce_price\Resolver\ChainPriceResolverInterface; use Drupal\commerce_store\CurrentStoreInterface; use Drupal\commerce_store\SelectStoreTrait; use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides the order item add to cart bundle form. * * Code was taken from and modified: * @see \Drupal\commerce_cart\Form\AddToCartForm * * Could not extend AddToCartForm because of form save. */ class AddToCartBundleForm extends ContentEntityForm implements AddToCartFormInterface { use SelectStoreTrait; /** * The cart manager. * * @var \Drupal\commerce_cart\CartManagerInterface */ protected $cartManager; /** * The cart provider. * * @var \Drupal\commerce_cart\CartProviderInterface */ protected $cartProvider; /** * The order type resolver. * * @var \Drupal\commerce_order\Resolver\OrderTypeResolverInterface */ protected $orderTypeResolver; /** * The current store. * * @var \Drupal\commerce_store\CurrentStoreInterface */ protected $currentStore; /** * The chain base price resolver. * * @var \Drupal\commerce_price\Resolver\ChainPriceResolverInterface */ protected $chainPriceResolver; /** * The current user. * * @var \Drupal\Core\Session\AccountInterface */ protected $currentUser; /** * The form ID. * * @var string */ protected $formId; /** * AddToCartBundleForm constructor. * * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * @param \Drupal\Component\Datetime\TimeInterface $time * @param \Drupal\commerce_cart\CartManagerInterface $cart_manager * @param \Drupal\commerce_cart\CartProviderInterface $cart_provider * @param \Drupal\commerce_order\Resolver\OrderTypeResolverInterface $order_type_resolver * @param \Drupal\commerce_store\CurrentStoreInterface $current_store * @param \Drupal\commerce_price\Resolver\ChainPriceResolverInterface $chain_price_resolver * @param \Drupal\Core\Session\AccountInterface $current_user */ public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, OrderTypeResolverInterface $order_type_resolver, CurrentStoreInterface $current_store, ChainPriceResolverInterface $chain_price_resolver, AccountInterface $current_user) { parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->cartManager = $cart_manager; $this->cartProvider = $cart_provider; $this->orderTypeResolver = $order_type_resolver; $this->currentStore = $current_store; $this->chainPriceResolver = $chain_price_resolver; $this->currentUser = $current_user; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('entity.repository'), $container->get('entity_type.bundle.info'), $container->get('datetime.time'), $container->get('commerce_cart.cart_manager'), $container->get('commerce_cart.cart_provider'), $container->get('commerce_order.chain_order_type_resolver'), $container->get('commerce_store.current_store'), $container->get('commerce_price.chain_price_resolver'), $container->get('current_user') ); } /** * {@inheritdoc} * @see \Drupal\commerce_cart\Form\AddToCartForm::actions() */ protected function actions(array $form, FormStateInterface $form_state) { $actions['submit'] = [ '#type' => 'submit', '#value' => $this->t('Add to cart'), '#submit' => ['::submitForm'], '#attributes' => [ 'class' => ['button--add-to-cart'], ], ]; return $actions; } /** * {@inheritdoc} * @see \Drupal\commerce_cart\Form\AddToCartForm::setEntity() */ public function setEntity(EntityInterface $entity) { $this->entity = $entity; return $this; } /** * {@inheritdoc} */ public function getBaseFormId() { return $this->entity->getEntityTypeId() . '_' . $this->operation . '_form'; } /** * {@inheritdoc} */ public function getFormId() { if (empty($this->formId)) { $this->formId = $this->getBaseFormId(); /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ $order_item = $this->entity; if ($purchased_entity = $order_item->getPurchasedEntity()) { $this->formId .= '_' . $purchased_entity->getEntityTypeId() . '_' . $purchased_entity->id(); } } return $this->formId; } /** * {@inheritdoc} */ public function setFormId($form_id) { $this->formId = $form_id; return $this; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form = parent::buildForm($form, $form_state); // The widgets are allowed to signal that the form should be hidden // (because there's no purchasable entity to select, for example). if ($form_state->get('hide_form')) { $form['#access'] = FALSE; } return $form; } /** * {@inheritdoc} * @see \Drupal\commerce_cart\Form\AddToCartForm::submitForm() */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ $order_item = $this->entity; /** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */ $purchased_entity = $order_item->getPurchasedEntity(); // Set ref. PV and quantity on bundle order item. $selected_bundle_variations = reset($form_state->getValue('purchased_entity')); $ref_eck = []; if(isset($selected_bundle_variations['bundle_variations_ref_options'])){ foreach ($selected_bundle_variations['bundle_variations_ref_options'] as $product_id => $selected_bundle_variation){ $ref_eck[] = [ 'product_var_id' => $selected_bundle_variation, 'quantity' => isset($selected_bundle_variations['quantity'][$product_id]) ? $selected_bundle_variations['quantity'][$product_id] : 1 ]; } } $order_item->set('field_product_variation_ref', $ref_eck); $cart = $order_item->getOrder(); if (!$cart) { $order_type_id = $this->orderTypeResolver->resolve($order_item); $store = $this->selectStore($purchased_entity); $cart = $this->cartProvider->createCart($order_type_id, $store); } $this->entity = $this->cartManager->addOrderItem($cart, $order_item, $form_state->get(['settings', 'combine'])); // Other submit handlers might need the cart ID. $form_state->set('cart_id', $cart->id()); } /** * {@inheritdoc} * @see \Drupal\commerce_cart\Form\AddToCartForm::buildEntity() */ public function buildEntity(array $form, FormStateInterface $form_state) { /** @var \Drupal\commerce_order\Entity\OrderItemInterface $entity */ $entity = parent::buildEntity($form, $form_state); // Now that the purchased entity is set, populate the title and price. $purchased_entity = $entity->getPurchasedEntity(); $entity->setTitle($purchased_entity->getOrderItemTitle()); if (!$entity->isUnitPriceOverridden()) { $store = $this->selectStore($purchased_entity); $context = new Context($this->currentUser, $store); $resolved_price = $this->chainPriceResolver->resolve($purchased_entity, $entity->getQuantity(), $context); $entity->setUnitPrice($resolved_price); } $order_type_id = $this->orderTypeResolver->resolve($entity); $store = $this->selectStore($purchased_entity); $cart = $this->cartProvider->getCart($order_type_id, $store); if ($cart) { $entity->set('order_id', $cart->id()); } return $entity; } }