WooCommerce product additional information shortcode - php

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:

Related

Display WooCommerce Product Dimensions via a Shortcode

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"]

Get Woocommerce product tag post count via a shortcode

I need to count the number of times a "product tag" is used across the website. Each product has a number of tags associated with it.
I thought about creating a shortcode which I can then reference when needed. The shortcode I have created below crashes the website.
// function
function tag_count_shortcode() {
// Identify the tag ID and then run a count.
$term = get_tag( $tag_ID );
$count = $term->count;
// Output the total number of times a tag is used
return $count;
}
// register shortcode
add_shortcode('tagcount', 'tag_count_shortcode');
Im not sure where I am going wrong with this. Would really appreciate any assistance.
Platform: WordPress |
File with code: "Functions.php"
Cheers
This is about product tags which is a WooCommerce custom taxonomy and not WordPress Tags.
Also a product can have many product tags, so the following code will handle the count on the first product tag term. This shortcode also handle some arguments:
the taxonomy (can handle any custom taxonomy, WordPress tag and category too) - By default: product tag
the term_id (can handle any defined term ID) - by default it gets the term on single product pages
the post_id - By default the current product ID
The code:
function term_count_shortcode( $atts ) {
extract( shortcode_atts( array(
'taxonomy' => 'product_tag', // Product tag taxonomy (by default)
'term_id' => 0,
'post_id' => get_queried_object_id(), // The current post ID
), $atts ) );
// For a defined term ID
if( $term_id > 0 ) {
// Get the WP_term object
$term = get_term_by( 'id', $term_id, $taxonomy );
if( is_a( $term, 'WP_Term' ) )
$count = $term->count;
else
$count = 0;
}
// On product single pages
elseif ( is_product() && $term_id == 0 && $post_id > 0 ) {
// Get the product tag post terms
$terms = get_the_terms( $post_id, $taxonomy );
// Get the first term in the array
$term = is_array($terms) ? reset( $terms ) : '';
if( is_a( $term, 'WP_Term' ) )
$count = $term->count;
else
$count = 0;
} else {
$count = false;
}
return $count;
}
add_shortcode('term_count', 'term_count_shortcode');
Code goes in function.php file of your active child theme (or active theme). Tested and works.
USAGE:
1). Basic usage: Display the first product tag count from product single page: [term_count]
2). Usage with arguments:
With a defined term ID: [term_count term_id="58"]
With a defined term ID and Taxonomy: [term_count term_id="15" taxonomy="product_cat"]
With a defined term ID and post ID: [term_count term_id="15" post_id="37"]

Remove sidebar from product categories which display type is subcategories in Woocommerce

I'd like to write a function that removes the sidebar from any product category page in woocommerce whose display type is subcategories.
Some kind of function that says if this category has display type subcategories then disappear the sidebar.
Any help is appreciated.
It mainly depend on your theme own customizations. So the following code will only handle:
The default woocommerce behavior based on woocommerce_sidebar action hook.
The storefront theme based on storefront_sidebar action hook.
The custom conditional function:
First, here below is a custom conditional function that will check if a product category term is set with 'subcategories' display type:
// Custom conditional function that check for "subcategories" display type in product categories term
function is_subcategory_display_type( $term ) {
$taxonomy = 'product_cat';
if( ! term_exists( $term, $taxonomy ) )
return false;
if( ! is_numeric( $term ) )
$term = get_term_by( 'slug', sanitize_title( $term ), $taxonomy )->term_id;
return get_term_meta( $term, 'display_type', true ) === 'subcategories' ? true : false;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Then you will add one of those depending on your theme:
1) For normal themes using the default woocommerce_sidebar hook:
// Removing default themes woocommerce sidebar conditionally
add_action( 'woocommerce_sidebar', 'remove_woocommerce_sidebar', 1, 1 );
function remove_woocommerce_sidebar( $name ){
$queried_object_id = get_queried_object_id();
if ( is_product_category() && is_subcategory_display_type( $queried_object_id ) ){
remove_action('woocommerce_sidebar','woocommerce_get_sidebar', 10 );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) For Storefront theme using it's own storefront_sidebar hook:
// Removing default "Storefront" theme woocommerce sidebar conditionally
add_action( 'storefront_sidebar', 'remove_storefront_get_sidebar', 1, 1 );
function remove_storefront_get_sidebar( $name ){
$queried_object_id = get_queried_object_id();
if ( is_product_category() && is_subcategory_display_type( $queried_object_id ) ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
3) Other themes with specific customizations:
You will have to find out which hook is used to make the code work.
By default, woocommerce uses the default type (displays subcategories if they exist and products if there are no subcategories)
To check the current value, use a conditional function:
woocommerce_products_will_display()
So you can remove the sidebar like this:
function remove_storefront_sidebar( $name ){
if ( is_product_category() && !woocommerce_products_will_display() ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');
Another option is to hide only if the display type is "subcategories":
function remove_storefront_sidebar( $name ){
$display_type = woocommerce_get_loop_display_mode();
if ( is_product_category() && 'subcategories' === $display_type ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');

WooCommerce: Display stock quantity for a given product ID on normal Pages or Posts

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

how to display product description of woo commerce products in normal post of wordpress

i want to show short product description and additional information in my normal post of wordpress of woo commerce products.is there any way of doing this by short codes or from php
in single post file in wordpress
There is not a built in shortcode to do this, but it is easy enough to add one. In the functions.php file (preferably in your Child Theme), add the following to create a shortcode like [product_shortdesc id="123"/].
function product_shortdesc_shortcode( $atts ){
// use shortcode_atts() to set defaults then extract() to variables
extract( shortcode_atts( array( 'id' => false ), $atts ) );
// if an $id was passed, and we could get a Post for it, and it's a product....
if ( ! empty( $id ) && null != ( $product = get_post( $id ) ) && $product->post_type = 'product' ){
// apply woocommerce filter to the excerpt
echo apply_filters( 'woocommerce_short_description', $product->post_excerpt );
}
}
// process [product_shortdesc] using product_shortdesc_shortcode()
add_shortcode( 'product_shortdesc', 'product_shortdesc_shortcode' );

Categories