0

I have build a Related Products view that supposed to show latest products which in the same category of the current product being viewed.

so I set a contextual filter for the category_id field, but couldn't find anyway to get the commerce product category id.

Drupal 8 / Commerce 2

0

I needed a block to display related products for the current viewed product by showing products from the same category.

To do that you can use the same steps used in the 'related article' tutorial.

but first you have to add the ability to extract term id from product page (like the way it is extracted from node page), so you have to modify

core\modules\taxonomy\src\Plugin\views\argument_default\Tid.php

like this

include this namespace:

use Drupal\commerce_product\Entity\ProductInterface;

look for:

    else {
      return implode($this->options['anyall'], array_keys($taxonomy));
    }
  }

replace with:

    else {
      return implode($this->options['anyall'], array_keys($taxonomy));
    }
  } elseif (($product = $this->routeMatch->getParameter('commerce_product')) && $product instanceof ProductInterface) {
    $taxonomy = [];
    foreach ($product->getFieldDefinitions() as $field) {
      if ($field->getType() == 'entity_reference' && $field->getSetting('target_type') == 'taxonomy_term') {
        $taxonomy_terms = $product->{$field->getName()}->referencedEntities();
        /** @var \Drupal\taxonomy\TermInterface $taxonomy_term */
        foreach ($taxonomy_terms as $taxonomy_term) {
          $taxonomy[$taxonomy_term->id()] = $taxonomy_term->bundle();
        }
      }
    }
    if (!empty($this->options['limit'])) {
      $tids = [];
      // filter by vocabulary
      foreach ($taxonomy as $tid => $vocab) {
        if (!empty($this->options['vids'][$vocab])) {
          $tids[] = $tid;
        }
      }
      return implode($this->options['anyall'], $tids);
    }
    // Return all tids.
    else {
      return implode($this->options['anyall'], array_keys($taxonomy));
    }
  }
|improve this answer|||||
  • Is there a way to do this that doesn't involve modifying core? Modifying core is bad practice and can lead to issues when core is updated. – fallenturtle Aug 7 '19 at 18:44
0

FWIW, I found a work around that doesn't involve modifying core, though its much less elegant and only really applicable if you only have a few terms. It also requires the twig_tweak module. Basically I created a view block for each Related Products block filtered by one of the terms. Then in the twig template for the product view I created a bunch of conditionals for each term that checks for the term and then based on the result outputs the appropriate view: Like:

{% if 'term1' in product.field_my_term.0['#plain_text']  %}
  {{ drupal_view('related_products', 'block_1') }}
{% elseif 'term2' in product.field_my_term.0['#plain_text'] %}
  {{ drupal_view('related_products', 'block_2') }}
{% elseif 'term3' in product.field_my_term.0['#plain_text'] %}
  {{ drupal_view('related_products', 'block_3') }}
{% endif %}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.