将自定义变体字段数据添加到CSV订单导出-WooCommerce

时间:2018-06-30 13:39:56

标签: php wordpress csv woocommerce

我一直在到处寻找答案,只是一直遇到障碍。我使用http://www.remicorson.com/woocommerce-custom-fields-for-variations/在WooCommerce中添加了自定义变体字段,这些字段在更新产品时可以正确保存。

我已经安装了WooCommerce Customer / Order CSV Export,并添加了一个meta_field列,其中放置了在以下代码中找到的meta_key。

Screenshot of the fields

但是,当我导出CSV文件时,这些字段为空。任何人都有通过CSV导出订单数据时出现自定义变体字段数据的经验。

下面列出的是正在使用的自定义变体代码。任何指导将不胜感激。

/**
 * Create new fields for variations
 *
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {

    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => 'source_code[' . $variation->ID . ']', 
            'label'       => __( 'Supplier', 'woocommerce' ), 
            'desc_tip'    => 'true',
            'value'       => get_post_meta( $variation->ID, 'source_code', true )
        )
    );
    woocommerce_wp_text_input( 
        array( 
            'id'          => 'source_prod_id[' . $variation->ID . ']', 
            'label'       => __( 'Supplier ID', 'woocommerce' ), 
            'desc_tip'    => 'true',
            'value'       => get_post_meta( $variation->ID, 'source_prod_id', true ),
            'custom_attributes' => array(
                            'step'  => 'any',
                            'min'   => '0'
                        ) 
        )
    );
    woocommerce_wp_text_input( 
        array( 
            'id'          => 'pkg_desc[' . $variation->ID . ']', 
            'label'       => __( 'Package Size', 'woocommerce' ), 
            'desc_tip'    => 'true',
            'value'       => get_post_meta( $variation->ID, 'pkg_desc', true ),
            'custom_attributes' => array(
                            'step'  => 'any',
                            'min'   => '0'
                        ) 
        )
    );          
                woocommerce_wp_text_input( 
        array( 
            'id'          => 'cost[' . $variation->ID . ']', 
            'label'       => __( 'Cost', 'woocommerce' ), 
            'desc_tip'    => 'true',
            'value'       => get_post_meta( $variation->ID, 'cost', true ),
            'custom_attributes' => array(
                            'step'  => 'any',
                            'min'   => '0'
                        ) 
        )
    );
}


// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes',  'variation_settings_fields', 10, 3 );

/**
 * Save new fields for variations
 *
*/
function save_variation_settings_fields( $post_id ) {

    // Text Field
    $text_field = $_POST['source_code'][ $post_id ];
    if( ! empty( $text_field ) ) {
        update_post_meta( $post_id, 'source_code', esc_attr( $text_field ) );
    }
    $number_field = $_POST['source_prod_id'][ $post_id ];
    if( ! empty( $number_field ) ) {
        update_post_meta( $post_id, 'source_prod_id', esc_attr( $number_field ) );
    }
    $number_field = $_POST['pkg_desc'][ $post_id ];
    if( ! empty( $number_field ) ) {
        update_post_meta( $post_id, 'pkg_desc', esc_attr( $number_field ) );
    }
    $number_field = $_POST['cost'][ $post_id ];
    if( ! empty( $number_field ) ) {
        update_post_meta( $post_id, 'cost', esc_attr( $number_field ) );
    }

}


// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

0 个答案:

没有答案