自定义变量get_post_meta返回空值

时间:2017-06-07 14:37:20

标签: php wordpress woocommerce custom-fields

http://www.remicorson.com/woocommerce-custom-fields-for-variations/ 我按照教程介绍了如何为产品变体创建自定义字段。这很有效,但是现在我尝试将自定义字段数据导出到电子邮件自定义程序模板文件中。

我尝试这样做会输出一个空白值; " array(0){}" 注意:" $ product_idMe"正在工作,并输入正确的产品ID。

$product_attr2 = get_post_meta( $product_idMe, '_variable_loc');

echo var_dump( $product_attr2 );

以下是获取自定义变量在Wordpress管理员中运行的原始教程代码,如果这有助于引用它的制作方式:

 add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );

add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

function variation_settings_fields( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_variable_loc[' . $variation->ID . ']', 
            'label'       => __( 'Inv Location', 'woocommerce' ), 
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_variable_loc', true )
        )
    );



function save_variation_settings_fields( $post_id ) {
    $variable_loc = $_POST['_variable_loc'][ $post_id ];
    if( ! empty( $variable_loc) ) {
        update_post_meta( $post_id, '_variable_loc', esc_attr( $variable_loc ) );
    }

add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );


function load_variation_settings_fields( $variations ) {

    $variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );

    return $variations;
}

0 个答案:

没有答案