2016-10-06 6 views
3

別の入力ボックスにサイドバーに表示するようにプラグインで製品重量を取得しようとしています。しかし、私は製品の重量を得ることができません。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'); 

} 

?> 

感謝。

答えて

4

私はあなたのコードをテストしていると私は体重を取得するには、この部分を少し変更しました:

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>'; 
} 

これはテストされ、機能しています。

+0

本当にありがとう、私の問題を修正しました:) – wind

関連する問題