Lv1983
Lv1983

Reputation: 87

Remove rel="nofollow" from woocommerce 'add to cart' buttons

WooCommerce is adding rel="nofollow" links to the add to cart button on the products on my site and I can't figure out how to remove it. I tried following the answer here, but couldn't get it to work.

Any help would be much appreciated.

Upvotes: 0

Views: 4717

Answers (3)

Cristab
Cristab

Reputation: 1

this work for me, add the code to your theme's functions.php file:

add_filter( 'woocommerce_loop_add_to_cart_link', 'remove_nofollow', 10, 2); 
function remove_nofollow( $html, $product ) {
 $html = str_replace( 'rel="nofollow"', '', $html );
 return $html;
}

To remove rel="nofollow" for a specific product (replace 1234 for your product id), you can use:

add_filter( 'woocommerce_product_add_to_cart_link', 'remove_nofollow', 10, 2 );
function remove_nofollow( $html, $product ) {
  if ( $product->get_id() == 1234 ) {
   $html = str_replace( 'rel="nofollow"', '', $html );
  }
  return $html;
}

Upvotes: 0

SpeedPlus
SpeedPlus

Reputation: 33

I use this code. But did not find how to insert in "aria-label" product title.

// Change rel “nofollow” to rel “dofollow” on Shop Page Product button
add_filter( 'woocommerce_loop_add_to_cart_link', 'add_to_cart_dofollow', 10, 2 );
function add_to_cart_dofollow($html, $product){
    if($product->product_type == 'simple') {
        $html = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" aria-label="Добавить в корзину" data-product_sku="%s" class="%s" %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( $args['class'] ) ? $args['class'] : 'button product_type_variable add_to_cart_button' ),
        esc_attr( isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : ''),
        esc_html( $product->add_to_cart_text() )
    );
    }else {
    $html = sprintf( '<a href="%s" data-quantity="%s" data-product_id="%s" aria-label="Выбрать" data-product_sku="%s" class="%s" %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( $args['class'] ) ? $args['class'] : 'button product_type_variable add_to_cart_button' ),
        esc_attr( isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : ''),
        esc_html( $product->add_to_cart_text() )
    );
    }
    return $html;
}

Upvotes: 0

kashalo
kashalo

Reputation: 3562

you can use woocommerce_loop_add_to_cart_args to unset the rel attribute from the add to cart button in the WooCommerce loop

add_filter( 'woocommerce_loop_add_to_cart_args', 'remove_rel', 10, 2 );
function remove_rel( $args, $product ) {
    unset( $args['attributes']['rel'] );

    return $args;
}

Upvotes: 6

Related Questions