View source
<?php
namespace Drupal\commerce_shipping_weight_tariff\Plugin\Commerce\ShippingMethod;
use Drupal\commerce_price\Price;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\PackageTypeManagerInterface;
use Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod\ShippingMethodBase;
use Drupal\commerce_shipping\ShippingRate;
use Drupal\commerce_shipping\ShippingService;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\state_machine\WorkflowManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ShippingByWeight extends ShippingMethodBase {
protected $stateService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PackageTypeManagerInterface $package_type_manager, WorkflowManagerInterface $workflow_manager, StateInterface $stateService) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $package_type_manager, $workflow_manager);
$this->services['default'] = new ShippingService('default', $this->configuration['rate_label']);
$this->stateService = $stateService;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.commerce_package_type'), $container
->get('plugin.manager.workflow'), $container
->get('state'));
}
public function defaultConfiguration() {
return [
'rate_label' => '',
'base_rate_amount' => '',
'services' => [
'default',
],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$amount = $this->configuration['base_rate_amount'];
if (isset($amount) && !isset($amount['number'], $amount['currency_code'])) {
$amount = NULL;
}
$form['rate_label'] = [
'#type' => 'textfield',
'#title' => t('Shipment type label'),
'#description' => t('Shown to customers during checkout.'),
'#default_value' => $this->configuration['rate_label'],
'#required' => TRUE,
];
$form['base_rate_amount'] = [
'#type' => 'commerce_price',
'#title' => t('Base shipping rate amount'),
'#default_value' => $amount,
'#required' => 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['rate_label'] = $values['rate_label'];
$this->configuration['base_rate_amount'] = $values['base_rate_amount'];
}
}
public function calculateRates(ShipmentInterface $shipment) {
$amount = $this->configuration['base_rate_amount'];
$shipment_tax_by_weight = $this->stateService
->get('order_' . $shipment
->getOrderId() . '_weight_condition');
$this->stateService
->delete('order_' . $shipment
->getOrderId() . '_weight_condition');
$shipping_tax_value = $shipment_tax_by_weight['condition_price'] + $amount['number'];
$amount = new Price((string) $shipping_tax_value, $amount['currency_code']);
$rates = [];
$rates[] = new ShippingRate([
'shipping_method_id' => $this->parentEntity
->id(),
'service' => $this->services['default'],
'amount' => $amount,
]);
return $rates;
}
}