bobbingwide / vgc

VGC - Garden Vista Group theme
GNU General Public License v2.0
0 stars 0 forks source link

Same related product appearing 3 times #67

Open bobbingwide opened 10 months ago

bobbingwide commented 10 months ago

When viewing the https://gardenvista.co.uk/store/vista-apent-garden-room/ product the Related Products section displayed the Regency Capri Summerhouse 3 times.

image

bobbingwide commented 10 months ago

woocommerce_output_related_products is where we have to start looking for

For this particular product, WooCommerce looks for products which have the same product categories. And this product has a lot of product categories - 78 in all - since it's one product that comes in many sizes. image

The query that WooCommerce runs is

                SELECT DISTINCT ID FROM vgcwp_posts p
              LEFT JOIN ( SELECT object_id FROM vgcwp_term_relationships WHERE term_taxonomy_id IN ( 7 ) ) AS exclude_join ON exclude_join.object_id = p.ID INNER JOIN ( SELECT object_id FROM vgcwp_term_relationships INNER JOIN vgcwp_term_taxonomy using( term_taxonomy_id ) WHERE term_id IN ( 597,325,528,596,599,421,598,326,530,594,593,542,592,545,591,546,547,590,327,529,595,533,588,534,587,548,586,549,585,550,584,531,589,532,537,582,581,538,539,580,540,579,541,578,535,583,536,621,623,619,622,624,620,600,521,323,602,601,319,634,242,321,23,79,251,184,237,553,318,238,168,252,191,255,243,189,253,1022 ) ) AS include_join ON include_join.object_id = p.ID 
                WHERE 1=1
                AND p.post_status = 'publish'
                AND p.post_type = 'product'

             AND exclude_join.object_id IS NULL AND p.ID NOT IN ( 0,8962 ) 
                LIMIT 23

See also the filters in wc_get_related_products().

bobbingwide commented 10 months ago

BTW: There's some wacky code in WooCommerce that takes the $limit you pass and adds 10 to it in two different places.

See wc-get_related_products()

$related_posts = $data_store->get_related_products( $cats_array, $tags_array, $exclude_ids, $limit + 10, $product_id );

and, in WC_Product_Data_Store_CPT

public function get_related_products( $cats_array, $tags_array, $exclude_ids, $limit, $product_id ) {
        global $wpdb;

        $args = array(
            'categories'  => $cats_array,
            'tags'        => $tags_array,
            'exclude_ids' => $exclude_ids,
            'limit'       => $limit + 10,

        );

        $related_product_query = (array) apply_filters( 'woocommerce_product_related_posts_query', $this->get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit + 10 ), $product_id, $args );