Skip to content

Instantly share code, notes, and snippets.

@roykho
Last active October 28, 2022 16:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roykho/1062c708af772412fe7e to your computer and use it in GitHub Desktop.
Save roykho/1062c708af772412fe7e to your computer and use it in GitHub Desktop.
WooCommerce: Remove related products when up-sell products are defined
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_action( 'woocommerce_after_single_product_summary', 'related_upsell_products', 15 );
function related_upsell_products() {
global $product;
if ( isset( $product ) && is_product() ) {
$upsells = version_compare( WC_VERSION, '3.0', '<' ) ? $product->get_upsells() : $product->get_upsell_ids();
if ( count( $upsells ) > 0 ) {
woocommerce_upsell_display();
} else {
woocommerce_upsell_display();
woocommerce_output_related_products();
}
}
}
@kevinmamaqi
Copy link

Thank you!

@arianfar62
Copy link

Thank you roykho!
You made my site more beautiful.
This is my site
http://sonarweb.ir/
i wish Health For you and your family!
thank you github.com.

@assoscoupa
Copy link

Hi, nice snippet but in line 10 my site return an PHP notice:

“Notice: WC_Product::get_upsells is deprecated since version 3.0! Use WC_Product::get_upsell_ids instead..."

How do I replace "get_upsell" ? I tried with "get_upsell_ids" but it didn't work.

If you have kindness helped me to solve it a bit.

@kamuz
Copy link

kamuz commented Aug 3, 2020

Great, thank you. It's working

@kamuz
Copy link

kamuz commented Aug 3, 2020

I think you does not need calling woocommerce_upsell_display(); inside else

@kjobber
Copy link

kjobber commented Nov 11, 2020

I also had this problem (using storefront) but made something like

function diverse_related_upsell() {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}

add_action( 'woocommerce_after_single_product_summary', 'related_upsell_products', 15 );

function related_upsell_products() {
global $product;

if ( isset( $product ) && is_product() ) {
	$upsells = version_compare( WC_VERSION, '3.0', '<' ) ? $product->get_upsells() : $product->get_upsell_ids();

	if ( count( $upsells ) > 0 ) {
		diverse_related_upsell();	
	} 
}

}
works fine

@chris-mccarston
Copy link

Hey,

not quite related to these questions, but related to upsells.

We would like to remove the link to the single product page of an upsell product.

So we're on a single product page of product A. Below the "Add to Cart" is an Upsell box with Product B, and this link to the single page of product B must be removed.

Would be awesome if someone has an idea how to achieve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment