• Resolved alexbbonzer

    (@alexbbonzer)


    I was toying around with Woocommerce recently. Most webshops I work on have a feature enabled to automatically hide a product from the site (both internal navigation as well as search functions) once the product is sold out.
    Handy and clean, but creates a lot of orphan pages.

    Then I found this script to automatically add a noindex tag to products which seemed to solve this problem, however now we’ve got a lot of noindex-pages in the sitemap.

    So I found the Yoast developer api documentation and since I am not too familiar with .php then I’d like to ask for some help in making a function to exclude sold-out products from the sitemap.

    https://developer.yoast.com/features/xml-sitemaps/api/#exclude-specific-posts

    function add_tagseo_metarob() {
        if ( get_post_type( get_the_ID() ) == 'product'){
            $pro = new WC_Product(get_the_ID());
            if( $pro->stock_status != 'instock' ){
                ?>
                 <meta name="robots" content="noindex">
    
                <?php
            }
        }
    }
    
    add_action('wp_head', 'add_tagseo_metarob');
    

    This is the code used to add the noindex tag, relatively simple in that it just checks the stock and if it’s not instock, then adds the noindex tag.

    I was thinking of something like this, but I am not too well-versed in .php so I’d like to guideance here

    function sitemap_exclude_soldout( $excluded, $post_type ) {
        return $pro = new WC_Product(get_the_ID());
            if( $pro->stock_status != 'instock' );
    }
    
    add_filter( 'wpseo_sitemap_exclude_post_type', 'sitemap_exclude_soldout', 10, 2 );
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Maybellyne

    (@maybellyne)

    Hello Alexbbonzer,

    Thanks for reaching out regarding your webshop. If I understand correctly, you want to exclude out-of-stock products from the sitemap. If yes, you can use the filter below:

    add_filter( 'wpseo_sitemap_entry', 'custom_exclude_from_xml_sitemap', 10, 3 );
    function custom_exclude_from_xml_sitemap( $url, $type, $object ) {
    	if ($object->post_type == 'product') {
    		$product = wc_get_product( $object );
    		if (!$product->is_in_stock())
    			return false;
    	}
    return $url;
    }

    Hope that helps.

    Thread Starter alexbbonzer

    (@alexbbonzer)

    Hello,

    Thank you so much for the response.

    I would try that right away, however I found an error with the script I was using

    I just had a re-think about it and the whole point is to avoid orphan pages and noindex pages in sitemap.

    I experimented a bit after this, and found that the script I was using was adding noindex-tags to mostly correct things, but also the odd category which is not good.

    Adding the same method of finding the post ID’s to handle didn’t seem to work on the noindex function, but then again… I don’t think that this would even be necessary, since Woocommerce automatically hides the URL from all site-navigation and your script removes it from the sitemap, crawlers or google shouldn’t be able to find the sold-out products then?

    • This reply was modified 2 years, 11 months ago by alexbbonzer.
    Plugin Support Maybellyne

    (@maybellyne)

    As far as I know, a URL not in the sitemap may be found by crawlers in another way. But adding a noindex tag is better. The URL will not be added to Google’s index.

    Thread Starter alexbbonzer

    (@alexbbonzer)

    Agreed, that’s the thing;

    I tried doing that initially, but couldn’t get the code snippet to work correctly, as it would generate noindex tags both on sold-out and in-stock products.

    Plugin Support Michael Ti?a

    (@mikes41720)

    Hi,

    We aren’t able to provide advice or suggestions on the implementation of the code as it’s outside our scope of support, but we’ll be keeping this thread open for a few days to see if other users can provide suggestions on how it can be improved.

    Plugin Support Maybellyne

    (@maybellyne)

    Hi,

    This thread has been marked as resolved due to a lack of activity.

    You’re always welcome to re-open this topic. Please read this post before opening a new request.

    Thanks for understanding!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Automatic sitemap exclusion based on product stock’ is closed to new replies.