display woocommerce product weight and dimensions on a single product template

Home » Snippets » display woocommerce product weight and dimensions on a single product template
0

Created with:

Visibility: 

public

Creator: Glenn

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * WooCommerce Single Product Shortcode: Display Weight and Dimensions
 */
function wpturbo_product_weight_dimensions_shortcode($atts) {
    // Get the product object
    global $product;

    // Check if the product has weight and dimensions
    if ($product->has_weight() && $product->has_dimensions()) {
        // Get the weight and dimensions
        $weight = wc_format_weight($product->get_weight());
        $dimensions = wc_format_dimensions($product->get_dimensions(false));

        // Output the weight and dimensions
        $output = '<div class="product-weight">' . __('Weight:', 'wpturbo') . ' ' . $weight . '</div>';
        $output .= '<div class="product-dimensions">' . __('Dimensions:', 'wpturbo') . ' ' . $dimensions . '</div>';

        return $output;
    }
}
add_shortcode('wpturbo_product_weight_dimensions', 'wpturbo_product_weight_dimensions_shortcode');
				

Explanation:

To use the shortcode in your content, you can simply add [wpturbo_product_weight_dimensions] where you want the weight and dimensions to be displayed. This will dynamically display the weight and dimensions of the current product.

Remember to replace the existing code in the 'wp-plugin.php' file with the updated code.

Register an account to save your snippets or go Pro to get more features.