Display specific data from selected product variation in Woocommerce - php

I'm attempting to display the selected product variation description on my single product page after add to cart button on variable product pages.
Below is the code I've implemented, but it does not appear to be working:
add_action( 'woocommerce_after_add_to_cart_form', 'description_below_add_cart', 11 );
function description_below_add_cart() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'cookware', $categories ) ) {
global $product;
$weight = $product->get_weight();
$dimensions = wc_format_dimensions($product->get_dimensions(false));
echo '<div class="short-description">' . $item['product']->get_description() .'</div>';
}
}
Here is a product page from the site.
How can I display specific data (the variation description) from selected product variation in Woocommerce after add to cart button for variable products?

Updated: Your current code can be simplified a bit regarding the product categories part. I have added some code to display the variation description for the selected product variation from variable product pages.
Here is the way to display the selected product variations description (requires some JQuery):
add_action( 'woocommerce_after_add_to_cart_form', 'description_below_add_cart', 11 );
function description_below_add_cart() {
global $product;
$terms_slugs = wp_get_post_terms( $product->get_id(), 'product_cat', array('fields' => 'slugs') ); // Get the terms slugs
// Only for 'cookware' product category
if ( in_array( 'cookware', $terms_slugs ) ) {
$weight = $product->get_weight();
$weight = ! empty($weight) ? wc_format_weight($weight) : '';
$dimensions = $product->get_dimensions(false);
$dimensions = ! empty($dimensions) ? wc_format_dimensions($dimensions) : '';
// For variable products
if ( $product->is_type('variable') ) {
// Display the selected variation description
echo '<div class="selected-variation-description"></div>';
?>
<script type="text/javascript">
jQuery( function($){
// On select variation display variation description
$('form.variations_form').on('show_variation', function( event, data ){
$('div.selected-variation-description').html(data.variation_description);
console.log(data.variation_description);
});
// On unselect variation remove variation description
$('form.variations_form').on('hide_variation', function(){
$('div.selected-variation-description').html('');
});
});
</script>
<?php
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related

Shortcode to display product attribute terms description when a variation is selected in Woocommerce

I'm looking for a way to create a wordpress shortcode to display the terms descriptions of the selected attributes for the woocommerce variable products.
For example on a T-shirt with color variation and logo variation
I would like to be able to display the description of the 2 terms : Of the selected color and the selected logo one below the other .
I relied on this code to understand how to create a shortcode :
Shortcode that display all product attributes set for a WooCommerce product
And on this code to retrieve and display attribute descriptions but I can't seem to combine the two.
How Can I show attribute variation description with woocommerce?
Thanks in advance for your help and suggestions.
LeoM
With this code I manage to display all the descriptions of the variations used on the product :
add_shortcode( 'woo_attribute_descriptions', 'woo_attribute_descriptions_shortcode' );
function woo_attribute_descriptions_shortcode( $atts ) {
global $product;
$atts = shortcode_atts( array('product_id' => $product->get_id(),),
$atts, 'woo_attribute_descriptions' );
$product_id = $atts['product_id'];
$product = wc_get_product( $product_id );
$attributes = $product->get_attributes();
$output = '';
foreach ( $attributes as $attribute ) {
$name = wc_attribute_label( $attribute->get_name() );
$terms = wc_get_product_terms( $product_id, $attribute->get_name(), array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
$output .= '<p>' . wpautop($term->description) . '</p>';
}
}
return $output;}
How to display only the descriptions of the selected variations?
Thanks!

Add linked specific product attribute before title on Woocommerce single products

In WooCommerce I am trying to add "pa_artist" product attribute before the product title in single product page as below:
pa_attribute – product title
I would like the "artist" product attribute term name to be automatically listed before the title. I managed to add attribute to product title using this answer code.
What I need is to add an active link to the "artist" product attribute term name, that display the products for this attribute term name.
Based on Add specific product attribute after title on Woocommerce single products you can easily change the code to add a specific product attribute before the product title on WooCommerce single product pages as follows:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_single_product_summary', 'custom_template_single_title', 5 );
function custom_template_single_title() {
global $product;
$taxonomy = 'pa_artist';
$artist_terms = get_the_terms( $product->get_id(), $taxonomy ); // Get the post terms array
$artist_term = reset($artist_terms); // Keep the first WP_term Object
$artist_link = get_term_link( $artist_term, $taxonomy ); // The term link
echo '<h1 class="product_title entry-title">';
if( ! empty($artist_terms) ) {
echo '' . $artist_term->name . ' - ';
}
the_title();
echo '</h1>';
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Addition: For multiple linked product attributes terms use the following instead:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_single_product_summary', 'custom_template_single_title', 5 );
function custom_template_single_title() {
global $product;
$taxonomy = 'pa_artist';
$artist_terms = get_the_terms( $product->get_id(), $taxonomy ); // Get the WP_terms array for current post (product)
$linked_terms = []; // Initializing
// Loop through the array of WP_Term Objects
foreach ( $artist_terms as $artist_term ) {
$artist_link = get_term_link( $artist_term, $taxonomy ); // The term link
$linked_terms[] = '' . $artist_term->name . '';
}
if( ! empty($linked_terms) ) {
echo '<h1 class="product_title entry-title">' . implode( ' ', $linked_terms) . ' - ';
the_title();
echo '</h1>';
}
}

Display the selected WooCommerce variation formatted price with shortcode

I have created this piece of code that displays the price on any page on the website, which is controlled by woocommerce.
add_shortcode( 'hhs_product_price', 'hhs_woo_product_price_shortcode' );
function hhs_woo_product_price_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => null
), $atts, 'hhs_product_price' );
if ( empty( $atts[ 'id' ] ) ) {
return '';
}
$product = wc_get_product( $atts['id'] );
if ( ! $product ) {
return '';
}
return $product->get_price_html();
}
What I would like to do is modify the code so that if a customer selects a product with a variation. Then the price changes to display the variation price. For example right now if a person selects a product, in this case a tincture bottle, with a price variation connected to the size of the bottle. On the products page they see the following:-
Product (Tincture) $30 - $50
From a drop down menu they can select an option of either 10mg bottle ($30), 15mg bottle ($40), or 20mg bottle ($50). So if a person selects option 20mg the price should display $50 instead of $30 - $50
I have already looked at various posts on stackoverflow with a similar problem but non of those solutions are working for me
Display woocommerce variable product price
Woocommerce get variation product price
Any help would be greatly appropriated.
Thank you
To display the selected product price from a variation on variable products, jQuery is required. So the following shortcode will handle all product types, including variable products and their price variations:
add_shortcode( 'product_price', 'display_formatted_product_price' );
function display_formatted_product_price( $atts ) {
extract(shortcode_atts( array(
'id' => null
), $atts, 'product_price' ) );
global $product;
if( ! empty($id) || ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( empty($id) ? get_the_id() : $id );
}
$price_html = $product->get_price_html();
// Others product types than variable
if ( ! $product->is_type('variable') ) {
return '<span class="product-price">' . $price_html . '</span>';
}
// For variable products
else {
ob_start();
?>
<script type="text/javascript">
jQuery( function($){
var p = '<?php echo $price_html; ?>', s = 'span.product-price';
$( 'form.variations_form.cart' ).on('show_variation', function(event, data) {
$(s).html(data.price_html); // Display the selected variation price
});
$( 'form.variations_form.cart' ).on('hide_variation', function() {
$(s).html(p); // Display the variable product price range
});
});
</script>
<?php
return ob_get_clean() . '<span class="product-price">' . $price_html . '</span>';
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
USAGE:
Use [product_price] or in php code echo do_shortcode('[product_price]').
You can also define a product id like (for example for product id 37): [product_price id="37"]
I'd like to thank LoicTheAztec for this code which is tested and works. However I banged my head on one issue: if product variations are all with the same price, the price_html will come back empty (issue described here: https://github.com/woocommerce/woocommerce/issues/11827). I solved this with a filter found in the github conv above:
add_filter( 'woocommerce_show_variation_price', '__return_true');

Live update product category cart item count without reloading in Woocommerce

I created a shortcode that counts the quantity of products belonging to the '72' product category, that are added to the cart.
I am using the custom conditional function has_product_category() from this answer thread:
Set item quantity to multiples of “x” for products in a specific category in Woocommerce
Here is my code:
function cat_cart_count_bottiglie() {
$bottiglie = 0;
// Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], $category_ids = array( 72 ) ) ) {
$bottiglie += $cart_item['quantity'];
}
}
return $bottiglie;
}
add_shortcode('bottiglie', 'cat_cart_count_bottiglie');
The code works correctly.
But this shortcode count get updated only when the page is refreshed after add to cart and doesn't work for Ajax add to cart or when removing items or changing items quantity in cart page.
Is there a way to get the update instantly?
The following code will Ajax refresh your shortcode custom product category "72" item count:
// Custom conditional function that checks also for parent product category terms
function has_product_category( $product_id, $category_ids, $taxonomy = 'product_cat' ) {
$term_ids = array(); // Initializing
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$term_ids[] = $term->parent; // Set the parent product category
$term_ids[] = $term->term_id;
} else {
$term_ids[] = $term->term_id;
}
}
return array_intersect( $category_ids, array_unique($term_ids) );
}
// Custom function that count cart items remaining to a product_category
function get_bottiglie_count( $term_ids ){
$count = 0; // Initializing
// Loop through each cart item
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], $term_ids ) ) {
$count += $cart_item['quantity'];
}
}
return $count;
}
// Ajax refressing count
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_bottiglie_count', 50, 1 );
function refresh_bottiglie_count( $fragments ){
$fragments['#bottiglie-count'] = do_shortcode( "[bottiglie]" );
return $fragments;
}
// Shortcode that display the count cart items remaining to a product_category
add_shortcode('bottiglie', 'shortcode_bottiglie_count');
function shortcode_bottiglie_count( $atts ) {
return '<span id="bottiglie-count">' . get_bottiglie_count( array(72) ) . '</span>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
You will be needing a javascript to run a live changes to DOM, but wordpress shortcodes don't work with ajax (I think, correct me if I'm wrong). But yeah, basically you need ajax to fetch the shortcode again or manually change the DOM after clicking add to cart.

Woocommerce Single Product Page Button For Specific Categories Only

I have added code to display a "View Product Sample" button on the woocommerce page. The button functions correctly; however I would like for the button to display only for a certain category. The category that we have is "e-courses"
Here is the code I have used for the button:
add_action('woocommerce_after_add_to_cart_button','custom_button_by_categories');
function custom_button_by_categories() {
global $post;
$demoslug = $post->post_name;
$demourl = get_bloginfo('url').'/courses/'.$demoslug.'/';
$demotitle = esc_attr($post->post_title);
echo 'View Product Sample';
}
Thank you for any help you can provide.
You can do it using has_term() Wordpress function (where you will have to define your product category).
You can use woocommerce_simple_add_to_cart with a priority above 30 this way:
add_action( 'woocommerce_simple_add_to_cart', function(){
global $product, $post;
// Set HERE your product category (ID, name or slug)
if ( has_term( 'e-courses', 'product_cat', $post->ID ) ){
$demourl = get_bloginfo('url').'/courses/'.esc_attr($post->post_name).'/';
$demotitle = esc_attr($post->post_title);
echo 'View Product Sample';
}
}, 31 );
Or also using your hook:
add_action('woocommerce_after_add_to_cart_button','custom_button_by_categories');
function custom_button_by_categories() {
// Set HERE your product category (ID, name or slug)
if ( has_term( 'e-courses', 'product_cat', $post->ID ) ){
global $post;
$demoslug = $post->post_name;
$demourl = get_bloginfo('url').'/courses/'.$demoslug.'/';
$demotitle = esc_attr($post->post_title);
echo 'View Product Sample';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.

Categories