Guest User

Untitled

a guest
Apr 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. /**
  2.  * Implements hook_field_formatter_view().
  3.  */
  4. function custom_commerce_cart_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  5.   $settings = array_merge(field_info_formatter_settings($display['type']), $display['settings']);
  6.   $result = array();
  7.  
  8.   // Collect the list of product IDs.
  9.   $product_ids = array();
  10.  
  11.   foreach ($items as $delta => $item) {
  12.     $product_ids[] = $item['product_id'];
  13.   }
  14.  
  15.   if ($display['type'] == 'custom_commerce_cart_add_to_cart_form') {
  16.     // Load the referenced products.
  17.     $products = commerce_product_load_multiple($product_ids);
  18.  
  19.     // Check to ensure products are referenced, before returning results.
  20.     if (!empty($products)) {
  21.       if ($cache = cache_get('products_nid_' . $entity->nid)) {
  22.         $products = $cache->data;
  23.       } else {
  24.         // Sort products by attribute name if exists and sort alphabetically is actived.
  25.         // Products types with attributes, are required. If the first product have attributes, all have them it.
  26.         if ($settings['sort_alphabetically'] && isset(current($products)->field_product_accessory[LANGUAGE_NONE]['0']['tid'])) {
  27.           $terms = array();
  28.           foreach ($products as $product) {
  29.             $terms[] = $product->field_product_accessory[LANGUAGE_NONE]['0']['tid'];
  30.           }
  31.           $terms = taxonomy_term_load_multiple(array_unique($terms));
  32.           foreach ($terms as $key => $term) {
  33.             $termsSort[$key] = $term->name;
  34.           }
  35.           asort($termsSort);
  36.           $tmp_products = $products;
  37.           unset($products);
  38.           foreach ($termsSort as $key => $term) {
  39.             foreach ($tmp_products as $product) {
  40.               if ($product->field_product_accessory[LANGUAGE_NONE]['0']['tid'] == $key) {
  41.                 $products[] = $product;
  42.               }
  43.             }
  44.           }
  45.           cache_set('products_nid_' . $entity->nid, $products, 'cache');
  46.         }
  47.       }
  48.       $type = !empty($settings['line_item_type']) ? $settings['line_item_type'] : 'product';
  49.       $line_item = commerce_product_line_item_new(reset($products), $settings['default_quantity'], 0, array(), $type);
  50.       $line_item->data['context']['product_ids'] = array_keys($products);
  51.       $line_item->data['context']['add_to_cart_combine'] = $settings['combine'];
  52.  
  53.       $result[] = array(
  54.         '#arguments' => array(
  55.           'form_id' => custom_commerce_cart_add_to_cart_form_id($product_ids),
  56.           'line_item' => $line_item,
  57.           'show_quantity' => $settings['show_quantity'],
  58.           'sort_alphabetically' => $settings['sort_alphabetically'],
  59.         ),
  60.       );
  61.     }
  62.   }
  63.  
  64.   return $result;
  65. }
Add Comment
Please, Sign In to add comment