View source
<?php
namespace Drupal\commerce_cart_api\Plugin\rest\resource;
use Drupal\commerce\Context;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_cart\CartManagerInterface;
use Drupal\commerce_cart\CartProviderInterface;
use Drupal\commerce_order\Resolver\ChainOrderTypeResolverInterface;
use Drupal\commerce_price\Resolver\ChainPriceResolverInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\rest\ModifiedResourceResponse;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class CartAddResource extends CartResourceBase {
protected $entityTypeManager;
protected $orderItemStorage;
protected $chainOrderTypeResolver;
protected $currentStore;
protected $chainPriceResolver;
protected $currentUser;
protected $entityRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, CartProviderInterface $cart_provider, CartManagerInterface $cart_manager, EntityTypeManagerInterface $entity_type_manager, ChainOrderTypeResolverInterface $chain_order_type_resolver, CurrentStoreInterface $current_store, ChainPriceResolverInterface $chain_price_resolver, AccountInterface $current_user, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger, $cart_provider, $cart_manager);
$this->entityTypeManager = $entity_type_manager;
$this->orderItemStorage = $entity_type_manager
->getStorage('commerce_order_item');
$this->chainOrderTypeResolver = $chain_order_type_resolver;
$this->currentStore = $current_store;
$this->chainPriceResolver = $chain_price_resolver;
$this->currentUser = $current_user;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->getParameter('serializer.formats'), $container
->get('logger.factory')
->get('rest'), $container
->get('commerce_cart.cart_provider'), $container
->get('commerce_cart.cart_manager'), $container
->get('entity_type.manager'), $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'), $container
->get('entity.repository'));
}
public function post(array $data, Request $request) {
$order_items = [];
foreach ($data as $key => $order_item_data) {
if (!isset($order_item_data['purchased_entity_type'])) {
throw new UnprocessableEntityHttpException(sprintf('You must specify a purchasable entity type for row: %s', $key));
}
if (!isset($order_item_data['purchased_entity_id'])) {
throw new UnprocessableEntityHttpException(sprintf('You must specify a purchasable entity ID for row: %s', $key));
}
if (!$this->entityTypeManager
->hasDefinition($order_item_data['purchased_entity_type'])) {
throw new UnprocessableEntityHttpException(sprintf('You must specify a valid purchasable entity type for row: %s', $key));
}
}
foreach ($data as $order_item_data) {
$storage = $this->entityTypeManager
->getStorage($order_item_data['purchased_entity_type']);
$purchased_entity = $storage
->load($order_item_data['purchased_entity_id']);
if (!$purchased_entity || !$purchased_entity instanceof PurchasableEntityInterface) {
continue;
}
$purchased_entity = $this->entityRepository
->getTranslationFromContext($purchased_entity);
$store = $this
->selectStore($purchased_entity);
$order_item = $this->orderItemStorage
->createFromPurchasableEntity($purchased_entity, [
'quantity' => !empty($order_item_data['quantity']) ? $order_item_data['quantity'] : 1,
]);
$context = new Context($this->currentUser, $store);
$order_item
->setUnitPrice($this->chainPriceResolver
->resolve($purchased_entity, $order_item
->getQuantity(), $context));
$order_type_id = $this->chainOrderTypeResolver
->resolve($order_item);
$cart = $this->cartProvider
->getCart($order_type_id, $store);
if (!$cart) {
$cart = $this->cartProvider
->createCart($order_type_id, $store);
}
if (!isset($order_item_data['combine'])) {
$order_item_data['combine'] = TRUE;
}
$order_items[] = $this->cartManager
->addOrderItem($cart, $order_item, $order_item_data['combine']);
}
$response = new ModifiedResourceResponse(array_values($order_items), 200);
return $response;
}
protected function selectStore(PurchasableEntityInterface $entity) {
$stores = $entity
->getStores();
if (count($stores) === 1) {
$store = reset($stores);
}
elseif (count($stores) === 0) {
throw new UnprocessableEntityHttpException('The given entity is not assigned to any store.');
}
else {
$store = $this->currentStore
->getStore();
if (!in_array($store, $stores)) {
throw new UnprocessableEntityHttpException("The given entity can't be purchased from the current store.");
}
}
return $store;
}
}