{"id":1099,"date":"2020-03-31T13:30:43","date_gmt":"2020-03-31T11:30:43","guid":{"rendered":"https:\/\/www.welaunch.io\/en\/?p=1099"},"modified":"2020-06-20T12:28:24","modified_gmt":"2020-06-20T10:28:24","slug":"show-only-last-woocommerce-category-products-in-related-section","status":"publish","type":"post","link":"https:\/\/www.welaunch.io\/en\/2020\/03\/show-only-last-woocommerce-category-products-in-related-section\/","title":{"rendered":"Show only last WooCommerce category products in related section"},"content":{"rendered":"
If you want to only show products from the last category inside your WooCommerce related products section, then place the code below into your functions.php.<\/p>\n
\r\nadd_filter( 'woocommerce_get_related_product_cat_terms', 'last_child_cats_only', 10, 2 );\r\nfunction last_child_cats_only( $term_ids, $product ) {\r\n\t\r\n\tif(!$product) {\r\n\t\treturn $term_ids;\r\n\t}\r\n\r\n $cats_array = array();\r\n $terms = wp_get_post_terms($product, 'product_cat');\r\n foreach ( $terms as $key => $term ){\r\n $check_for_children = get_categories(array('parent' => $term->term_id, 'taxonomy' => 'product_cat'));\r\n if(empty($check_for_children)){\r\n $cats_array[] = $term->term_id;\r\n }\r\n }\r\n\r\n if(!empty($cats_array)) {\r\n \t$term_ids = $cats_array;\r\n }\r\n\r\n \treturn $term_ids;\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"If you want to only show products from the last category inside your WooCommerce related products section, then place the code below into your functions.php. add_filter( ‘woocommerce_get_related_product_cat_terms’, ‘last_child_cats_only’, 10, 2 ); function last_child_cats_only( $term_ids, $product ) { if(!$product) { return $term_ids; } $cats_array = array(); $terms = wp_get_post_terms($product, ‘product_cat’); foreach ( $terms as $key =>…<\/p>\n","protected":false},"author":1,"featured_media":2749,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1099","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerce"],"_links":{"self":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/comments?post=1099"}],"version-history":[{"count":1,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/1099\/revisions"}],"predecessor-version":[{"id":1100,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/1099\/revisions\/1100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/media\/2749"}],"wp:attachment":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/media?parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/categories?post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/tags?post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}