public function XquantityOrderItem::setQuantityPrices in Commerce Extended Quantity 8
File
- src/
Entity/ XquantityOrderItem.php, line 95
Class
- XquantityOrderItem
- Overrides the order item entity class.
Namespace
Drupal\commerce_xquantity\EntityCode
public function setQuantityPrices(FormInterface &$form_object, $widget, FormStateInterface $form_state) {
$settings = $this
->getQuantityWidgetSettings();
if (empty($settings['qty_prices']) || !($count = count($settings['qty_price'])) || !($purchased_entity = $this
->getPurchasedEntity())) {
return $settings;
}
$lis = $notify = '';
$price = $purchased_entity
->getPrice();
$variation_type = $purchased_entity
->bundle();
$product = $purchased_entity
->getProduct();
$product_stores = $product
->getStores();
array_walk($product_stores, function (&$store) {
$store = $store
->bundle();
});
$list_price = $purchased_entity
->getListPrice();
$data = [
'variation_id' => $purchased_entity
->id(),
'variation_type' => $purchased_entity
->bundle(),
'product_id' => $product
->id(),
'product_type' => $product
->bundle(),
'list_price' => $list_price,
'product_stores' => $product_stores,
'current_roles' => \Drupal::currentUser()
->getRoles(),
];
$arguments = [];
$form_object->quantityScale = Numeric::getDecimalDigits($settings['step']);
$formatter = \Drupal::service('commerce_price.currency_formatter');
// Roll back to an initial price.
$form_object->quantityPrices[] = [
'price' => $price,
'qty_start' => $settings['min'] ?: $settings['step'],
'qty_end' => '',
];
foreach ($settings['qty_price'] as $index => $qty_price) {
extract($qty_price);
if ($qty_start && $settings['qty_prices'] > $index && $this
->quantityPriceApplies($qty_price, $data)) {
$new = $list ? $list_price : $price;
if (is_numeric($adjust_value)) {
if ($adjust_type == 'fixed_number') {
$adjust_price = new $new($adjust_value, $new
->getCurrencyCode());
}
else {
$adjust_price = $new
->divide('100')
->multiply($adjust_value);
}
$new = $new
->{$adjust_op}($adjust_price);
}
if ($new
->isNegative()) {
$new = $new
->multiply('0');
}
$form_object->quantityPrices[] = [
'price' => $new,
] + $qty_price;
$new = $new
->toArray();
if ($this
->isNew() && !empty($notify['add_to_cart']) || $this
->id() && !empty($notify['shopping_cart'])) {
$args = [];
foreach ($qty_price as $key => $value) {
if ($key == 'notify') {
$value = implode(', ', array_values($qty_price[$key]));
}
$args["%{$key}"] = $value;
}
$arguments[] = [
'%price' => $formatter
->format(Calculator::round($new['number'], 2), $new['currency_code']),
] + $args;
$li = new TranslatableMarkup('Buy <span style="color:yellow;font-weight: bolder;">%qty_start</span> or more and get <span style="color:yellow;font-weight: bolder;">%price</span> price for an item', end($arguments));
$lis .= "<li>{$li}</li>";
}
}
}
$module_handler = \Drupal::moduleHandler();
$module_handler
->alter("xquantity_add_to_cart_qty_prices", $form_object, $widget, $form_state);
$form_state
->setFormObject($form_object);
if ($lis) {
$msg = new TranslatableMarkup("Price adjustments for the %label:<br><ul>{$lis}</ul><hr>", [
'%label' => $this
->label(),
'qty_arguments' => $arguments,
]);
$module_handler
->alter("xquantity_add_to_cart_qty_prices_msg", $msg, $widget, $form_state);
$messenger = \Drupal::service('messenger');
$messages = $messenger
->messagesByType('status');
$messenger
->deleteByType('status');
// Make sure the 'Added to cart' message displayed the last.
$added_to_cart_msg = NULL;
foreach ($messages as $message) {
if (preg_match('/\\<a href\\="\\/cart"\\>.*\\<\\/a\\>/', $message
->__toString(), $matches)) {
$added_to_cart_msg = $message;
}
else {
$messenger
->addMessage($message);
}
}
$msg && $messenger
->addMessage($msg);
$added_to_cart_msg && $messenger
->addMessage($added_to_cart_msg);
}
return $settings;
}