How to add a shortcode below add to cart in Woocommerce - php

I need to add a shortcode under "Add to cart" button in Woocommerce product pages. This shortcode is displaying a custom field from a plugin. I've tried the code below to output in a product page but it doesn't get outputted:
add_filter( 'woocommerce_after_shop_loop_item', 'custom_woocommerce_short_description' );
function custom_woocommerce_short_description( $woocommerce_after_shop_loop_item ) {
global $post;
$woocommerce_after_shop_loop_item = $woocommerce_after_shop_loop_item . do_shortcode('[wpuf-meta name="instructions_to_customer"]') ;
return $woocommerce_after_shop_loop_item;
}

I figured it out if anyone needs it:
add_action( 'woocommerce_after_add_to_cart_button', 'misha_after_add_to_cart_btn' );
function misha_after_add_to_cart_btn(){
echo do_shortcode( '[wpuf-meta name="instructions_to_customer"]' );
}

Related

Woocommerce before add to cart when product is out of stock

I have moved the product description before the variations (custom made) and before the add to cart button with the hook "woocommerce_before_add_to_cart_form". It works great.
The problem is with products that are not i stock, then this hook does not seam to fire at all and the product description never shows.
How can I add the description before the variations but also on out of stock products?
I solved it like this now, with two different codes. But the idea is to use only one hook for this. To use only woocommerce_single_product_summary places the description under the add to cart button.
This code adds the product description for products out of stock:
add_action( 'woocommerce_single_product_summary', 'visa_produktbeskrivning', 40 );
function visa_produktbeskrivning() {
global $product;
if ( ! $product->is_in_stock() ) {
echo '<div itemprop="description">';
echo apply_filters( 'the_content', $product->description );
echo '</div>';
}
}
And this code adds the product description for products in stock:
add_action( 'woocommerce_before_add_to_cart_button', 'display_custom_field' );
function display_custom_field() {
global $product;
echo '<div itemprop="description">';
echo apply_filters( 'the_content', $product->description );
echo '</div>';
}

Change Add to Cart text to "Apply" on WooCommerce single product pages only

I am using the code below to replace add to cart button with a linked button to the product in Shop, related products and archives pages only
add_action( 'woocommerce_after_shop_loop_item', 'shop_view_product_button', 10);
function shop_view_product_button() {
global $product;
$link = $product->get_permalink();
echo 'View Product';
}
But I need to change the add to cart button text on the single product page to "Apply".
Any help?
Use the following:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_single_add_to_cart_custom_text', 20, 1 );
function product_single_add_to_cart_custom_text( $text ) {
$text = __( 'Apply', 'woocommerce' );
return $text;
}

Conditional Add To Cart Button On Woocommerce Product Archive

I'm trying to replace the default woocommerce product archive add to cart button based on a condition.
For example
Product A - Checkbox Active --> Display Find A Dealer Button
Product B - Checkbox inactive -- > Display default add to cart button
I have managed to successfully write the code to add the checkbox and the condition to replace the button if the product has a custom checkbox active. The button for product A works fine and diaplyas as intended in the shop archives.
I am however not sure how to retain the woocommerce default add to cart button if for products that do no have this checkbox activate. I thought adding the action would work however I am stumped. Any help would be greatly appreciated. Thank you in advance.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
global $product;
if ($product->get_meta('_checkbox_active') === 'yes' ){
return '<button>Finda Dealer</button>';}
else {add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );}
You just forgot the hooked function variables arguments. Try the following instead:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
if ( $product->get_meta('_checkbox_active') === 'yes' ){
$button = '' . __( "Find a dealer", "woocommerce" ) . '';
}
return $button;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.

Add custom content Next add-to-cart In button for a specific Product

I am trying to add content next to add to cart button in woocommerce for specific product, I have used the bellow woocommerce hook, But unfortunately it didn't work:
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
function add_content_after_addtocart_button_func() {
$current_product = $product->id;
if($current_product === '13333') {
echo '<div class="second_content">Place your content here!</div>';
}
}
Any one can help me on this?
Thanks
Update: Added compatibility with WC +3
This custom function hooked in woocommerce_single_product_summary action hook, will display a custom content after add-to-cart button for a specific product ID in the single product page:
add_action( 'woocommerce_single_product_summary', 'add_content_after_addtocart_button_func', 35 );
function add_content_after_addtocart_button_func() {
global $product;
// Added compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if($product_id == 13333)
echo '<div class="custom-content">'.__('Place your content here!','woocommerce').'</div>';
}
The 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.

woocommerce product addon - empty cart upon going to product page

Right now I am trying to get it so that whenever someone gets to my site's product page their cart is automatically emptied.
I am using a woocommerce product addon called "Product & Checkout Options for WooCommerce" that allows me to use radiobuttons/checkboxes for my products, I don't know if that will alter any of the code.
I've tried php code such as this but it hasn't worked:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_in_cart' , 10, 1);
function only_one_in_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
unset($cart_item_data['product_meta']);
return true;
}
It's better to add the hook on your single product page, do it with the action woocommerce_before_single_product:
add_action( 'woocommerce_before_single_product', `only_one_in );
function only_one_in_cart() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
This will empty your cart each time you visit the page, if it's late, then you can add the function into the wp_head hook and validate if you are in the product page by is_product():
add_action( 'wp_head', `only_one_in );
function only_one_in_cart() {
if ( is_product() ){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}

Categories