public function XquantityAddTocartForm::submitForm in Commerce Extended Quantity 8
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides AddToCartForm::submitForm
File
- src/
Form/ XquantityAddTocartForm.php, line 80
Class
- XquantityAddTocartForm
- Overrides the order item add to cart form.
Namespace
Drupal\commerce_xquantity\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$item = NULL;
$order_item = $this->entity;
$purchased_entity = $order_item
->getPurchasedEntity();
$form_object = $form_state
->getFormObject();
if ($id = $form_state
->getValue('purchased_entity')) {
if (!empty($id[0]['variation']) && $id[0]['variation'] != $purchased_entity
->id()) {
$this
->setEntity($this
->buildEntity($form, $form_state));
$order_item = $this->entity;
$purchased_entity = $purchased_entity
->load($id[0]['variation']);
// Reset quantity prices.
$order_item
->setQuantityPrices($form_object, $order_item
->getFormDisplayWidget(), $form_state);
}
}
if ($qty = $form_state
->getValue('quantity')) {
if (!empty($qty[0]['value'])) {
$quantity = $qty[0]['value'];
}
}
else {
$quantity = $order_item
->getQuantity();
}
if ($form_object->quantityScale !== NULL) {
$scale = $form_object->quantityScale ?: 0;
$order_type_id = $this->orderTypeResolver
->resolve($order_item);
$store = $this
->selectStore($purchased_entity);
$cart = $this->cartProvider
->getCart($order_type_id, $store);
if ($cart && ($items = $cart
->getItems())) {
$matcher = \Drupal::service('commerce_cart.order_item_matcher');
if ($item = $matcher
->match($order_item, $items)) {
$quantity = bcadd($quantity, $item
->getQuantity(), $scale);
}
}
if ($price = $order_item
->getQuantityPrice($form_object, $purchased_entity, $quantity)) {
$this->entity
->setUnitPrice($price, TRUE);
if ($item && !$price
->equals($item
->getUnitPrice())) {
$item
->setUnitPrice($price, TRUE);
$this->cartManager
->updateOrderItem($cart, $item, FALSE);
}
}
}
parent::submitForm($form, $form_state);
$messenger = $this
->messenger();
$messages = $messenger
->messagesByType('status');
$messenger
->deleteByType('status');
foreach ($messages as $msg) {
if (preg_match('/\\<a href\\="\\/cart"\\>.*\\<\\/a\\>/', $msg
->__toString(), $matches)) {
$this->moduleHandler
->alter("xquantity_added_to_cart_msg", $msg, $this);
$msg && $messenger
->addMessage($msg);
}
else {
$messenger
->addMessage($msg);
}
}
}