Replace Upsells with products from a specific product category in Woocommerce 3

StephanG

I have the following case - So far I've always set the Upsell products custom, 1 by 1, as choosing a product category in the admin is not possible.

Now after a big cleaning of products that are no longer available, there are many product pages on the site with 1, 2 or 3 instead of the default number of 4 Upsell products, which breaks the design.

Is there a way to replace those upsells with products from a specific product category?

Any help is appreciated.

LoicTheAztec

To replace woocommerce upsells by products from a specific product category use the following code (where you will define your product category(ies) slug(s)):

add_filter( 'woocommerce_product_get_upsell_ids', 'custom_upsell_ids', 20, 2 );
function custom_upsell_ids( $upsell_ids, $product ){
    // HERE define your product categories slugs in the array
    $product_categories = array( 't-shirts' ); // <=====  <=====  <=====  <=====  <=====

    // Return the custom query
    return wc_get_products( array(
        'status'    => 'publish', // published products
        'limit'     => 4, // 4 products
        'category'  => $product_categories, // Product categories
        'orderby'   => 'rand', // Random order
        'exclude'   => array( $product->get_id() ), // Exclude current product
        'return'    => 'ids', // Query returns only IDs
    ) );
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

The query is set to display 4 products from a specific product category in a random order (excluding the current product).

Official documentation: The wc_get_products() and WC_Product_Query

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Display products from specific product category in Woocommerce

display woocommerce upsells products under product gallery

Remove "No products found" from a specific product category in Woocommerce

Hide products having a specific product category in Woocommerce

Exclude a product category from Woocommerce related products

Exclude a product category from Related products in WooCommerce

Show products from a specific category in the product list

Exclude Woocommerce products in recently viewed products widget from a product category

Enable shipping address for specific virtual products from a product category in Woocommerce checkout

Redirect when buying products from a certain product category in Woocommerce

Sort Woocommerce Products from a product category and custom meta_key

How to get all products from current WooCommerce product category?

Products dropdown from same category inside Woocommerce product short description

Exclude products from a product category on a specific page in Entrada theme

Hide cart coupon field for specific products category in Woocommerce 3

Get all WooCommerce products from a category that have specific meta data

Woocommerce how to display products from specific category on archive page

Set custom quantity arguments to products from specific category in WooCommerce

How to hide out of stock products from Cross-sells and Upsells products in WooCommerce?

Woocommerce adding custom product count to products only in specific category and in specific page

Automatically assign products to a defined product category in WooCommerce

Adding "Sale" product category to products that are on sale in Woocommerce

Move Upsells after Related products section in WooCommerce

Adding products from specific Ids in WooCommerce product custom tab

Woocommerce products from category loop

Change the add to cart text for a specific product category in Woocommerce 3

Exclude specific product ids from a category price suffix change in WooCommerce

Disable other product categories for a cart item from specific category in Woocommerce

Send email if woocommerce order has items from a specific product category