-1

I use the following command line to exclude specific tags products from related WooCommerce products:

add_filter( 'woocommerce_get_related_product_tag_terms', 'remove_related_tags' );
function remove_related_tags( $terms ) {
  foreach ( $terms as $key => $term ) {
    if ( 'Đồng Hồ Thụy Sỹ' === $term->name ) {
      unset( $terms[ $key ] );
    }
    if ( 'dong-ho-thuy-sy' === $term->slug ) {
      unset( $terms[ $key ] );
    }
    if ( 'Đồng Hồ 6 Kim' === $term->name ) {
      unset( $terms[ $key ] );
    }
    if ( 'Citizen Eco-Drive' === $term->name ) {
      unset( $terms[ $key ] );
    }
    if ( 'Seiko Kinetic' === $term->name ) {
      unset( $terms[ $key ] );
    }
    if ( 'Seiko Solar' === $term->name ) {
      unset( $terms[ $key ] );
    }
    if ( 'Đồng Hồ Dây Da Nam Nữ' === $term->name ) {
      unset( $terms[ $key ] );
    }
  }
  return $terms;
}

But since WooCommerce update version 3 this code doesn't work anymore and has no effect.

Is there any other way I can exclude specific tags products from related products?

closed as off-topic by Michael, Mark Kaplun, Jack Johansson, cjbj, Max Yudin Sep 3 '17 at 10:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

0

The first parameter of the filter gets its value from function wc_get_product_term_ids:

http://hookr.io/filters/woocommerce_get_related_product_tag_terms/

This function returns an array of term_ids instead of objects: https://docs.woocommerce.com/wc-apidocs/source-function-wc_get_product_term_ids.html#860-871

So you can match the term id values directly or you can use get_term_by to get one of the term objects in the comparison.

e.g. getting the term on the right hand side of the comparison:

$termnames = array(
                    'Đồng Hồ Thụy Sỹ',
                    'Đồng Hồ 6 Kim',
                    'Citizen Eco-Drive',
                    'Seiko Kinetic',
                    'Seiko Solar',
                    'Đồng Hồ Dây Da Nam Nữ'
                  );
foreach($terms as $key => $term) {
  $t = get_term_by('id', $term, 'product_tag');
  if(in_array($t->name, $termnames, true) || 'dong-ho-thuy-sy' === $t->slug) {
    unset($terms[$key]);
  }
}
  • Great, looks like it's working with me: D Thanks a lot inarilo :) – Đặng Hải Triều Aug 28 '17 at 3:35

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