<?php namespace Drupal\app_core\Plugin\views\filter; use Drupal\views\Plugin\views\filter\FilterPluginBase; /** * Filter for recommended products. * * @ingroup views_filter_handlers * * @ViewsFilter("recommended_products") */ class RecommendedProductsFilter extends FilterPluginBase { public function query() { $this->ensureMyTable(); $product_id = $this->view->args[0]; $product = \Drupal::entityTypeManager() ->getStorage('commerce_product') ->load($product_id); $term = $product->get('app_group')->referencedEntities()[0]; // Get all products belonging to the same term (group). $products = \Drupal::entityTypeManager() ->getStorage('commerce_product') ->loadByProperties([ 'app_group' => $term->id(), ]); $product_ids = []; array_walk($products, function($product) use (&$product_ids) { $product_ids[] = $product->id(); }); $this->query->addWhere(0, 'product_id', $product_ids, 'IN'); // $product_top_group = $this->findProductsTopGroup($term); // $child_terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('group', $product_top_group->id()); // $acceptable_terms = []; // foreach ($child_terms as $child_term) { // $acceptable_terms[] = $child_term->name; // } // $products = \Drupal::entityTypeManager()->getStorage('commerce_product')->loadMultiple(); $mile = 'mile'; } // public function findProductsTopGroup($term) { // if ($term->hasField('parent') && $term->get('parent')->target_id !== '0') { // $term = $term->get('parent')->referencedEntities()[0]; // return $this->findProductsTopGroup($term); // } // else { // return $term; // } // // } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter