basic_cart.module in Basic cart 8.3
Basic cart module file.
File
basic_cart.moduleView source
<?php
/**
* @file
* Basic cart module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\basic_cart\Utility;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
/**
* Implements hook_theme() for node entities.
*/
function basic_cart_theme($existing, $type, $theme, $path) {
global $base_url;
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,
),
),
'basic_cart_count_block' => array(
'variables' => array(
'cartcount' => NULL,
'module_url' => $base_url . '/' . drupal_get_path('module', 'basic_cart'),
'size' => "48x39",
'float' => 'right',
'right' => '7px',
'left' => '0',
'top' => '6px',
'bottom' => 0,
'size_class' => '-48-39',
),
),
);
}
/**
* Implements hook_page_attachments().
*/
function basic_cart_page_attachments(array &$page) {
$page['#attached']['library'][] = 'basic_cart/basic_cart';
}
/**
* Implements hook_entity_insert().
*/
function basic_cart_entity_insert(EntityInterface $node) {
$utility = new Utility();
if (Utility::isBasicCartOrder($node
->bundle())) {
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();
}
}
/**
* Implements hook_form_alter().
*/
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']);
unset($form['basic_cart_content']);
$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';
}
}
}
}
/**
* Implements hook_entity_presave().
*/
function basic_cart_entity_presave(EntityInterface $node) {
if (Utility::isBasicCartOrder($node
->bundle())) {
$get_price = Utility::getTotalPrice();
$cart = Utility::getCart();
$content = $cart['cart'] ? $cart['cart'] : [];
if (empty($node
->id())) {
$node
->set('basic_cart_vat', $get_price->vat);
$node
->set('basic_cart_total_price', $get_price->total);
$node
->set('basic_cart_content', array_keys($content));
}
}
}
/**
* Implements hook_entity_delete().
*/
/*function basic_cart_entity_delete(EntityInterface $node) {
if (Utility::isBasicCartOrder($node->bundle())) {
$entitytype = $node->getEntityTypeId();
$id = $node->id();
// if (isset($entitytype) && isset($id)) {
// //$params['oid'] = $id;
// //$params['entitytype'] = $entitytype;
// // OrderConnectStorage::orderDelete($params);
// }
}
}
*/
/**
* Implements hook_mail().
*/
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;
}
}
/**
* Sending emails.
*/
function basic_cart_order_send_notifications($order) {
$mailManager = \Drupal::service('plugin.manager.mail');
// Admin message.
$utility = new Utility();
$token = \Drupal::token();
$config = $utility
->checkoutSettings();
$message_html = $config
->get('admin')['body'];
// Token data.
$data = array(
'node' => $order,
);
$message_html = $token
->replace($message_html, $data, array(
'callback' => 'basic_cart_order_tokens_format',
));
// Admin mail.
$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)) {
// Sending mail to admin.
$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) {
// Sending mail to each admin.
$message = $mailManager
->mail('basic_cart', 'admin_mail', $admin_email, $langcode, $params, NULL, $send);
// Verifing that the mail was sent for at least one email address.
if ($message['result']) {
$ok = TRUE;
}
}
$mails_sent = 0;
if ($ok) {
$mails_sent++;
}
}
}
// User email.
$send_user_mail = $config
->get('send_emailto_user');
if ($send_user_mail) {
$message_html = $config
->get('user')['body'];
// Token data.
$data = array(
'node' => $order,
);
$message_html = $token
->replace($message_html, $data, array(
'callback' => 'basic_cart_order_tokens_format',
));
$params['user_message'] = $message_html;
// Sending mail.
$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;
}
/**
* Callback function for formatting the order tokens.
*
* @param array $tokens
* The tockens list.
*
* @return array
* The formatted list of tokens
*/
function basic_cart_order_tokens_format(array &$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;
}
/**
* Implements hook_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();
// The first thing that we're going to check for is the type of token - node,
// user etc...
if ($type == 'basic_cart_order') {
// Loop through each of the available tokens.
foreach ($tokens as $name => $original) {
// Find the desired token by name.
switch ($name) {
case 'products':
$new = '';
$get_cart = $utility::getCart();
$cart = $get_cart['cart'];
// Building the order details.
$i = 0;
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";
}
// Add the new value into the replacements array.
$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 the replacements.
return $replacements;
}
/**
* Implements hook_token_info().
*/
function basic_cart_token_info() {
$info = array();
// Add any new tokens.
$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 them.
return $info;
}
/**
* Implements hook_user_login().
*/
function basic_cart_user_login($account) {
$utility = new Utility();
$utility
->loggedInActionCart();
}
/**
* Custom form submit function.
*/
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);
}
}
Functions
Name | Description |
---|---|
basic_cart_entity_insert | Implements hook_entity_insert(). |
basic_cart_entity_presave | Implements hook_entity_presave(). |
basic_cart_form_alter | Implements hook_form_alter(). |
basic_cart_mail | Implements hook_mail(). |
basic_cart_order_send_notifications | Sending emails. |
basic_cart_order_submit | Custom form submit function. |
basic_cart_order_tokens_format | Callback function for formatting the order tokens. |
basic_cart_page_attachments | Implements hook_page_attachments(). |
basic_cart_theme | Implements hook_theme() for node entities. |
basic_cart_tokens | Implements hook_tokens(). |
basic_cart_token_info | Implements hook_token_info(). |
basic_cart_user_login | Implements hook_user_login(). |