View source
<?php
namespace Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer;
use Drupal\commerce\EntityUuidMapperInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_price\RounderInterface;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\PromotionOfferBase;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\ShippingOrderManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ShipmentPromotionOfferBase extends PromotionOfferBase implements ShipmentPromotionOfferInterface {
protected $entityUuidMapper;
protected $shippingOrderManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RounderInterface $rounder, EntityUuidMapperInterface $entity_uuid_mapper, ShippingOrderManagerInterface $shipping_order_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $rounder);
$this->entityUuidMapper = $entity_uuid_mapper;
$this->shippingOrderManager = $shipping_order_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('commerce_price.rounder'), $container
->get('commerce.entity_uuid_mapper'), $container
->get('commerce_shipping.order_manager'));
}
public function defaultConfiguration() {
return [
'display_inclusive' => FALSE,
'filter' => 'none',
'shipping_methods' => [],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$uuids = $this
->getShippingMethodUuids();
$shipping_method_ids = $this->entityUuidMapper
->mapToIds('commerce_shipping_method', $uuids);
$radio_parents = array_merge($form['#parents'], [
'filter',
]);
$radio_path = array_shift($radio_parents);
$radio_path .= '[' . implode('][', $radio_parents) . ']';
$form['display_inclusive'] = [
'#type' => 'radios',
'#title' => $this
->t('Discount display'),
'#title_display' => 'invisible',
'#options' => [
TRUE => $this
->t('Include the discount in the displayed amount'),
FALSE => $this
->t('Only show the discount on the order total summary'),
],
'#default_value' => (int) $this->configuration['display_inclusive'],
];
$form['filter'] = [
'#type' => 'radios',
'#title' => $this
->t('Applies to'),
'#default_value' => $this->configuration['filter'],
'#options' => [
'none' => $this
->t('All shipping methods'),
'include' => $this
->t('Only the selected shipping methods'),
'exclude' => $this
->t('All except the selected shipping methods'),
],
];
$form['container'] = [
'#type' => 'container',
'#states' => [
'invisible' => [
':input[name="' . $radio_path . '"]' => [
'value' => 'none',
],
],
],
];
$form['container']['shipping_methods'] = [
'#parents' => array_merge($form['#parents'], [
'shipping_methods',
]),
'#type' => 'commerce_entity_select',
'#title' => $this
->t('Shipping methods'),
'#default_value' => $shipping_method_ids,
'#target_type' => 'commerce_shipping_method',
'#hide_single_entity' => FALSE,
'#autocomplete_threshold' => 10,
'#multiple' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['display_inclusive'] = $values['display_inclusive'];
$this->configuration['filter'] = $values['filter'];
$this->configuration['shipping_methods'] = [];
if ($values['filter'] != 'none') {
$uuids = $this->entityUuidMapper
->mapFromIds('commerce_shipping_method', $values['shipping_methods']);
foreach ($uuids as $uuid) {
$this->configuration['shipping_methods'][] = [
'shipping_method' => $uuid,
];
}
}
}
}
public function isDisplayInclusive() {
return $this->configuration['display_inclusive'];
}
protected function getShippingMethodUuids() {
return array_column($this->configuration['shipping_methods'], 'shipping_method');
}
public function apply(EntityInterface $order, PromotionInterface $promotion) {
assert($order instanceof OrderInterface);
if (!$order
->hasField('shipments') || $order
->get('shipments')
->isEmpty()) {
return;
}
$shipments = $order
->get('shipments')
->referencedEntities();
foreach ($shipments as $shipment) {
if ($this
->appliesToShipment($shipment)) {
$this
->applyToShipment($shipment, $promotion);
}
}
}
protected function appliesToShipment(ShipmentInterface $shipment) {
if (!$shipment
->getShippingMethodId() || !$shipment
->getAmount()) {
return FALSE;
}
if ($this->configuration['filter'] == 'none') {
return TRUE;
}
$shipping_method = $shipment
->getShippingMethod();
if (!$shipping_method) {
return FALSE;
}
$match = in_array($shipping_method
->uuid(), $this
->getShippingMethodUuids());
return $this->configuration['filter'] == 'include' ? $match : !$match;
}
protected abstract function applyToShipment(ShipmentInterface $shipment, PromotionInterface $promotion);
}