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.
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;
}
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;
}
Related
I tried to replace the Add To Cart button by this code:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
return __( '<i class="fas fa-cart-plus"></i>', 'woocommerce' );
}
and I insert the code bellow to my woocommerce/loop/add-to-cart.php
global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
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' ),
$product->add_to_cart_text()
),
$product );
and the result I received is button Add to Cart replace to Shopping icon but without link, could anybody help me fix this ?!
Thanks
If you can edit the template page, then why can't you replace add_to_cart_text with icon html
global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
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' ),
'<i class="fas fa-cart-plus"></i>'
),
$product );
I need to display quantity select near "Add to cart" button in WooCommerce in loop. How do I do that?
Thank you MahdiY, that works. But I faced another problem:
I want to replace button "Add to cart" with cart icon.
In file add-to-cart.php I have the following code:
global $product;
$class = isset( $class ) ? $class . ' cart-icon-btn' : 'cart-icon-btn';
$tdir = get_template_directory_uri();
echo has_filter('woocommerce_loop_add_to_cart_link');
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s"><img class="cart-icon-btn" src="' . $tdir . '/images/basketin.png"></a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' )
// esc_html( $product->add_to_cart_text() )
),
But this filter 'woocommerce_loop_add_to_cart_link' seems to override my HTML and remove IMG tag. I tried to find any function added to this filter in files of WC code but did not find.
How do I fix that?
Use this code:
<?php
/**
* Code should be placed in your theme functions.php file.
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
Override loop template and show quantities next to add to cart buttons.
I want to add add to cart button with quantity filed under image in single product page. i have tried many code but not luck. one code which work but issue is that quantity not adding properly. i add below code my-theme/woocommerce/single-product/product-image.php before do_action( 'woocommerce_product_thumbnails' ); hook.
if ( ! is_shop() && ! is_product_taxonomy() ) {
$quantity_field = woocommerce_quantity_input( array(
'input_name' => 'product_id',
'input_value' => ! empty( $product->cart_item['quantity'] ) ? $product->cart_item['quantity'] : 1,
'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity(),
'min_value' => 0,
), $product, false );
$quantity_field = str_replace( array( '<div class="quantity">', "</div>" ), '', $quantity_field );
echo str_replace( '<input ', '<input style="max-width: 70px" ', $quantity_field );
}
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
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->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
Hi add this hook in your function file.
remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30 );
add_action( 'woocommerce_product_thumbnails','woocommerce_template_single_add_to_cart',30 );
display as like default WC
http://screencast.com/t/u8giinLfKmzt
I need to replace the Add to Cart button (only on certain categories) with a custom button for "Text a Dealer." The "Text a Dealer" button will trigger a gravity form in a lightbox which allows the user to submit a text message over the Twilio SMS service.
Here is a screenshot
I think I know how to link the button to a form in a lightbox but I do not know how to replace the button.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
return '<button>Text a Dealer</button>';
}
You can replace button code with your desired code.
This will replace the default button code with your custom code.
You also want this customization to be applicable only on certain categories. This can be achieved by addition of some more code. See below.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
//list category slugs where button needs to be changed
$selected_cats = array('cat-one-slug', 'cat-two-slug', 'cat-three-slug');
//get current category object
$current_cat = get_queried_object();
//get category slug from category object
$current_cat_slug = $current_cat->slug;
//check if current category slug is in the selected category list
if( in_array($current_cat_slug, $selected_cats) ){
//replace default button code with custom code
return '<button>Text a Dealer</button>';
}
}
Hope this helps.
$args = array();
if ( $product ) {
$defaults = array(
'quantity' => 1,
'class' => implode(
' ',
array_filter(
array(
'button add-to-cart-loop',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
)
)
)
);
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
$price = $product->get_price();
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" data-product_price_'.esc_attr( $product->get_id() ).'="'. $price .'" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s"><span>%s</span></a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
}
I am using this shortcode to display products [product_category category="extras" orderby="date"].
Variable products show "select options" and single products show "add to cart". I was able to change text of both to say "View Product".
The problem now is that I need to change the url of those that used to say "add to cart", because they don't link to the product page but to "Add to the cart".
I know I can edit the woocommerce template, but I would need this as a function to be added to function.php
I don't need any button involved, just replacing the url.
So again purpose:
Replace/redirect "Add to Cart" url to link to product page (only in loop, obviously not in product page).
Can someone help?
If someone does elect to change the woocommerce file (in child theme of course!).
In file: /loop/add-to-cart.php
Change:
global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )
),
$product );
To:
global $product;
if ( $product->product_type == "simple" ) {
$simpleURL = get_permalink();
$simpleLabel = "View Product"; // BUTTON LABEL HERE
} else {
$simpleURL = $product->add_to_cart_url();
$simpleLabel = $product->add_to_cart_text();
};
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '%s',
esc_url( $simpleURL ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
esc_attr( $product->product_type ),
esc_html( $simpleLabel )
),
$product );
In the funtions.php of you theme add this code:
/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');
/*STEP 2 -ADD NEW BUTTON THAT LINKS TO PRODUCT PAGE FOR EACH PRODUCT */
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo '<p style="text-align:center;margin-top:10px;">';
$currentlang = get_bloginfo('language');
//for multilanguage
if($currentlang=="en-GB"){
echo do_shortcode('View Product');
} elseif($currentlang=="fr-FR"){
echo do_shortcode('Voir le produit');
}else {
echo do_shortcode('Ver Producto');
}
echo '</p>';
}
I believe you can disable AJAX add-to-cart functionality in the WooCommerce settings.
If that isn't satisfactory for some reason you can take a look at the loop/add-to-cart.php template. The add to cart link is filterable. If you look at add-to-cart.js you can see that the AJAX add to cart function is triggered for any link with the add_to_cart button class, and only works for buttons with the product_type_simple class.... ie: only for simple products. Depending on your styles, you could either remove the product type class or the add_to_cart_button class from the link. In the example below, I have removed the add_to_cart_button class.
add_filter( 'woocommerce_loop_add_to_cart_link', 'so_26247988_add_to_cart_link', 10, 2 );
function so_26247988_add_to_cart_link( $link, $product ){
$link = sprintf( '%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )
)
return $link;
}