Hi I would like to ask how to Echo the variation product description. I would need to echo it depending on variant chosen. I have a working code to echo short descriptin of the curent product like this
add_action( 'woocommerce_before_variations_form', 'woo_show_excerpt_shop_page_rz', 80 );
function woo_show_excerpt_shop_page_rz() {
global $product;
echo $product->post->post_excerpt;
}
I would need something similar with current variant description.
Related
I want to Display Free Shipping badge underneath the ITEM TITLE (example attached) and underneath the PRICE on a single product pag.
Example
I found the below code but not sure what changes to make in order to achieve the above.
PS: it is only for items with no shipping class.
add_action( 'woocommerce_before_shop_loop_item_title', 'single_product_label', 10 );
function single_product_label() {
global $product;
if ( empty( $product->get_shipping_class() ) ) {
echo '<span class="freedel">Free UK Delivery</span>';
}
}
To achive the result as in the example picture you have to change the hook you're using.
The hook woocommerce_before_shop_loop_item_title will execute before the product title just as it states.
Instead you should use woocommerce_after_shop_loop_item which executes after the product information but before the add to cart button.
Businessbloomer.com have a wonderful visual guide to the hooks on the Woocommerce archive page that I recommend you take a look at: https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/
This is what your code should look like instead:
Obviously you will have to style it with css to get the same result as in the picture though.
add_action( 'woocommerce_after_shop_loop_item', 'archive_product_label', 10 );
function archive_product_label() {
global $product;
if ( empty( $product->get_shipping_class() ) ) {
echo '<span class="freedel">Free UK Delivery</span>';
}
}
The example picture and code you used, all look like you want this on the archive page and not on the single product page as you have written. But for good measure I'll add the link for the visual guide to the single product page and add the code for that aswell.
Single product page visual hook guide: https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/
Here are a few hooks you could use but I'm guessing the one you want is the woocommerce_before_add_to_cart_form
Here's the code for the single product page:
Notice that I changed the above function name to archive_product_label and I'm now calling this single_product_label instead. This is to avoid conflict and to make it easier to locate if you need to change the code sometime in the future.
add_action( 'woocommerce_before_add_to_cart_form', 'single_product_label', 10 );
function single_product_label() {
global $product;
if ( empty( $product->get_shipping_class() ) ) {
echo '<span class="freedel">Free UK Delivery</span>';
}
}
If you always want the same text on both archive and single product page you could also bind the two different actions to the same function
Like this:
add_action( 'woocommerce_after_shop_loop_item', 'free_shipping_product_label', 10 );
add_action( 'woocommerce_before_add_to_cart_form', 'free_shipping_product_label', 10 );
function free_shipping_product_label() {
global $product;
if ( empty( $product->get_shipping_class() ) ) {
echo '<span class="freedel">Free UK Delivery</span>';
}
}
Hope this answers your question and teaches you a little bit about how hooks, functions and actions work in wordpress/woocommerce.
Have a great day! :)
I am trying to echo a custom field 'course_short_desc' underneath the title of each woocommerce subcategory on the parent category page (which lists all the shop subcategories).
Have spent a whole day on this without success. The closest I have got is the following code...
add_action( 'woocommerce_after_subcategory_title', 'display_short_course_info', 35 );
function display_short_course_info() {
$term_object = get_queried_object();
echo the_field('course_short_desc', 'product_cat_'.$term_object->term_id);
}
This code does echo out 'course_short_desc' but for the parent category, not the shop subcategory itself. Any help much appreciated, thanks.
You only need to pass the $term_object in de the_field() function
add_action( 'woocommerce_after_subcategory_title', 'display_short_course_info', 35 );
function display_short_course_info() {
$term = get_queried_object();
the_field('course_short_desc', $term);
}
also the_field() function already echo's the field.
this function is the same as echo get_field().
I use WooCommerce shortcode to show some products on the front page.
Like this [products limit="3" category="my-category" ids="86, 71, 54"].
The front page is a regular WordPress static page. The problem is that it doesn't show product short descriptions. If I use the code below but for is_front_page(), it shows short description of a regular WordPress post (not of the listed products).
function custom_short_description() {
if ( is_product_category() ) {
echo '<div class="custom-short-description">' . get_the_excerpt() . '</div>';
} }
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_short_description', 45 );
Adding to the function
global $post;
$product = get_product($loop->post);
and using
$product->post->post_excerpt;
didn't help.
Any ideas how to show product short descriptions?
===================
Update
===================
If you create custom loops, you might want to create variables at the beginning of the loop and then use them:
$product = wc_get_product( $loop->post->ID );
$product_short_description = $product->get_short_description();
$product_url = $product->add_to_cart_url();
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_short_description', 45 );
function custom_short_description() {
if (is_front_page()) {
global $product;
echo '<div class="custom-short-description">' . $product->get_short_description() . '</div>';
}
}
This should get you the outcome you're looking for.
Tried and tested WordPress 5.1.
Have Woocommerce setup with a range of variable products. In the variable tab I've setup a unique price, image, description, weight & dimensions for each item.
All variable data displays as expected on the front-end except the dimensions & weight.
Despite hours of searching, I cannot find any documentation, tutorials, hints on how to hook into it.
Have Woocommerce templates setup and know that I will need to hook into the do_action( 'woocommerce_single_variation' ); in variable.php.
Anyone know how to get each variable's dimensions & weight to display beneath the variable description?
If you have the variation ID, you can use it to create a new WC_Product(). This object will then have properties available on it for the $length, $width, and $height. See the docs here (at the bottom under "Magic Properties").
To get the variations for a given product, you can use the global $product and then the get_available_variations() function.
global $product
$variations = $product->get_available_variations();
foreach ( $variations as $variable_array ){
$variation = new WC_Product( $variable_array['variation_id'] );
echo "The length is {$variation->length}.";
}
If you want to display additional information regarding your variable product add this function to your child theme’s function.php (or plugin). You’ll probably want to alter the html tags to fit your theme:
add_filter( 'woocommerce_product_additional_information', 'tim_additional_tab', 9 );
function tim_additional_tab( $product ){
$variations = $product->get_available_variations();
//print the whole array in additional tab and examine it
//echo '<pre>';
//print_r($variations);
//echo '</pre>';
//html and style to your likings
foreach ( $variations as $key ){
echo $key['image']['title'].'<br>';
echo $key['weight_html'].'<br>';
echo $key['dimensions_html'].'<br>';
}
}
I have hard time trying to find the solution, does somebody know how to get this:
I have one WordPress post in more than one category, but only one is permalink category. I need to get the ID only of that permalink category (I need this info so I can take few latest posts from permalink category via custom query).
url looks like this http://domain.com/category-name/post-title
I need that "category-name" ID.
A Good one to use is:
<?php $page_object = get_queried_object(); ?>
<h1><?php echo $page_object->cat_name; ?></h1>
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) ); get url
$url_array = explode('/',$current_url);
$retVal = !empty($url_array[5]) ? $url_array[5] : $url_array[4] ;
$idObj = get_category_by_slug($retVal);
echo $idObj->name
My answer is:
function get_category_by_url($url) {
foreach( (get_the_category()) as $category) {
if ( get_category_link($category->cat_ID) == $url )
return $category->slug;
}
return false;
}
$category = end(get_the_category());
$current = $category->cat_ID;
echo 'id='.$current . ' - name=' . $category->cat_name;
If the post belongs to many categories, what if you're viewing the post from a second category. In that case retrieving the category ID of the permalink category may not help, since you would need the related posts of the current category in action.
For that, you can get the ID by passing the category name as follows:
<?php get_cat_ID($cat_name)?>
Does this help?
Wordpress chooses the oldest category as the permalink category. There's no way to change that behavior unless you use some plugin. If you choose to use a plugin you make take the category ID from plugin settings.
You can list all categories of this post and choose most relevant category.
Use the following code inside The Loop:
foreach((get_the_category()) as $category)
{
if ( $category->cat_ID == 1000 )
; // DO SOMETHING
}