How do I display quantity select in loop in woocommerce? - wordpress

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.

Related

How to check if product is in basket and if it is in basket replace it with plus and minus?

Is it possible to check if the product is in the cart, and then show (+ or -)? And if it is not in the basket showing Add to basket button.
I use the following line of codes, But it shows both plus and minus and add to cart button.
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;
}
I am looking for a solution to only show add to cart button. if it is in the cart then show plus and minus.

Woocommerce - add to cart button and quantity in product list on category page

I have a custom product list on a specific category page.
It looks like this:
Product list category page with missing quantity selection and add to cart button at end.
I have been using the below in functions.php and it has worked fine. Since the last update of WooCommerce it no longer works. The add to cart buttons and quantity fields are not showing anymore.
I get no errors and the fields where the qty and button html goes are blank when I check the html for the page.
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 ) {
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
// Only for this product category
if ($wk_cat_value == 1 && is_product_category())
{
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;
}
}
This line is a checkbox I added to the category page to specify if the current category is to use the custom template:
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
Template version Woocommerce files were 2.0.0.
Updated versions are 3.4.0.
Any suggestions greatly appreciated.
Found a solution:
function wk_add_to_cart() {
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
// $wk_cat_value is a flag added to this specific category to decide whether or not the add to cart should be added
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
global $product;
if ($wk_cat_value == 1 && is_product_category()) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
echo '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
echo woocommerce_quantity_input( array(), $product, false );
echo '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
echo '</form>';
}
}
}
add_action('woocommerce_after_shop_loop_item','wk_add_to_cart');
Hope that helps.

How to insert add to cart link to icon - Woocommerce

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 );

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.
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;
}

Woocommerce replace add to cart url to link to product page

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;
}

Resources