带有Metabox插件的WooCommerce

时间:2012-12-04 作者:FlimFlam

我正在使用woocommerce作为我的电子商务解决方案和metabox插件(http://www.deluxeblogtips.com/meta-box/ ) 在实现元框时,让我的生活更轻松。我从来没有遇到过任何问题,但我现在需要将自己的自定义元框添加到woocommerce产品帖子类型中。。。。但它从未被添加?如果我以老式的add\\u meta\\u box方式添加元框,并且元框添加正确。。。。所以我想知道你是否知道为什么使用插件会导致问题?下面是我用来将meta-box添加到全局meta\\u-box数组的代码:

global $meta_boxes;

$prefix = "esfproduct_";

// Aggregator metaboxes

$meta_boxes[] = array(
\'id\'    => \'additionalproductdetails\',
\'title\' => \'Additional Details\',
\'pages\' => array( \'product\' ),
\'context\' => \'normal\',
\'priority\' => \'side\',
\'fields\' => array(
    array(
        \'name\' => \'Product Features\',
        \'id\'   => "{$prefix}productfeatures",
        \'type\' => \'text\'
    ),
)
);
感谢您提供的任何帮助!

2 个回复
SO网友:mroncetwice

下面是我正在使用的元盒代码,它对我来说运行良好:

// Add meta boxes with TinyMCE via wp_editor() function

// Define the custom box
add_action( \'add_meta_boxes\', \'product_details_add\' );                                                      
// Do something with the data entered
add_action( \'save_post\', \'product_details_save\' );
// Adds a box to the main column on the Product post_type edit screens
function product_details_add() {
    add_meta_box( \'product_details\', \'Product Details\', \'product_details_call\', \'product\', \'normal\', \'high\' );
}
// Prints the box content
function product_details_call( $post ) {
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), \'product_details_noncename\' ); 
    $field_value = get_post_meta( $post->ID, \'product_details_meta\', false );
    wp_editor( $field_value[0], \'product_details_meta\' );
}
// When the post is saved, saves our custom data
function product_details_save( $post_id ) {  
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
            return;
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( ( isset ( $_POST[\'product_details_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'product_details_noncename\'], plugin_basename( __FILE__ ) ) ) )
            return;
    // Check permissions
    if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
        if ( ! current_user_can( \'edit_page\', $post_id ) ) {
            return;
        }       
    }
    else {
        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }
    }
    // OK, we\'re authenticated: we need to find and save the data
    if ( isset ( $_POST[\'product_details_meta\'] ) ) {
        update_post_meta( $post_id, \'product_details_meta\', $_POST[\'product_details_meta\'] );
    }   
}

////////////
然而,get_post_meta() 没有返回模板页面中的数据,因此我使用更“直接”的方法来获取和显示元内容:

<?php echo $product->product_custom_fields[\'product_details_meta\'][0];?>
当然,这假设您还声明global $product; 模板文件中此代码上方的某个位置。

SO网友:Anh Tran

我认为你使用的代码已经过时了。以下是将元框和自定义字段添加到WooCommerce产品的代码,该产品使用最新版本的Meta Box:

add_filter( \'rwmb_meta_boxes\', \'your_prefix_meta_boxes\' );
function your_prefix_meta_boxes( $meta_boxes ) {
    $prefix = \'esfproduct_\';
    $meta_boxes[] = array(
        id\' => \'additionalproductdetails\',
        \'title\' => \'Additional Details\',
        \'context\' => \'normal\',
        \'priority\' => \'side\',
        \'fields\'     => array(
            array(
                \'name\' => \'Product Features\',
                \'id\'   => "{$prefix}productfeatures",
                \'type\' => \'text\'
            ),
        ),
    );
    return $meta_boxes;
}

结束