微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-无法在WooCommerce中获得产品重量

我正在尝试在插件获取产品重量,以将其显示在单独输入框中的边栏上.但是我无法获得产品的重量.

请看一下我的代码,并帮助我从WooCommerce产品获得产品重量:

<?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');

}

?>

谢谢.

解决方法:

我已经测试了您的代码,并且对这一部分做了一些更改,以获取更多信息:

add_action( 'woocommerce_product_Meta_end', 'showtest', 10 );

function showtest(){
    global $product;

    $prweight = $product->weight;
    // or
    // $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>';
}

这已经过测试并且可以工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐