class CashOnDelivery in Ubercart 8.4
Defines the cash on delivery payment method.
Plugin annotation
@UbercartPaymentMethod(
id = "cod",
name = @Translation("Cash on delivery"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_payment\PaymentMethodPluginBase implements ContainerFactoryPluginInterface, PaymentMethodPluginInterface
- class \Drupal\uc_payment_pack\Plugin\Ubercart\PaymentMethod\CashOnDelivery
- class \Drupal\uc_payment\PaymentMethodPluginBase implements ContainerFactoryPluginInterface, PaymentMethodPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CashOnDelivery
File
- payment/
uc_payment_pack/ src/ Plugin/ Ubercart/ PaymentMethod/ CashOnDelivery.php, line 19
Namespace
Drupal\uc_payment_pack\Plugin\Ubercart\PaymentMethodView source
class CashOnDelivery extends PaymentMethodPluginBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'policy' => 'Full payment is expected upon delivery or prior to pick-up.',
'max_order' => 0,
'delivery_date' => FALSE,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['policy'] = [
'#type' => 'textarea',
'#title' => $this
->t('Policy message'),
'#default_value' => $this->configuration['policy'],
'#description' => $this
->t('Help message shown at checkout.'),
];
$form['max_order'] = [
'#type' => 'uc_price',
'#title' => $this
->t('Maximum order total eligible for COD'),
'#default_value' => $this->configuration['max_order'],
'#description' => $this
->t('Set to 0 for no maximum order limit.'),
];
$form['delivery_date'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Let customers enter a desired delivery date.'),
'#default_value' => $this->configuration['delivery_date'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['policy'] = $form_state
->getValue('policy');
$this->configuration['max_order'] = $form_state
->getValue('max_order');
$this->configuration['delivery_date'] = $form_state
->getValue('delivery_date');
}
/**
* {@inheritdoc}
*/
public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) {
$build['#attached']['library'][] = 'uc_payment_pack/cod.styles';
$build['policy'] = [
'#prefix' => '<p>',
'#markup' => Html::escape($this->configuration['policy']),
'#suffix' => '</p>',
];
if (($max = $this->configuration['max_order']) > 0 && is_numeric($max)) {
$build['eligibility'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('Orders totalling more than @amount are <b>not eligible</b> for COD.', [
'@amount' => uc_currency_format($max),
]),
'#suffix' => '</p>',
];
}
if ($this->configuration['delivery_date']) {
$build += $this
->deliveryDateForm($order);
}
return $build;
}
/**
* {@inheritdoc}
*/
public function cartProcess(OrderInterface $order, array $form, FormStateInterface $form_state) {
if ($this->configuration['delivery_date']) {
$order->payment_details = $form_state
->getValue([
'panes',
'payment',
'details',
]);
if (isset($order->payment_details['delivery_date'])) {
$order->payment_details['delivery_date'] = $order->payment_details['delivery_date']
->getTimestamp();
}
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function cartReview(OrderInterface $order) {
$review = [];
if ($this->configuration['delivery_date'] && isset($order->payment_details['delivery_date'])) {
$date = \Drupal::service('date.formatter')
->format($order->payment_details['delivery_date'], 'uc_store');
$review[] = [
'title' => $this
->t('Delivery date'),
'data' => $date,
];
}
return $review;
}
/**
* {@inheritdoc}
*/
public function orderView(OrderInterface $order) {
$build = [];
if ($this->configuration['delivery_date'] && isset($order->payment_details['delivery_date'])) {
$build['#markup'] = $this
->t('Desired delivery date:') . '<br />' . \Drupal::service('date.formatter')
->format($order->payment_details['delivery_date'], 'uc_store');
}
return $build;
}
/**
* {@inheritdoc}
*/
public function orderEditDetails(OrderInterface $order) {
$build = [];
if ($this->configuration['delivery_date']) {
$build = $this
->deliveryDateForm($order);
}
return $build;
}
/**
* {@inheritdoc}
*/
public function orderEditProcess(OrderInterface $order, array $form, FormStateInterface $form_state) {
if ($this->configuration['delivery_date']) {
$payment_details = $form_state
->getValue('payment_details');
if (isset($payment_details)) {
$payment_details['delivery_date'] = $payment_details['delivery_date']
->getTimestamp();
return $payment_details;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function orderLoad(OrderInterface $order) {
$result = $this->database
->query('SELECT * FROM {uc_payment_cod} WHERE order_id = :id', [
':id' => $order
->id(),
]);
if ($row = $result
->fetchObject()) {
$order->payment_details['delivery_date'] = $row->delivery_date;
}
}
/**
* {@inheritdoc}
*/
public function orderSave(OrderInterface $order) {
if (isset($order->payment_details['delivery_date'])) {
$this->database
->merge('uc_payment_cod')
->key([
'order_id' => $order
->id(),
])
->fields([
'delivery_date' => $order->payment_details['delivery_date'],
])
->execute();
}
}
/**
* {@inheritdoc}
*/
public function orderSubmit(OrderInterface $order) {
$max = $this->configuration['max_order'];
if ($max > 0 && $order
->getTotal() > $max) {
return $this
->t('Your final order total exceeds the maximum for COD payment. Please go back and select a different method of payment.');
}
}
/**
* {@inheritdoc}
*/
public function orderDelete(OrderInterface $order) {
$this->database
->delete('uc_payment_cod')
->condition('order_id', $order
->id())
->execute();
}
/**
* Collect additional information for the "Cash on Delivery" payment method.
*
* @param \Drupal\uc_order\OrderInterface $order
* The order entity.
*/
protected function deliveryDateForm(OrderInterface $order) {
$delivery_date = empty($order->payment_details['delivery_date']) ? DrupalDateTime::createFromTimestamp(\Drupal::time()
->getRequestTime()) : DrupalDateTime::createFromTimestamp($order->payment_details['delivery_date']);
$form['delivery_date'] = [
'#type' => 'datetime',
'#title' => $this
->t('Enter a desired delivery date:'),
'#date_date_element' => 'date',
'#date_time_element' => 'none',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#default_value' => $delivery_date,
];
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CashOnDelivery:: |
public | function |
Form constructor. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Returns the form or render array to be displayed at checkout. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Called when checkout is submitted with this payment method selected. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Returns the payment method review details. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Gets default configuration for this plugin. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
protected | function | Collect additional information for the "Cash on Delivery" payment method. | |
CashOnDelivery:: |
public | function |
Called when an order is being deleted. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Called when an order is being edited with this payment method. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Called when an order is being submitted after being edited. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Called when an order is being loaded with this payment method. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Called when an order is being saved with this payment method. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Called when an order is being submitted with this payment method. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Called when an order is being viewed by an administrator. Overrides PaymentMethodPluginBase:: |
|
CashOnDelivery:: |
public | function |
Form submission handler. Overrides PaymentMethodPluginBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PaymentMethodPluginBase:: |
protected | property | The database service. | |
PaymentMethodPluginBase:: |
public | function |
Returns the payment method title to be used on the checkout review page. Overrides PaymentMethodPluginInterface:: |
2 |
PaymentMethodPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Called when an order is being viewed by a customer. Overrides PaymentMethodPluginInterface:: |
2 |
PaymentMethodPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Returns the payment method label with logo. Overrides PaymentMethodPluginInterface:: |
3 |
PaymentMethodPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Constructs the PaymentMethodPluginBase object. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |