Related products useful functions

/**
 * Change number of related products output
 */
function woo_related_products_limit() {
    global $product;

    $args['posts_per_page'] = 6;
    return $args;
}
function twstudio_related_products_args( $args ) {
    $args['posts_per_page'] = 5; // 5 related products
    $args['columns'] = 2; // arranged in 2 columns
    return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'twstudio_related_products_args', 20 );



/**
 * Remove category from Related products
 */
function twstudio_exclude_product_category_from_related_products( $related_posts, $product_id, $args  ){
    $term_slug = 'ukategorisert';

    $exclude_ids = wc_get_products( array(
        'status'    => 'publish',
        'limit'     => -1,
        'category'  => array($term_slug),
        'return'    => 'ids',
    ) );

    return array_diff( $related_posts, $exclude_ids );
}
add_filter( 'woocommerce_related_products', 'twstudio_exclude_product_category_from_related_products', 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *