commerce_cart_categories-8.x-1.1/commerce_cart_categories.module

commerce_cart_categories.module
<?php

use \Drupal\views\ViewExecutable;
use \Drupal\views\Plugin\views\cache\CachePluginBase;

/**
 * Implements hook_views_post_build(ViewExecutable $view)
 */

function commerce_cart_categories_views_post_build(ViewExecutable $view) {
  if($view->id() == "commerce_cart_block" || $view->id() == "commerce_cart_form") {

    // Get the session of current user
    $session = \Drupal::request()->getSession();

    // Get the name of taxonomy reference field of the product from Config
    $taxonomy_field_name = \Drupal::config('commerce_cart_categories.conf')->get('products_taxonomy_field');

    // If name of taxonomy reference field is not yet set, we have to clear possible module data from session
    if(!isset($taxonomy_field_name) || $taxonomy_field_name == "") {
      $session->set('user_current_cart', []);
      return;
    };

    $sorted_view_results = [];
    $lineitems_by_taxonomy = [];
    $terms_ids = [];

    // Save view results to the array
    foreach($view->result AS $result) {
      $sorted_view_results[$result->_relationship_entities['order_items']->id()] = $result;
    };

    /* get current store, @var CurrentStoreInterface $cs */
    $cs = \Drupal::service('commerce_store.current_store');
    /* get current shopping cart @var CartProviderInterface $cpi */
    $cpi = \Drupal::service('commerce_cart.cart_provider');
    $cart = $cpi->getCart('default', $cs->getStore());

    // Processing of line items from the cart
    foreach($cart->getItems() AS $lineitem) {
      $category_f = NULL;
      // if for current product the field $taxonomy_field_name is provided...
      if($lineitem->getPurchasedEntity()->getProduct()->__isset($taxonomy_field_name)) {
        // ... acquire term id it references
        $category_f = $lineitem->getPurchasedEntity()->getProduct()->get($taxonomy_field_name);
      };
      if(isset($category_f)) {
        // Get term IDs corresponding to product of each line item, and prepare line items for sorting
        $category_field = $category_f->getValue();
        $lineitems_by_taxonomy[$lineitem->id()] = $category_field[0]['target_id'];
        $terms_ids[$category_field[0]['target_id']] = $category_field[0]['target_id']; 
      } else {
        // if current product does not have field {$taxonomy_field_name} attached, move this product outside of categories
        $lineitems_by_taxonomy[$lineitem->id()] = 0;
      };
    };

    // Sorting of line items by term ID
    natcasesort($lineitems_by_taxonomy);

    // Load taxonomy terms to get their titles
    $category_terms = \Drupal\taxonomy\Entity\Term::loadMultiple($terms_ids);

    // Transfer sorted line items back to view
    $iterator = 0;
    foreach($lineitems_by_taxonomy AS $key => $value) {
      $view->result[$iterator] = $sorted_view_results[$key];
      $iterator += 1;
    };

    // Generating list of category titles and positions where they should be inserted in the products list
    $custom_categories = [];
    $prev_tid = -1;
    foreach($lineitems_by_taxonomy AS $value) {
      if($value != $prev_tid && $value != 0) {
        $custom_categories[] = $category_terms[$value]->get('name')->getValue()[0]['value'];
        $prev_tid = $value;
      } else {
        $custom_categories[] = "";
      };
    };

    // Let's save generated list of categories to session
    $session->set('user_current_cart', $custom_categories);
  };
}

/**
 * Implements hook_theme()
 * After declaring our custom templates for commerce cart, we need to instruct Drupal to pass them usual variables from $view
 */

function commerce_cart_categories_theme($existing, $type, $theme, $path) {
  return [
    'views_view_table__commerce_cart_block' => [
      'base hook' => 'views',
    ],
    'views_view_table__commerce_cart_form' => [
      'base hook' => 'views',
    ],
  ];
}

/**
 * Implements hook_theme_registry_alter()
 * Here we instruct Drupal to use our custom templates for cart block and cart form
 */

function commerce_cart_categories_theme_registry_alter(&$theme_registry){
  $theme_registry['views_view_table__commerce_cart_block']['path'] = drupal_get_path('module','commerce_cart_categories') . '/templates';
  $theme_registry['views_view_table__commerce_cart_block']['template'] = 'views-view-table--commerce-cart-block';
  $theme_registry['views_view_table__commerce_cart_form']['path'] = drupal_get_path('module','commerce_cart_categories') . '/templates';
  $theme_registry['views_view_table__commerce_cart_form']['template'] = 'views-view-table--commerce-cart-block';
}

/**
 * Implements hook_preprocess_HOOK()
 * Here we transfer additional variables needeed to theme our categories in the commerce cart block
 */

function commerce_cart_categories_preprocess_views_view_table__commerce_cart_block(&$variables) {
  // Session is used to transfer data from commerce_cart_categories_views_pre_render() to here
  $session = \Drupal::request()->getSession();
  $data = $session->get('user_current_cart');
  $variables['custcategories'] = isset($data) ? $data : [];
}

/**
 * Implements hook_preprocess_HOOK()
 * Here we transfer additional variables needeed to theme our categories in the commerce cart form
 */

function commerce_cart_categories_preprocess_views_view_table__commerce_cart_form(&$variables) {
  // Session is used to transfer data from commerce_cart_categories_views_pre_render() to here
  $session = \Drupal::request()->getSession();
  $data = $session->get('user_current_cart');
  $variables['custcategories'] = isset($data) ? $data : [];
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc