class Utility in Basic cart 8.5
Same name and namespace in other branches
- 8.6 src/Utility.php \Drupal\basic_cart\Utility
- 8 src/Utility.php \Drupal\basic_cart\Utility
- 8.0 src/Utility.php \Drupal\basic_cart\Utility
- 8.2 src/Utility.php \Drupal\basic_cart\Utility
- 8.3 src/Utility.php \Drupal\basic_cart\Utility
- 8.4 src/Utility.php \Drupal\basic_cart\Utility
Utilty functions for basic cart.
Hierarchy
Expanded class hierarchy of Utility
8 files declare their use of Utility
- AddToCartItem.php in src/
Plugin/ Field/ FieldType/ AddToCartItem.php - basic_cart.inc in ./
basic_cart.inc - Basic cart inc file.
- basic_cart.module in ./
basic_cart.module - Basic cart module file.
- CartBlock.php in src/
Plugin/ Block/ CartBlock.php - CartController.php in src/
Controller/ CartController.php
File
- src/
Utility.php, line 13
Namespace
Drupal\basic_cartView source
class Utility extends Settings {
private $storage;
const FIELD_ADDTOCART = 'addtocart';
const FIELD_ORDERCONNECT = 'orderconnect';
const BASICCART_ORDER = 'basic_cart_order';
/**
* Get Storage session or table.
*/
private static function getStorage() {
$user = \Drupal::currentUser();
$config = self::cartSettings();
$storage = new CartStorageSelect($user, $config
->get('use_cart_table'));
return $storage;
}
/**
* Check if its basic cart.
*
* @param string $bundle
* Content type name.
*
* @return bool
* Returns true or false
*/
public static function isBasicCartOrder($bundle) {
if ($bundle == self::BASICCART_ORDER) {
return TRUE;
}
return FALSE;
}
/**
* Function for shopping cart retrieval.
*
* @param int $nid
* We are using the node id to store the node in the shopping cart.
*
* @return mixed
* Returning the shopping cart contents.
* An empty array if there is nothing in the cart
*/
public static function getCart($nid = NULL) {
$storage = static::getStorage();
return $storage
->getCart($nid);
}
/**
* Returns the final price for the shopping cart.
*
* @return mixed $total_price
* The total price for the shopping cart.
*/
/**
* Callback function for cart/remove/.
*
* @param int $nid
* We are using the node id to remove the node in the shopping cart.
*/
public static function removeFromCart($nid = NULL) {
$nid = (int) $nid;
$storage = static::getStorage();
$storage
->removeFromCart($nid);
}
/**
* Shopping cart reset.
*/
public static function emptyCart() {
$storage = static::getStorage();
$storage
->emptyCart();
}
/**
* Add to cart.
*
* @param int $id
* Node id.
* @param array $params
* Quantity and entity types.
*/
public static function addToCart($id, array $params = array()) {
$storage = static::getStorage();
$storage
->addToCart($id, $params);
}
/**
* Login Sync.
*/
public function loggedInActionCart() {
$storage = static::getStorage();
return $storage
->loggedInActionCart();
}
/**
* Returns the fields we need to create.
*
* @param string $type
* Type of field sets to be created.
*
* @return mixed
* Key / Value pair of field name => field type.
*/
public static function getFieldsConfig($type = NULL) {
$config = self::cartSettings();
$fields['bundle_types'] = $config
->get('content_type');
foreach ($config
->get('content_type') as $key => $value) {
if ($value) {
$bundles[$key] = $key;
}
}
$fields['bundle_types'] = $bundles;
if ($type == self::FIELD_ORDERCONNECT) {
$fields['bundle_types'] = array(
'basic_cart_order' => 'basic_cart_order',
);
$fields['fields'] = array(
'basic_cart_content' => array(
'type' => 'entity_reference',
'entity_type' => 'node',
'bundle' => 'basic_cart_order',
'title' => t('Basic cart content'),
'label' => t('Basic cart content'),
'required' => FALSE,
'description' => t('Basic cart reference content'),
'cardinality' => -1,
'formatter' => array(
'default' => array(
'label' => 'inline',
'type' => 'entity_reference_entity_view',
'settings' => [
'view_mode' => 'basic_cart_order',
],
),
'search_result' => 'default',
'teaser' => 'default',
),
'widget' => array(
'type' => 'entity_reference_autocomplete',
'cardinality' => -1,
),
'settings' => array(
'handler' => 'default:node',
'handler_settings' => array(
"target_bundles" => $bundles,
),
),
),
);
}
else {
$fields['fields'] = array(
'add_to_cart_price' => array(
'type' => 'decimal',
'entity_type' => 'node',
'title' => t($config
->get('price_label')),
'label' => t($config
->get('price_label')),
'required' => FALSE,
'description' => t("Please enter this item's price."),
'cardinality' => 1,
'widget' => array(
'type' => 'number',
),
'formatter' => array(
'default' => array(
'label' => 'inline',
'type' => 'number_decimal',
'weight' => 11,
),
'search_result' => 'default',
'teaser' => 'default',
),
),
'add_to_cart' => array(
'type' => 'addtocart',
'entity_type' => 'node',
'title' => t($config
->get('add_to_cart_button')),
'label' => t($config
->get('add_to_cart_button')),
'required' => FALSE,
'description' => 'Enable add to cart button',
'cardinality' => 1,
'widget' => array(
'type' => 'addtocart',
),
'formatter' => array(
'default' => array(
'label' => 'hidden',
'weight' => 11,
'type' => $config
->get('quantity_status') ? 'addtocartwithquantity' : 'addtocart',
),
'search_result' => array(
'label' => 'hidden',
'weight' => 11,
'type' => 'addtocart',
),
'teaser' => array(
'label' => 'hidden',
'weight' => 11,
'type' => 'addtocart',
),
),
),
);
}
return (object) $fields;
}
/**
* Create Fields for content type basic cart enabled.
*
* @param string $type
* Type fields to be created.
*/
public static function createFields($type = NULL) {
$fields = $type == self::FIELD_ORDERCONNECT ? self::getFieldsConfig(self::FIELD_ORDERCONNECT) : self::getFieldsConfig();
$view_modes = \Drupal::entityManager()
->getViewModes('node');
$storage = \Drupal::entityTypeManager()
->getStorage('entity_view_display');
foreach ($fields->fields as $field_name => $config) {
$field_storage = FieldStorageConfig::loadByName($config['entity_type'], $field_name);
if (empty($field_storage)) {
FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => $config['entity_type'],
'type' => $config['type'],
'cardinality' => $config['cardinality'],
))
->save();
}
}
foreach ($fields->bundle_types as $bundle) {
$view_display = $storage
->load($config['entity_type'] . '.' . $bundle . '.basic_cart_order');
if ($view_display == NULL) {
$storage
->create([
'targetEntityType' => $config['entity_type'],
'bundle' => $bundle,
'mode' => 'basic_cart_order',
'status' => TRUE,
])
->save();
}
foreach ($fields->fields as $field_name => $config) {
$config_array = array(
'field_name' => $field_name,
'entity_type' => $config['entity_type'],
'bundle' => $bundle,
'label' => $config['label'],
'required' => $config['required'],
'cardinality' => $config['cardinality'],
);
if (isset($config['settings'])) {
$config_array['settings'] = $config['settings'];
}
$field = FieldConfig::loadByName($config['entity_type'], $bundle, $field_name);
if (empty($field) && $bundle !== "" && !empty($bundle)) {
FieldConfig::create($config_array)
->save();
}
if ($bundle !== "" && !empty($bundle)) {
if (!empty($field)) {
$field
->setLabel($config['label'])
->save();
$field
->setRequired($config['required'])
->save();
}
if ($config['widget']) {
entity_get_form_display($config['entity_type'], $bundle, 'default')
->setComponent($field_name, $config['widget'])
->save();
}
if ($config['formatter']) {
foreach ($config['formatter'] as $view => $formatter) {
if (isset($view_modes[$view]) || $view == "default") {
$formatter_view = entity_get_display($config['entity_type'], $bundle, $view);
if ($view != 'basic_cart_order') {
$formatter_view
->setComponent($field_name, !is_array($formatter) ? $config['formatter']['default'] : $config['formatter']['default']);
}
elseif ($view == 'basic_cart_order' && $field_name == "add_to_cart_price") {
$formatter_view
->setComponent($field_name, !is_array($formatter) ? $config['formatter']['default'] : $config['formatter']['default']);
}
elseif ($view == 'basic_cart_order' && $field_name != "add_to_cart_price") {
$formatter_view
->removeComponent($field_name);
}
$formatter_view
->save();
}
}
}
}
}
// Display mode basic_cart_order add fields to title and
// price feild in formatter and remove other fields.
$view_display = $storage
->load($config['entity_type'] . '.' . $bundle . '.basic_cart_order');
if ($view_display != NULL) {
foreach (\Drupal::entityManager()
->getFieldDefinitions($config['entity_type'], $bundle) as $field_name => $field_definition) {
if (!empty($field_definition
->getTargetBundle())) {
if (!in_array($field_definition
->getName(), [
'add_to_cart_price',
])) {
$view_display
->removeComponent($field_definition
->getName());
}
else {
$view_display
->setComponent($field_definition
->getName(), [
'type' => 'number_decimal',
]);
}
}
}
if ($view_display != NULL) {
$view_display
->removeComponent('links');
$view_display
->save();
}
}
}
}
/**
* Create Order connect Fields.
*/
public static function orderConnectFields() {
self::createFields(self::FIELD_ORDERCONNECT);
}
/**
* Render function.
*
* @param string $template_name
* Name of the template.
*/
public static function render($template_name = 'basic-cart-cart-template.html.twig', $variable = NULL) {
$twig = \Drupal::service('twig');
$template = $twig
->loadTemplate(drupal_get_path('module', 'basic_cart') . '/templates/' . $template_name);
return $template
->render([
'basic_cart' => $variable ? $variable : self::getCartData(),
]);
}
/**
* Get Cart Data.
*/
public static function getCartData() {
$config = self::cartSettings();
$cart = self::getCart();
//$quantity_enabled = $config->get('quantity_status');
$total_price = self::getTotalPrice();
$cart_cart = isset($cart['cart']) ? $cart['cart'] : array();
$basic_cart = array();
$basic_cart['config']['quantity_enabled'] = $config
->get('quantity_status');
$basic_cart['empty']['text'] = $config
->get('empty_cart');
if (empty($cart_cart)) {
$basic_cart['empty']['status'] = TRUE;
}
else {
if (is_array($cart_cart) && count($cart_cart) >= 1) {
foreach ($cart_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();
$url = new Url('entity.node.canonical', [
"node" => $nid,
]);
$link = new Link($title[0]['value'], $url);
$basic_cart['data']['contents'][$nid] = [
"quantity" => $cart['cart_quantity'][$nid],
'price_value' => isset($price_value[0]) ? self::formatPrice($price_value[0]['value']) : '',
'link' => $link
->toString(),
];
}
$basic_cart['config']['total_price_label'] = $config
->get('total_price_label');
$basic_cart['config']['total_price'] = self::formatPrice($total_price->total);
$basic_cart['config']['vat_enabled'] = $config
->get('vat_state');
$basic_cart['config']['vat_label'] = 'Total VAT';
$basic_cart['config']['total_price_vat'] = self::formatPrice($total_price->vat);
$basic_cart['config']['view_cart_button'] = $config
->get('view_cart_button');
$url = new Url('basic_cart.cart');
$basic_cart['config']['view_cart_url'] = $url
->toString();
$basic_cart['empty']['status'] = FALSE;
}
}
return $basic_cart;
}
/**
* Get Total price data.
*/
public static function getTotalPriceMarkupData() {
$config = Utility::cartSettings();
$price = Utility::getTotalPrice();
$total = Utility::formatPrice($price->total);
$vat_is_enabled = (int) $config
->get('vat_state');
$vat_value = !empty($vat_is_enabled) && $vat_is_enabled ? Utility::formatPrice($price->vat) : 0;
$basic_cart = array(
'total_price' => $total,
'vat_enabled' => $vat_is_enabled,
'vat_value' => $vat_value,
'total_price_label' => $config
->get('total_price_label'),
'total_vat_label' => 'Total VAT',
);
return $basic_cart;
}
/**
* Get Quantity prefix data.
*
* @param int $nid
* Node id of content.
*/
public static function quantityPrefixData($nid) {
global $base_url;
$langcode = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$url = new Url('basic_cart.cartremove', array(
"nid" => $nid,
));
$cart = Utility::getCart($nid);
$basic_cart = array();
$basic_cart['delete_url'] = $url
->toString();
$basic_cart['module_url'] = $base_url . '/' . drupal_get_path('module', 'basic_cart');
$basic_cart['notempty'] = FALSE;
if (!empty($cart['cart'])) {
$basic_cart['notempty'] = TRUE;
$unit_price = $cart['cart']
->getTranslation($langcode)
->get('add_to_cart_price')
->getValue();
$unit_price = isset($unit_price[0]['value']) ? $unit_price[0]['value'] : 0;
$title = $cart['cart']
->getTranslation($langcode)
->get('title')
->getValue()[0]['value'];
// Price and currency.
$url = new Url('entity.node.canonical', array(
"node" => $nid,
));
$link = new Link($title, $url);
$unit_price = isset($unit_price) ? $unit_price : 0;
$unit_price = Utility::formatPrice($unit_price);
$basic_cart['unit_price'] = $unit_price;
$basic_cart['title_link'] = $link
->toString();
}
return $basic_cart;
}
/**
* Get Cart count.
*/
public static function cartCount() {
$cart = Utility::getCart();
return isset($cart['cart_quantity']) ? array_sum($cart['cart_quantity']) : 0;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Settings:: |
protected | property | ||
Settings:: |
protected | property | ||
Settings:: |
public static | function | Get the cart configuration. | |
Settings:: |
public static | function | Return Cart updated message. | |
Settings:: |
public | function | Force Extending class to define this method. | |
Settings:: |
public static | function | Formats the input $price in the desired format. | |
Settings:: |
public static | function | Returns the final price for the shopping cart. | |
Settings:: |
public static | function | Returns the available price formats. | |
Utility:: |
private | property | ||
Utility:: |
public static | function | Add to cart. | |
Utility:: |
constant |
Overrides Settings:: |
||
Utility:: |
public static | function | Get Cart count. | |
Utility:: |
public static | function | Create Fields for content type basic cart enabled. | |
Utility:: |
public static | function | Shopping cart reset. | |
Utility:: |
constant |
Overrides Settings:: |
||
Utility:: |
constant |
Overrides Settings:: |
||
Utility:: |
public static | function | Function for shopping cart retrieval. | |
Utility:: |
public static | function | Get Cart Data. | |
Utility:: |
public static | function | Returns the fields we need to create. | |
Utility:: |
private static | function | Get Storage session or table. | |
Utility:: |
public static | function | Get Total price data. | |
Utility:: |
public static | function | Check if its basic cart. | |
Utility:: |
public | function | Login Sync. | |
Utility:: |
public static | function | Create Order connect Fields. | |
Utility:: |
public static | function | Get Quantity prefix data. | |
Utility:: |
public static | function | Callback function for cart/remove/. | |
Utility:: |
public static | function | Render function. |