Support » Plugin: WooCommerce » get_upsell_ids() returning deleted products

  • Resolved sporkme


    So I started seeing some issues with a custom theme that indicated my upsell product ID was not valid. I was using the deprecated “get_upsells()”, so I moved to “get_upsell_ids()” and my deprecation warning went away, but I was still seeing 3 products returned in my upsells even though two of those three were deleted. I verified that they are truly gone and not just in the trash.

    I also do some checks on whether “get_upsell_ids()” returns any products to determine in my theme if I even want to include this section, and I feel like if it returns bogus products, that means I have to re-do that to verify each and every deleted product, which seems counter-intuitive (and it’s wasteful to do a lookup on every upsell).

    This seems like a bug – I don’t think I should see deleted items. Some good reasons not to show them:

    – The results of the function don’t match the product’s edit/admin page (in my case I saw one upsell there, but the template was looping on three)
    – If you use get_upsell_ids() to check if you should show an upsell section or not, it will return true even if no valid products exist
    – It seems inefficient to call get_upsell_ids and then have to check each result to see if it’s valid or not

    Feels like a bug to me, should I open something in github?

    For reference, I’m doing something like this in my theme:

    if ($product->get_upsell_ids()) { // TEST IF UPSELLS EXIST
    <section id="product-grid">
    	<div class="container">
    		<h6>You Might Also Like</h6>
    	if ($upsells = $product->get_upsell_ids()) { ?>
    		foreach($upsells as $product_id) {
    			$upsell_product = wc_get_product($product_id);
    			$upsell_product_name = $upsell_product->get_name();
    			$upsell_product_slug = $upsell_product->get_slug();
    			$upsell_image_id = $upsell_product->get_image_id();
    			$upsell_product_image = wp_get_attachment_image_src($upsell_image_id, $size='colors-finishes');
    			<a href="/product/<?=$upsell_product_slug?>/"><?=$upsell_product_name?></a>
    		<?php }
    		} ?>
    <?php } ?>
    <?php } ?>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_upsell_ids() returning deleted products’ is closed to new replies.