commerce-8.x-2.8/modules/cart/src/Form/AddToCartForm.php
modules/cart/src/Form/AddToCartForm.php
<?php
namespace Drupal\commerce_cart\Form;
use Drupal\commerce\Context;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_cart\CartManagerInterface;
use Drupal\commerce_cart\CartProviderInterface;
use Drupal\commerce_order\Resolver\OrderTypeResolverInterface;
use Drupal\commerce_price\Resolver\ChainPriceResolverInterface;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
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 form.
*/
class AddToCartForm extends ContentEntityForm implements AddToCartFormInterface {
/**
* 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;
/**
* Constructs a new AddToCartForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
* @param \Drupal\commerce_cart\CartManagerInterface $cart_manager
* The cart manager.
* @param \Drupal\commerce_cart\CartProviderInterface $cart_provider
* The cart provider.
* @param \Drupal\commerce_order\Resolver\OrderTypeResolverInterface $order_type_resolver
* The order type resolver.
* @param \Drupal\commerce_store\CurrentStoreInterface $current_store
* The current store.
* @param \Drupal\commerce_price\Resolver\ChainPriceResolverInterface $chain_price_resolver
* The chain base price resolver.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(EntityManagerInterface $entity_manager, 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_manager, $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.manager'),
$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}
*/
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}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Add to cart'),
'#submit' => ['::submitForm'],
];
return $actions;
}
/**
* {@inheritdoc}
*/
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();
$order_type_id = $this->orderTypeResolver->resolve($order_item);
$store = $this->selectStore($purchased_entity);
$cart = $this->cartProvider->getCart($order_type_id, $store);
if (!$cart) {
$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}
*/
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);
}
return $entity;
}
/**
* Selects the store for the given purchasable entity.
*
* If the entity is sold from one store, then that store is selected.
* If the entity is sold from multiple stores, and the current store is
* one of them, then that store is selected.
*
* @param \Drupal\commerce\PurchasableEntityInterface $entity
* The entity being added to cart.
*
* @throws \Exception
* When the entity can't be purchased from the current store.
*
* @return \Drupal\commerce_store\Entity\StoreInterface
* The selected store.
*/
protected function selectStore(PurchasableEntityInterface $entity) {
$stores = $entity->getStores();
if (count($stores) === 1) {
$store = reset($stores);
}
elseif (count($stores) === 0) {
// Malformed entity.
throw new \Exception('The given entity is not assigned to any store.');
}
else {
$store = $this->currentStore->getStore();
if (!in_array($store, $stores)) {
// Indicates that the site listings are not filtered properly.
throw new \Exception("The given entity can't be purchased from the current store.");
}
}
return $store;
}
}
