View source
<?php
namespace Drupal\commerce_wishlist\Plugin\views\field;
use Drupal\commerce_cart\CartManagerInterface;
use Drupal\commerce_cart\CartProviderInterface;
use Drupal\commerce_order\Resolver\OrderTypeResolverInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\commerce_store\SelectStoreTrait;
use Drupal\commerce_wishlist\Entity\WishlistItemInterface;
use Drupal\commerce_wishlist\WishlistManagerInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MoveToCart extends FieldPluginBase {
use SelectStoreTrait;
use UncacheableFieldHandlerTrait;
protected $cartManager;
protected $cartProvider;
protected $orderTypeResolver;
protected $currentStore;
protected $wishlistManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, WishlistManagerInterface $wishlist_manager, CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, OrderTypeResolverInterface $order_type_resolver, CurrentStoreInterface $current_store) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->wishlistManager = $wishlist_manager;
$this->cartManager = $cart_manager;
$this->cartProvider = $cart_provider;
$this->orderTypeResolver = $order_type_resolver;
$this->currentStore = $current_store;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('commerce_wishlist.wishlist_manager'), $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'));
}
public function clickSortable() {
return FALSE;
}
public function getValue(ResultRow $row, $field = NULL) {
return '<!--form-item-' . $this->options['id'] . '--' . $row->index . '-->';
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['keep_item'] = [
'default' => FALSE,
];
$options['combine'] = [
'default' => TRUE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['keep_item'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Keep item'),
'#description' => $this
->t('Enable in order to keep the item in the wishlist (copy instead of move).'),
'#default_value' => $this->options['keep_item'],
];
$form['combine'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Combine'),
'#description' => $this
->t('Combine order items containing the same product variation.'),
'#default_value' => $this->options['combine'],
];
}
public function viewsForm(array &$form, FormStateInterface $form_state) {
$form['#cache']['max-age'] = 0;
if (empty($this->view->result)) {
unset($form['actions']);
return;
}
$form[$this->options['id']]['#tree'] = TRUE;
foreach ($this->view->result as $row_index => $row) {
$wishlist_item = $this
->getEntity($this->view->result[$row_index]);
if ($this
->isValid($wishlist_item)) {
$form[$this->options['id']][$row_index] = [
'#type' => 'submit',
'#value' => $this->options['keep_item'] ? $this
->t('Copy to cart') : $this
->t('Move to cart'),
'#name' => 'move-wishlist-item-' . $row_index,
'#move_wishlist_item' => TRUE,
'#row_index' => $row_index,
'#attributes' => [
'class' => [
'move-wishlist-item',
],
],
];
}
else {
$form[$this->options['id']][$row_index] = [];
}
}
}
public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if (!empty($triggering_element['#move_wishlist_item'])) {
$row_index = $triggering_element['#row_index'];
$wishlist_item = $this
->getEntity($this->view->result[$row_index]);
$purchased_entity = $wishlist_item
->getPurchasableEntity();
$order_item = $this->cartManager
->createOrderItem($purchased_entity, $wishlist_item
->getQuantity());
$order_type = $this->orderTypeResolver
->resolve($order_item);
$store = $this
->selectStore($purchased_entity);
$cart = $this->cartProvider
->getCart($order_type, $store);
if (!$cart) {
$cart = $this->cartProvider
->createCart($order_type, $store);
}
$this->cartManager
->addOrderItem($cart, $order_item, $this->options['combine']);
if (!$this->options['keep_item']) {
$wishlist = $wishlist_item
->getWishlist();
$wishlist
->removeItem($wishlist_item);
$wishlist
->save();
$wishlist_item
->delete();
}
}
}
public function query() {
}
protected function isValid(WishlistItemInterface $wishlist_item) {
$purchasable_entity = $wishlist_item
->getPurchasableEntity();
$valid = FALSE;
if ($purchasable_entity instanceof ProductVariationInterface) {
$valid = $purchasable_entity
->isActive();
}
elseif ($purchasable_entity instanceof EntityPublishedInterface) {
$valid = $purchasable_entity
->isPublished();
}
if ($valid) {
try {
$this
->selectStore($wishlist_item
->getPurchasableEntity());
} catch (\Exception $e) {
$valid = FALSE;
}
}
return $valid;
}
}