• Resolved donlee101

    (@donlee101)


    Hi,

    I found this function on github, but it’s out of date now!
    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();
    }
    }
    }

    Can I have up to date function to remove related products when upsell products are defined? Many thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Stuart Duff – a11n

    (@stuartduff)

    Automattic Happiness Engineer

    Hi @donlee101,

    The code below functions correctly when tested using a default theme like Twenty Twenty. If this does not work for your theme that indicates the default WooCommerce action hooks have possibly been changed by the theme your using.

    If you search your themes codebase for woocommerce_upsell_display you should be able to locate those and alter the code below as required.

    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();
    		}
    	}
    }

    I hope this helps.

    Thread Starter donlee101

    (@donlee101)

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Remove related when using Upsell’ is closed to new replies.