I am trying to get product weight in my plugin to show it on sidebar in separate input box. But I am unable to get the product weight.
Please take a look at my code and help me to get the product weight from WooCommerce product:
<?php
/**
* Plugin Name: Calculator
* Plugin URI: http://wordpress.org/
* Description: Calculate
* Version: 1.0.0
* Author: wtm
* Author URI: http://wtynet.com
*/
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return; // Check if WooCommerce is active
add_action( 'woocommerce_product_meta_end', 'showtest', 10 );
function showtest(){
$prweight = WC_Product::get_weight();
echo "<div style='border: 2px solid #43515f; padding: 10px; text-align:center;'>";
your_css_and_js();
echo "<input id= 'prweight' type='text' value='".$prweight."'>";
echo "</div>";
}
function your_css_and_js() {
wp_register_style('your_css_and_js', plugins_url('css/style.css',__FILE__ ));
wp_enqueue_style('your_css_and_js');
wp_register_script( 'your_css_and_js', plugins_url('js/script.js',__FILE__ ));
wp_enqueue_script('your_css_and_js');
}
?>
Thanks.
I have tested your code and I have changed a little bit this part, to get the weight:
add_action( 'woocommerce_product_meta_end', 'showtest', 10 );
function showtest(){
global $product;
$prweight = $product->get_weight();
echo '<div style="border: 2px solid #43515f; padding: 10px; text-align:center;">';
your_css_and_js();
echo '<input id="prweight" type="text" value="'.$prweight.'">';
echo '</div>';
}
This is tested and works.