I'm trying to crate a snipped to display a second "add to cart" button on all product single pages from a specific categorie. This button should add 6 of the current product to the cart.
UPDATE:
With the info from #kmclaws and here additional add to cart button with fixed quantity in woocommerce single product pages I was able to get this to work. The only problem now is, that the IF in order to only display on "biere" product category does not work.
Problem:
Some Products are from "biere" or "cider" only and some are from "biere" AND "cider" and have some other categories. Means, sometime there are more than one sometime only one category is defined. We want to display this always if "biere" OR/AND "cider" is one of the product category.
How can I code it that way, that if "biere" OR "cider" is the categorie to display that all?
This is my current code:
add_action( 'woocommerce_after_add_to_cart_button', 'additional_simple_add_to_cart', 20 );
function additional_simple_add_to_cart() {
global $product;
// Only for simple product type
if( ! $product->is_type('simple') ) return;
// Only for products from category "biere" AND/OR "Cider"
if ( has_term( 'biere', 'cider', 'product_cat' ) ) {
// Output Info text for "biere" or "cider" products
echo '<p class="rtrn">Infotext block</p>';
// Only display when more than 6 are available
if( $product->get_stock_quantity() >= 6 ) {
$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=6';
$class = 'ingle_add_to_cart_button-12 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$button_text = __( "6 Pack bestellen", "woocommerce" );
// Output add to cart button
echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';
}
}
}
Thanks
It looks like you are not grabbing the ID of the current product in PHP correctly. Try this:
add_action( 'woocommerce_after_add_to_cart_quantity', 'add_quantity_6' );
function add_quantity_6() {
global $product;
$id = $product->get_id();
if ( is_product_category( 'jeans' ) ) {
echo 'Add 6 to Cart';
}
}
You need to replace example.com with either a static or relative path to your site
This is my solutions:
add_action( 'woocommerce_after_add_to_cart_button', 'additional_simple_add_to_cart', 20 );
function additional_simple_add_to_cart() {
global $product;
// Only for simple product type
if( ! $product->is_type('simple') ) return;
// Only for products from category "biere" AND/OR "Cider"
if ( has_term( array('biere', 'cider'), 'product_cat', $product->get_id() ) ) {
// Output Info text for "biere" or "cider" products
echo '<p class="rtrn">Infotext block</p>';
// Only display when more than 6 are available
if( $product->get_stock_quantity() >= 6 ) {
$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=6';
$class = 'ingle_add_to_cart_button-12 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$button_text = __( "6 Pack bestellen", "woocommerce" );
// Output add to cart button
echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';
}
}
}
Related
I have added badges to my product images to display important information. If a product has a specific tag, it will display that specific badge. I got this to work in the shop page and single product page, but having trouble with the cart page. I can load one badge to all the items in the cart, but can't seem to load the other badges conditionally.
These are the conditional statements I used for the shop page and single product page:
global $product;
if ( $product->is_on_sale() ) {
echo '<span class="product-badge">SALE</span>';
}elseif ( has_term( 'reversible', 'product_tag' ) ) {
echo '<span class="product-badge">REVERSIBLE</span>';
} elseif ( has_term( 'hard-sole', 'product_tag' ) ) {
echo '<span class="product-badge">HARD SOLE</span>';
} elseif ( has_term( 'soft-sole', 'product_tag' ) ) {
echo '<span class="product-badge">SOFT SOLE</span>';
}
This is the code I have so far for the cart page:
add_filter( 'woocommerce_cart_item_thumbnail', 'cart_badge' );
function cart_badge( $thumbnail ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'reversible', 'product_tag', $cart_item['product_id'] ) )
{
printf( '%s<span class="cart-badge">REVERSIBLE</span>',
esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
}
}
How do I add apply my conditional statements to my current code to show the specific product badge on specific product thumbnail, or else echo $thumbnail if conditions are false?
Using Woocommerce, I would like to add the SKU instead of the product title in the following pages: Shop page, Cart page and Checkout page.
For example in shop page, I am able to add the SKU above the product title using the code below. However, the problem is that the SKU added doesn't have the link to the single product page.
add_action( 'woocommerce_shop_loop_item_title', 'sku_before_title', 9 );
function sku_before_title() {
global $product;
$sku = $product->get_sku();
echo '<p class="name product-title">Product ' . $sku . '</p>';
}
What is the correct code to have SKU instead of product title and keeping the same link and CSS as the product title?
Code for Shop page and Cart page and Checkout page.
To replace the product title by the sku on WooCommerce archive pages you will use:
// Frontend: On shop and archives pages
add_action( 'woocommerce_shop_loop_item_title', 'sku_replace_title_on_loop', 9 );
function sku_replace_title_on_loop() {
global $product;
// Remove the product title
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
$sku = $product->get_sku();
// Replace the title by the Sku if is not empty
$title = empty($sku) ? get_the_title() : $sku;
// Output
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . $title . '</h2>';
}
For cart and checkout pages, you will use:
// Frontend: on cart, minicart and checkout
add_filter( 'woocommerce_cart_item_name', 'sku_replace_title_on_cart_checkout', 10, 3 );
function sku_replace_title_on_cart_checkout( $item_name, $cart_item, $cart_item_key ) {
if( $sku = $cart_item['data']->get_sku() ) {
if( is_cart() ) {
$item_name = sprintf( '%s', esc_url( $cart_item['data']->get_permalink( $cart_item ) ), $sku );
} else {
$item_name = $sku;
}
}
return $item_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
In Woocommerce I would like to add an extra add to cart button with a specific quantity.
The answer "additional add to cart button with fixed quantity in woocommerce single product pages" doesn't it mostly and I just need to enable that code for specific products of any product type.
Is this possible? What does I have to change…
To handle product variations (on variable products) it's much more complicated and requires to add some JavaScript/jQuery code.
I have separate the product settings restrictions, from the main function, in a custom conditional function and you have the choice between 2 ways:
1) You can use product Ids restrictions:
// PRODUCT IDs based ::: Conditional function
function enable_additional_add_to_cart_button( $product_id ) {
// HERE define your Product Ids in the array
$product_ids = array(37, 40, 53);
return in_array( $product_ids, $product_id ) ? true : false;
}
2) Or you can use product category restrictions:
// PRODUCT CATEGORY based ::: Conditional function
function enable_additional_add_to_cart_button( $product_id ) {
// HERE define your Product Category terms in the array (can be term Ids, names or slugs)
$terms = array('t-shirts', 'socks', 'glasses');
return has_term( $terms, 'product_cat', $product_id ) ? true : false;
}
Now below is the main function that will display an additional add to cart button and that works with one of the conditional functions above.
// Additional add to cart button with fixed bulk quantity
add_action( 'woocommerce_after_add_to_cart_button', 'additional_add_to_cart_button', 20 );
function additional_add_to_cart_button() {
global $product;
if( ! enable_additional_add_to_cart_button( $product->get_id() ) ) return;
$qty = 12;
$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity='.$qty;
$class = 'single_add_to_cart_button-12 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$btn_txt = __( "Add a case of 12", "woocommerce" );
## ---------------------- For non variable products ----------------------- ##
if( ! $product->is_type('variable') ) :
// Output
echo '<br><a href"'.$href.'"= rel="no-follow" class="'.$class.'" style="'.$style.'">'.$btn_txt.'</a>';
## ---------- For variable products (handling product variations) --------- ##
else :
// Output
echo '<br><a rel="no-follow" class="'.$class.'" style="'.$style.'">'.$btn_txt.'</a>';
$data = array();
// Loop through available variations data
foreach( $product->get_available_variations() as $variation_data ){
if( $variation_data['is_in_stock'] && $variation_data['is_purchasable'] ) {
$variation_id = $variation_data['variation_id'];
$attributes_str = '';
// Loop through variation attributes
foreach ( $variation_data['attributes'] as $attribute_taxonomy => $term_slug ) {
$attributes_str .= '&'.$attribute_taxonomy.'='.$term_slug;
}
// Set product attributes pairs for variations
$data[$variation_id] = $href . '&variation_id=' . $variation_id . $attributes_str;
}
}
?>
<script type="text/javascript">
jQuery(function($){
var a = <?php echo json_encode($data); ?>, b = '.single_add_to_cart_button-12',
c = 'wc-variation-selection-needed', d = 'disabled',
f = 'form.variations_form', h = $(c).attr('href'),
s = '.variations select', i = 'input.variation_id';
// On show and hide variation event
$(f).on('show_variation', function() {
var found = false;
$.each(a, function(j,k){
if( $(i).val() == j ) {
found = true;
if( $(b).hasClass(c) && $(b).hasClass(d) )
$(b).removeClass(c).removeClass(d).attr('href',k);
}
});
if( ! found && ! ( $(b).hasClass(c) && $(b).hasClass(d) ) )
$(b).addClass(c).addClass(d).removeAttr("href");
}).on('hide_variation', function() {
if( ! ( $(b).hasClass(c) && $(b).hasClass(d) ) )
$(b).addClass(c).addClass(d).removeAttr("href");
});
});
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You can check the product id using WC_prroduct->get_id() function
// Only for the product with id 16
if( $product->get_id() != 16 ) return;
$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=12';
$class = 'ingle_add_to_cart_button-12 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$button_text = __( "Add a case of 12", "woocommerce" );
// Output
echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';
thank you for all the clarifications.
I will only have single product types, but I will have some products where I need an add to cart for 6 bottles and for some others, 12 bottles and for some.
I want thinking if I could have an option in the product page, e.g. like a custom field, e.g. wine-box, where I could fill in a 6 or a 12 and this can be displayed as an extra add to cart button (Add a case of 6 text or Add a case of 12 text).
Thanks
This is my code:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text( $text ) {
if( has_term( 'liners', 'product_cat' ) ){
$text = __( ' ', 'your-plugin' );
echo do_shortcode('Request а Quote');
}
return $text;
}
I need to make function to replace the "Add to cart" button Url and Text for just one specific product category.
This button will trigger a Lightbox with a contact form and the text for this button will be: Request a Quote.
How can I make it work as expected?
Here is how it works actually on this link.
Updated: for 2 different product categories (2 different buttons)
A global and complete solution for your products from 'liners' product category:
If one of your products (in 'liners' product category) is not a variable product, you need first to replace the add-to-cart button in shop and archives pages by a simple button linked to the product.
In single product pages you need to remove add-to-cart button and quantities fields, to replace it by your custom button.
Here is that code:
// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_replacing_add_to_cart_button', 10, 2 );
function conditionally_replacing_add_to_cart_button( $button, $product ) {
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// For 'liners' product category
if( has_term( $categories, 'product_cat', $product_id ) ){
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
// replacing add to cart button and quantities by your custom button in Single product pages
add_action( 'woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0 );
function conditionally_replacing_template_single_add_to_cart() {
global $product;
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
function custom_button_replacement(){
global $product;
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if( has_term( $categories[0], 'product_cat', $product_id ) )
$class_id = "923"; // liners
elseif( has_term( $categories[1], 'product_cat', $product_id ) )
$class_id = "925"; // custom-classics
else $class_id = ""; // none
// set below your custom text
$button_text = __('Request а Quote', 'woocommerce');
// Output your custom text
echo ''.$button_text.'';
}
// Only for 'liners' and 'custom-classics' product categories
if( has_term( $categories, 'product_cat', $product_id ) ):
// For variable product types
if( $product->is_type( 'variable' ) ){
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
// The button replacement
add_action( 'woocommerce_single_variation', 'custom_button_replacement', 20 );
}
else // For all other product types
{
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The button replacement
add_action( 'woocommerce_single_product_summary', 'custom_button_replacement', 30 );
}
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for all product types (simple, variable…). You will get (example):
Here is my solution to replace the Add to Cart button with a Read More button, with class and id in a certain category.
/* // Replace the Add to Cart Btn in Category Ammunition with View Product Btn */
add_filter('woocommerce_loop_add_to_cart_link','change_simple_shop_add_to_cart',10,2);
function change_simple_shop_add_to_cart( $html, $product ){
$category_ammunition = $product->get_categories();
if (strstr($category_ammunition, 'Ammunition')) { // Add Your Category Here 'Ammuntion'
$html = sprintf( '<a id="read-more-btn" rel="nofollow" href="%s" data-product_id="%s" class="button vp-btn">%s</a>',
esc_url( get_the_permalink() ),
esc_attr( $product->get_id() ),
esc_html( __( 'Read More', 'woocommerce' ) )
);
$category_ammunition = $product->get_categories();
}
return $html;
}
I would like to Removal/disable/hide of "bid" button from product pages in WooCommerce for post authors of the post.
I am using WC vendors pro + Woocommerce + Wp Geine Auctions + WC Vendors Auction.
Please find the link of the screen shot below:
The Live Link to the product
How can I do it please?
As this buttons are already customized by you or some plugins, I am not sure at 100% that it will work for you, even if it works on my test server.
The first function is a conditional function that detects for a product if the current user is the author (the vendor) of this product.
Then on shop and archives pages the add to cart button is replace by a custom button liked to the product.
To finish on single product page the button is replace by a fake button with a custom text (here "Not Allowed")…
Here is the code:
// Custom conditional function (detecting the vendor of a product)
if( ! function_exists( 'is_the_vendor' ) ){
function is_the_vendor( $product ){
$current_user_id = get_current_user_id();
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get the product post object to get the post author
$post_obj = get_post( $product_id );
$post_author = $post_obj->post_author;
if( $post_author == $current_user_id ) return true;
else return false;
}
}
// Shop and archives pages: we replace the button add to cart by a link to the product
add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2 );
function custom_text_replace_button( $button, $product ) {
if( is_the_vendor( $product ) ){
$button_text = __("View product", "woocommerce");
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
} else {
return $button;
}
}
// replacing add to cart button and quantities by a custom inactive button
add_action( 'woocommerce_single_product_summary', 'replacing_template_single_add_to_cart', 1, 0 );
function replacing_template_single_add_to_cart() {
global $product;
if( is_the_vendor( $product ) ):
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The text replacement
add_action( 'woocommerce_single_product_summary', function(){
// set below your custom text
$text = __('Not allowed', 'woocommerce');
// Temporary style CSS
$style_css = 'style="border: solid 1px red; padding: 0 6px; text-align: center;"';
// Output your custom text
echo '<a class="button custom-button" style="background-color: grey !important;">'.$text.'</a>';
}, 30 );
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works. you will get this:
And this: