I need to set order increment quantities in my Commerce 2 website. The website has two stores: retail and wholesale. Customers who are shopping in my "retail consumers" store must have increments of 6 products in their cart in order to check out (6, 12, 18, 24, etc). Customers in my "wholesale consumers" store must have at least 4 products in their cart in order to check out (4, 8, 12, 16, etc).
My plan is to add a custom Condition that can be used for the two payment gateways that will be used in the stores (one for each store). That custom Condition would allow me to designate an increment value for the product quantity for each gateway. "Make this gateway available if the cart contains an increment of 6 products".
I recently created a similar plugin for product quantity minimums in the cart, and I think I can re-use most of that code with a few adjustments. I am just not sure how to factor in the incremental requirement. It would be acceptable to hard-code the increments of 6 if that is the best/easiest way to do it; I could then create two separate condition plugins, one for 6 and one for 4.
Below is the Condition plugin code I have so far:
<?php
namespace Drupal\order_total_quantity_increments\Plugin\Commerce\Condition;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\commerce_price\Calculator;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides the quantity increment condition for order items.
*
* @CommerceCondition(
* id = "order_total_quantity_increments",
* label = @Translation("Quantity Increment"),
* display_label = @Translation("Limit by quantity increments"),
* category = @Translation("Products"),
* entity_type = "commerce_order",
* weight = 10,
* )
*/
class OrderTotalQuantityIncrements extends ConditionBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'increment' => NULL,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$increment = $this->configuration['increment'];
if (isset($increment) && !isset($increment['number'])) {
$increment = NULL;
}
$form['increment'] = [
'#type' => 'number',
'#title' => t('Increment'),
'#default_value' => $this->configuration['increment'],
'#min' => 1,
'#required' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state->getValue($form['#parents']);
$this->configuration['increment'] = $values['increment'];
}
/**
* {@inheritdoc}
*/
public function evaluate(EntityInterface $entity) {
$this->assertEntity($entity);
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $entity;
$increment = '0';
foreach ($order->getItems() as $order_item) {
$increment = Calculator::add($increment, $order_item->getQuantity());
}
}
}
I believe I have to modify the last part of the code (below) or add something after it.
public function evaluate(EntityInterface $entity) {
$this->assertEntity($entity);
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $entity;
$increment = '0';
foreach ($order->getItems() as $order_item) {
$increment = Calculator::add($increment, $order_item->getQuantity());
}
}
Can anyone please provide me with a direction to go in/guidance on how I should modify the code to meet my objective?