2016-10-06 3 views
3

zu bekommen Ich versuche, Produktgewicht in meinem Plugin zu bekommen, um es auf Seitenleiste in separaten Eingabefeld zu zeigen. Aber ich kann das Produktgewicht nicht erreichen.Nicht in der Lage, Produktgewicht in WooCommerce

Bitte nehmen Sie sich einen Blick auf meinem Code und mir helfen, das Produktgewicht von WooCommerce Produkt zu erhalten:

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

} 

?> 

Dank.

Antwort

4

Ich habe Ihren Code getestet und ich habe ein wenig diesen Teil geändert, um das Gewicht zu bekommen:

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

Diese getestet und funktioniert.

+0

Danke lieber, es hat mein Problem wirklich behoben :) – wind