WordPress.org

Ready to get started?Download WordPress

Forums

[plugin: WooCommerce] if upsells exist don't display related products (6 posts)

  1. crashjohnson
    Member
    Posted 1 year ago #

    The ultimate goal is to modify the WooCommerce plugin to not display related products if up-sells exist. Woo randomly picks the "related products" from all of your products. so... there is a good chance that the "related products" aren't related at all.

    If one wants to go in and pick their own related-type products they can add the to the up-sells field in the product's edit screen.

    I don't want to entirely remove or not display the related products function or div. I would, however, like the template to perform an if/else statement to conditionally display the related products.

    If $upsells exist
        don't display $related
    else
         display $related

    The code below is from: /wp-content/plugins/woocommerce/templates/single-product, where I believe the appropriate changes can be made. I just don't know enough about php or the way Woo is put together to make the change without breaking something.

    related.php:

    <?php
    /**
     * Related Products
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     1.6.4
     */
    
    global $product, $woocommerce_loop;
    
    $related = $product->get_related();
    
    if ( sizeof($related) == 0 ) return;
    
    $args = apply_filters('woocommerce_related_products_args', array(
    	'post_type'				=> 'product',
    	'ignore_sticky_posts'	=> 1,
    	'no_found_rows' 		=> 1,
    	'posts_per_page' 		=> $posts_per_page,
    	'orderby' 				=> $orderby,
    	'post__in' 				=> $related
    ) );
    
    $products = new WP_Query( $args );
    
    $woocommerce_loop['columns'] 	= $columns;
    
    if ( $products->have_posts() ) : ?>
    
    	<div class="related products">
    
    		<h2><?php _e('Related Products', 'woocommerce'); ?></h2>
    
    		<ul class="products">
    
    			<?php while ( $products->have_posts() ) : $products->the_post(); ?>
    
    				<?php woocommerce_get_template_part( 'content', 'product' ); ?>
    
    			<?php endwhile; // end of the loop. ?>
    
    		</ul>
    
    	</div>
    
    <?php endif;
    
    wp_reset_postdata();

    up-sells.php:

    <?php
    /**
     * Related Products
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     1.6.4
     */
    
    global $product, $woocommerce_loop;
    
    $related = $product->get_related();
    
    if ( sizeof($related) == 0 ) return;
    
    $args = apply_filters('woocommerce_related_products_args', array(
    	'post_type'				=> 'product',
    	'ignore_sticky_posts'	=> 1,
    	'no_found_rows' 		=> 1,
    	'posts_per_page' 		=> $posts_per_page,
    	'orderby' 				=> $orderby,
    	'post__in' 				=> $related
    ) );
    
    $products = new WP_Query( $args );
    
    $woocommerce_loop['columns'] 	= $columns;
    
    if ( $products->have_posts() ) : ?>
    
    	<div class="related products">
    
    		<h2><?php _e('Related Products', 'woocommerce'); ?></h2>
    
    		<ul class="products">
    
    			<?php while ( $products->have_posts() ) : $products->the_post(); ?>
    
    				<?php woocommerce_get_template_part( 'content', 'product' ); ?>
    
    			<?php endwhile; // end of the loop. ?>
    
    		</ul>
    
    	</div>
    
    <?php endif;
    
    wp_reset_postdata();
  2. Aadil_hussain
    Member
    Posted 1 year ago #

    You can do that by adding a conditional tag in content-single-product.php

    <?php
    /**
     * The template for displaying product content in the single-product.php template
     *
     * Override this template by copying it to yourtheme/woocommerce/content-single-product.php
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     1.6.4
     */
    ?>
    <?php
    			global $product;
    
    			$upsells = $product->get_upsells();
    				?>

    remaining code remain same

    <?php
    			/**
    			 * woocommerce_after_single_product_summary hook
    			 *
    			 * @hooked woocommerce_output_product_data_tabs - 10
    			 * @hooked woocommerce_output_related_products - 20
    			 */
    				if ( sizeof($upsells) == 0 )
    				{
    				do_action( 'woocommerce_after_single_product_summary' );
    				}
    				else
    				{
    				remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    				do_action( 'woocommerce_after_single_product_summary' );
    				}
    		?>
    
    	</div><!-- #product-<?php the_ID(); ?> -->
    		</div>
    <?php do_action( 'woocommerce_after_single_product' ); ?>
  3. tongny
    Member
    Posted 1 year ago #

    Dear all, can u tell me how to show the up sell in woocommerce ?

  4. crashjohnson
    Member
    Posted 1 year ago #

    Thanks Aadil but I tried that and it didn't work, for whatever reason. followed your instructions to the letter and it is still displaying related products along with the up-sells. Any additional help would be awesome. You think it has anything to doo with the change to the new Woo framework?

    @tongny, you ad up-sells and cross-sells in the product editing page in the same area where you control the inventory, variations, etc... in the "linked Products" tab

  5. Jules Colle
    Member
    Posted 11 months ago #

    Thanks Aadil_hussain, that code snippet works great for me!

    Could be written a bit shorter (readable) like this:

    global $product;
    $upsells = $product->get_upsells();
    
    if (count($upsells) > 0) {
        remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    }
    do_action( 'woocommerce_after_single_product_summary' );
  6. Cartographer
    Member
    Posted 8 months ago #

    Hi Jules Colle and Aadil_hussain,

    As I am not developer could you please inform me, where exactly do I have to paste these lines?

    I see the single-product.php but I don't know where to paste the code.

Topic Closed

This topic has been closed to new replies.

About this Topic