View source
<?php
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\basic_cart\OrderConnectStorage;
use Drupal\basic_cart\Utility;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\views\ViewExecutable;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
function basic_cart_theme($existing, $type, $theme, $path) {
return array(
'basic_cart_cart_template' => array(
'variables' => array(
'basic_cart' => NULL,
),
),
'total_price_markup' => array(
'variables' => array(
'price' => NULL,
),
),
'cart_form' => array(
'render element' => 'form',
),
'basic_cart_quantity_prefix' => array(
'variables' => array(
'basic_cart' => NULL,
),
),
'basic_cart_quantity_suffix' => array(
'variables' => array(
'basic_cart' => NULL,
),
),
'basic_cart_thank_you' => array(
'variables' => array(
'basic_cart' => NULL,
),
),
);
}
function basic_cart_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
}
function basic_cart_page_attachments(array &$page) {
$page['#attached']['library'][] = 'basic_cart/basic_cart';
}
function basic_cart_entity_insert(EntityInterface $node) {
$utility = new Utility();
if (Utility::isBasicCartOrder($node
->bundle())) {
$get_cart = $utility::getCart();
$cart = $get_cart['cart'];
$nid = $node
->id();
basic_cart_order_send_notifications($node);
$order = new OrderConnectStorage();
foreach ($cart as $key => $value) {
$params['oid'] = $nid;
$params['id'] = $key;
$params['entitytype'] = $value
->getEntityTypeId();
$params['quantity'] = $get_cart['cart_quantity'][$key];
$order
->insert($params);
}
$utility::emptyCart();
}
}
function basic_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form_ids = array(
'node_basic_cart_order_edit_form',
'node_basic_cart_order_form',
);
if (in_array($form_id, $form_ids)) {
$form['title']['widget'][0]['value']['#title'] = t('Name');
unset($form['basic_cart_vat']);
unset($form['basic_cart_total_price']);
$form['actions']['submit']['#value'] = t('Place Order');
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'basic_cart_order_submit';
}
}
}
}
function basic_cart_entity_presave(EntityInterface $node) {
if (Utility::isBasicCartOrder($node
->bundle())) {
$get_price = Utility::getTotalPrice();
$bundle = $node
->bundle();
if (empty($node
->id())) {
$node
->set('basic_cart_vat', $get_price->vat);
$node
->set('basic_cart_total_price', $get_price->total);
}
}
}
function basic_cart_entity_delete(EntityInterface $node) {
$utility = new Utility();
if (Utility::isBasicCartOrder($node
->bundle())) {
$entitytype = $node
->getEntityTypeId();
$id = $node
->id();
$bundle = $node
->bundle();
if (isset($entitytype) && isset($id)) {
$params['oid'] = $id;
$params['entitytype'] = $entitytype;
OrderConnectStorage::orderDelete($params);
}
}
}
function basic_cart_mail($key, &$message, $params) {
$utility = new Utility();
$config = $utility
->checkoutSettings();
$options = array(
'langcode' => $message['langcode'],
);
$message['from'] = \Drupal::config('system.site')
->get('mail');
switch ($key) {
case 'admin_mail':
$message['subject'] = SafeMarkup::checkPlain($config
->get('admin')['subject']);
$message['body'][] = Xss::filter($params['admin_message']);
break;
case 'user_mail':
$message['subject'] = SafeMarkup::checkPlain($config
->get('user')['subject']);
$message['body'][] = Xss::filter($params['user_message']);
break;
}
}
function basic_cart_order_send_notifications($order) {
$mailManager = \Drupal::service('plugin.manager.mail');
$utility = new Utility();
$token = \Drupal::token();
$config = $utility
->checkoutSettings();
$message_html = $config
->get('admin')['body'];
$data = array(
'node' => $order,
);
$message_html = $token
->replace($message_html, $data, array(
'callback' => 'basic_cart_order_tokens_format',
));
$params['admin_message'] = $message_html;
$site_mail = \Drupal::config('system.site')
->get('mail');
$send = true;
$admin_emails = $config
->get('admin_emails');
$langcode = \Drupal::currentUser()
->getPreferredLangcode();
if (empty($admin_emails)) {
$message = $mailManager
->mail('basic_cart', 'admin_mail', $site_mail, $langcode, $params, NULL, $send);
$mails_sent = 0;
if ($message['result']) {
$mails_sent++;
}
}
else {
$admin_emails = explode("\n", $admin_emails);
if (is_array($admin_emails) && !empty($admin_emails)) {
$ok = FALSE;
foreach ($admin_emails as $admin_email) {
$message = $mailManager
->mail('basic_cart', 'admin_mail', $admin_email, $langcode, $params, NULL, $send);
if ($message['result']) {
$ok = TRUE;
}
}
$mails_sent = 0;
if ($ok) {
$mails_sent++;
}
}
}
$send_user_mail = $config
->get('send_emailto_user');
if ($send_user_mail) {
$message_html = $config
->get('user')['body'];
$data = array(
'node' => $order,
);
$message_html = $token
->replace($message_html, $data, array(
'callback' => 'basic_cart_order_tokens_format',
));
$params['user_message'] = $message_html;
$email = $order
->getTranslation($langcode)
->get('basic_cart_email')
->getValue();
$email = $email[0]['value'];
$message = $mailManager
->mail('basic_cart', 'user_mail', $email, $langcode, $params, NULL, $send);
if ($message['result']) {
$mails_sent++;
}
}
return $mails_sent;
}
function basic_cart_order_tokens_format(&$tokens) {
$utility = new Utility();
if (is_array($tokens) && count($tokens) > 0) {
foreach ($tokens as $token => $value) {
switch ($token) {
case '[basic_cart_order:basic_cart_total_price]':
$tokens[$token] = $utility::formatPrice($value
->__toString());
break;
case '[basic_cart_order:basic_cart_vat]':
$tokens[$token] = $utility::formatPrice($value
->__toString());
break;
}
}
}
return $tokens;
}
function basic_cart_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = array();
$utility = new Utility();
$order = isset($data["node"]) ? $data["node"] : array();
if ($type == 'basic_cart_order') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'products':
$new = '';
$get_cart = $utility::getCart();
$cart = $get_cart['cart'];
$i = 0;
$products = '';
foreach ($cart as $nid => $node) {
$langcode = $node
->language()
->getId();
$price_value = $node
->getTranslation($langcode)
->get('add_to_cart_price')
->getValue();
$title = $node
->getTranslation($langcode)
->get('title')
->getValue();
$unit_price = $utility::formatPrice($price_value[0]['value']);
$new .= ++$i . '. ' . $title[0]['value'] . "\t" . $get_cart['cart_quantity'][$nid] . ' x ' . $unit_price . "\n";
}
$replacements[$original] = $new;
break;
case 'basic_cart_total_price':
$total = Utility::getTotalPrice();
$langcode = $order
->language()
->getId();
$title = $order
->getTranslation($langcode)
->get('title')
->getValue();
$replacements[$original] = !empty($total) && isset($total->total) ? $total->total : 0;
break;
case 'basic_cart_email':
$langcode = $order
->language()
->getId();
$email = $order
->getTranslation($langcode)
->get('basic_cart_email')
->getValue();
$replacements[$original] = isset($email[0]['value']) ? $email[0]['value'] : "";
break;
case 'basic_cart_vat':
$order = $data["node"];
$total = Utility::getTotalPrice();
$langcode = $order
->language()
->getId();
$vat = $order
->getTranslation($langcode)
->get('basic_cart_vat')
->getValue();
$replacements[$original] = isset($vat[0]['value']) ? $vat[0]['value'] : 0;
break;
}
}
}
return $replacements;
}
function basic_cart_token_info() {
$info = array();
$info['tokens']['basic_cart_order']['products'] = 'Listing of ordered products.';
$info['tokens']['basic_cart_order']['basic_cart_total_price'] = 'Total Price of the Order';
$info['tokens']['basic_cart_order']['basic_cart_vat'] = 'VAT value of the order.';
$info['tokens']['basic_cart_order']['basic_cart_email'] = 'Email defined with the order.';
return $info;
}
function basic_cart_user_login($account) {
$utility = new Utility();
$utility
->loggedInActionCart();
}
function basic_cart_order_submit(&$form, FormStateInterface $form_state, $form_id) {
$utility = new Utility();
$config = $utility
->checkoutSettings();
$location = trim($config
->get('thankyou')['custom_page']);
if ($location) {
$redirect = \Drupal::pathValidator()
->getUrlIfValid($location);
$form_state
->setRedirectUrl($redirect);
}
else {
$url = Url::fromRoute('basic_cart.thankyou');
$form_state
->setRedirectUrl($url);
}
}