从WooCommerce中的ID获取正确的产品对象类型

时间:2019-04-01 10:15:52

标签: php wordpress woocommerce product

我正在将woocomerce的Admin部分的产品页面输入一个meta框。

在此框内,我需要检查当前产品类型以围绕该类型创建代码。

以某种方式,只有在这种设置下,我才能得到不正确的结果,而在其他地方,我可以执行相同的代码并返回正确的数据。我已经在自己的管理页面中进行了测试,甚至在前端显示了该类型。只是管理产品页面出现了这个异常。

所有CMS和插件均三重选中以保持最新状态。

代码很简单:

add_action( 'add_meta_boxes', 'reboot_add_meta_boxes' );
if ( ! function_exists( 'reboot_add_meta_boxes' ) )
{
    function reboot_add_meta_boxes()
    {
    add_meta_box( 'reboot_custom_testbox', __('Test Box','woocommerce'), 'reboot_custom_testbox_function', 'product', 'advanced', 'core' );
    }
}

if ( ! function_exists( 'reboot_custom_testbox_function' ) )
{
    function reboot_custom_testbox_function()
    {
        global $post;
        $RebootProduct = new WC_Product( $post->ID );
        $RebootMessage = '';
        if( $RebootProduct->get_type() == 'simple' ){
            $RebootMessage = 'This is a simple Product <br>';
        } elseif( $RebootProduct->get_type() == 'variable' ){
            $RebootMessage = 'This is a Variable Product <br>';
        } else{
            $RebootMessage = $RebootProduct->get_type();
        }
        echo $RebootMessage;
        return;
    }
}


那我要去哪里错了?

1 个答案:

答案 0 :(得分:1)

您应更改为:new WC_Product($ post-> ID);到wc_get_product($ post-> ID); 所以你有

$RebootProduct = wc_get_product( $post->ID );