I am creating a custom layout (using Elementor) for a product - what I need is to have the dimensions of a product to display in a custom tab I have created.
Is there a shortcode for product dimensions? (some of the products are variable too if that makes a difference)
I have managed to get some code to display the long description in a shortcode (see below) however I'm not advanced enough to know what to do to get it to display product dimensions (length, width, height - ideally each one on a separate line)
Here is the long description code i found that works...
function custom_product_description($atts){
global $product;
try {
if( is_a($product, 'WC_Product') ) {
return wc_format_content( $product->get_description("shortcode") );
}
return "Product description shortcode run outside of product context";
} catch (Exception $e) {
return "Product description shortcode encountered an exception";
}
}
add_shortcode( 'custom_product_description', 'custom_product_description' );
Does anyone can give me some help to display product dimensions via a shortcode?
You can use the following simple code snippet, to display product dimensions via a shortcode:
add_shortcode( 'product_dimensions', 'display_product_dimensions' );
function display_product_dimensions( $atts ){
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => '',
), $atts, 'product_dimensions' ) );
if( $id == '' ) {
global $product;
if( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_id() );
}
}
return method_exists( $product, 'get_dimensions' ) ? wc_format_dimensions($product->get_dimensions(false)) : '';
}
Code goes in functions.php file of the active child theme (or active theme). it should works.
USAGE:
On WooCommerce product single page: [product_dimensions]
Anywhere with a defined Id (for example product Id 35): [product_dimensions id="35"]
Related
I'm trying to figure out how to target a specific variable product in woocommerce to show this message underneath the "Add to cart" button. The current code is showing for all variable product type.
`
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message' , 10, 0 );
function custom_product_message() {
global $product;
if( ! $product->is_type( 'variable' )) return; // Only for variable products
echo '<div class="woo_variable_custom_message">Hello</div>';
}
`
This is the snippet I am working on.`
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message' , 10, 0 );
function custom_product_message() {
global $product;
if( ! $product->is_type( 'variable' )) return; // Only for variable products
echo '<div class="woo_variable_custom_message">Hello</div>';
}
`
Global and dynamic solution is create a custom field in product and you can custom text field.
You can create custom field by custom code or you can use ACF plugin to create custom field in the product.
Custom code to create custom field:
https://www.cloudways.com/blog/add-custom-product-fields-woocommerce/
ACF plugin:
https://wordpress.org/plugins/advanced-custom-fields/
Then using below action hook you can make the process dynamic from admin itself using one time code.
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message', 20 );
function custom_product_message() {
global $product;
$getcustomText = get_post_meta(post_id, 'meta_key', true);
if ( !empty ($getcustomText)) {
echo '<div class="woo_variable_custom_message">Hello</div>';
}
}
Quick Note:
I know process is little big, but it will never give you trouble some or make again and again coding to add variable product id or other parameter in the theme functions file
Thanks
Found out this can target Variable or simple product type using this way.
add_action( 'woocommerce_after_add_to_cart_button', 'custom_product_message', 20 );
function custom_product_message() {
if ( is_single( '8941' )) {
echo '<div class="woo_variable_custom_message">Hello</div>';
}
}
I want to add the product's category between the title and the price of each product. As long as I edited the archive-product.php file with codes i found here, nothing seemed to work. I created a copy of the file on the active theme too.
we can use 'woocommerce_after_shop_loop_item_title' action for this case
function product_category_in_shop_loop() {
global $product;
$product_id = $product->get_id();
$cat = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'names'));
if( !empty($cat[0]) ){
echo '<p class="catil">'.$cat[0].'</p>';
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'product_category_in_shop_loop', 40 );
code goes to child theme function.php.
I need to display a 'new' badge to product on the archive page but only for products in the 'New' category
add_action( 'woocommerce_after_shop_loop_item_title', 'lottie_new_badge', 40 );
function lottie_new_badge($badge) {
global $product;
if (has_term ('new')) {
echo '<p>New</p>';
}
return $badge;
}
Got this code but not working, mixed few bits of code together to try get it to work but no luck.
There is missing arguments when using has_term() function with Woocommerce product category. Also as you are using an action hook, $badge is not needed and not has to be returned like in a filter hook. So try the following instead:
add_action( 'woocommerce_after_shop_loop_item_title', 'display_lottie_new_badge', 40 );
function display_lottie_new_badge() {
global $product;
if ( has_term ( array('new'), 'product_cat', $product->get_id() ) ) {
echo '<p class="new">New</p>';
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.
I am new to WooCommerce and am looking for a solution to display Product attributes on a post page.
I've made research and some test but nothing seems to work.
Ideally, I would like to use a shortcode taking the Product ID and displaying all his product attributes on my post page.
something like [product_page id="99" display_only_attributes ]
Here is the way to get the product attributes in a custom shortcode where you will define the product ID in as a shortcode argument id.
The function code:
if ( ! function_exists( 'display_product_additional_information' ) ) {
function display_product_additional_information($atts) {
// Shortcode attribute (or argument)
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'product_additional_information' );
// If the "id" argument is not defined, we try to get the post Id
if ( ! ( ! empty($atts['id']) && $atts['id'] > 0 ) ) {
$atts['id'] = get_the_id();
}
// We check that the "id" argument is a product id
if ( get_post_type($atts['id']) === 'product' ) {
$product = wc_get_product($atts['id']);
}
// If not we exit
else {
return;
}
ob_start(); // Start buffering
do_action( 'woocommerce_product_additional_information', $product );
return ob_get_clean(); // Return the buffered outpout
}
add_shortcode('product_additional_information', 'display_product_additional_information');
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
SHORTCODE USAGE (Updated)
with a defined product id:
[product_additional_information id='37']
Or in php:
echo do_shortcode("[product_additional_information id='37']");
In an existing product page (when "additional information" product tab is removed for example):
[product_additional_information]
Or in php:
echo do_shortcode("[product_additional_information]");
You will get something like this:
I am trying to display the product quantity for a given product ID outside WoCommerce pages, in the normal WordPress Pages or Posts.
I assume you paste some code into functions.php then have a snippet you put in the post or page.
I'm really struggling here and only half-baked answers I've found so far where none of them work for me...
How do I echo the stock quantity of a WooCommerce product ID on a normal Wordpress page or post?
The best way is to make a custom shortcode function, that is going to output the product quantity for a given product ID.
The code for this shortcode function:
if( !function_exists('show_specific_product_quantity') ) {
function show_specific_product_quantity( $atts ) {
// Shortcode Attributes
$atts = shortcode_atts(
array(
'id' => '', // Product ID argument
),
$atts,
'product_qty'
);
if( empty($atts['id'])) return;
$stock_quantity = 0;
$product_obj = wc_get_product( intval( $atts['id'] ) );
$stock_quantity = $product_obj->get_stock_quantity();
if( $stock_quantity > 0 ) return $stock_quantity;
}
add_shortcode( 'product_qty', 'show_specific_product_quantity' );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
USAGE
The shortcode works with an ID argument (the targeted product ID).
1) In a WordPress Page or Post content, just paste this shortcode in the text editor to display the stock quantity for a given product ID (Here the ID is 37):
[product_qty id="37"]
2 In any PHP code (example):
echo '<p>Product quantity is: ' . do_shortcode( '[product_qty id="37"]' ) .'</p><br>';
3 In an HTML/PHP page (example):
<p>Product quantity is: <?php echo do_shortcode( '[product_qty id="37"]' ); ?></p>
Similar: Display custom stock quantity conditionally via shortcode on Woocommerce