Change rel "nofollow" to rel "dofollow" on Shop Page Product Links

Most of the products in my Woocommerce shop use variables, so show the “Choose Options” bar across the product’s image, linking it to the product’s page. Each of these links gives a rel “nofollow” tag in the markup.

How can I remove the “nofollow” and change it to “dofollow”? The screenshots below hopefully show this clearer.

Thanks,

Hi There,

You have to install and activate the child theme first.

After that adding this code under functions.php file locates in your child theme:

add_filter( 'woocommerce_loop_add_to_cart_link', 'add_to_cart_dofollow', 10, 2 );
function add_to_cart_dofollow($html, $product){
	$html = sprintf( '<a rel="dofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
		esc_url( $product->add_to_cart_url() ),
		esc_attr( isset( $quantity ) ? $quantity : 1 ),
		esc_attr( $product->get_id() ),
		esc_attr( $product->get_sku() ),
		esc_attr( isset( $class ) ? $class : 'button' ),
		esc_html( $product->add_to_cart_text() )
	);
	return $html;
}

Let us know how it goes!

Hi Thai,

Thanks for the code. It works perfectly! Is it possible to tweak it further to give “dofollow” for the “Choose Option” button, but a “nofollow” for the “Add To Cart” button?

Thanks for you help.

Hi,

You can change the code to this.

add_filter( 'woocommerce_loop_add_to_cart_link', 'add_to_cart_dofollow', 10, 2 );
function add_to_cart_dofollow($html, $product){
    echo $product->add_to_cart_text();
    if($product->add_to_cart_text()=="Add to cart") {
         $html = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->get_id() ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $class ) ? $class : 'button' ),
        esc_html( $product->add_to_cart_text() )
    );
    }else {
    $html = sprintf( '<a rel="dofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->get_id() ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $class ) ? $class : 'button' ),
        esc_html( $product->add_to_cart_text() )
    );
    }
    return $html;
}

Hope that helps.

Thanks Paul,

I’m afraid that did not quite do what was expected. It returns a “dofollow” for both Add to Cart and Choose Options buttoms.

Hey @cdnwhite,

The code provided serves only as a guide. You should seek help from a third party developer to fix the code or enhance it.

Thank you for understanding.

No problems. Thanks for the help.

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.