保存订购产品变体到订单的自定义元数据

时间:2019-07-28 17:56:54

标签: php wordpress woocommerce woocommerce-subscriptions

我需要能够将管理数据添加到订阅产品版本(Woo订阅扩展)中,以便在下订单时将这些数据传递到履行中心。

我遇到的问题是从产品中获取这些值,并将其传递到订单和电子邮件通知中,以便可以将其正确导出,添加到运输标签中并通过各种woo通知通过电子邮件发送。

我已将自定义元字段添加到订阅产品的变体中。我可以将数据添加到这些文件中,然后使用正确的版本ID将数据正确地保存到数据库中。没问题。

我可以在购物车和结帐页面上显示自定义测试数据,并将这些数据保存到订单中,就像您在代码中看到的那样。

为简洁起见,下面将代码简化了,但实际上有6个文本字段:

// Add custom field inputs to each product variation
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );

function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input( 
        array(
            'id' => '_dose_customer_field[' . $loop . ']',
            'label'       => __( 'Dose (Customer)', 'woocommerce' ), 
            'placeholder' => __('Enter the treatment dosage','woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Enter dosage info for this variation', 'woocommerce' ),
            'class' => '',
            'value' => get_post_meta( $variation->ID, '_dose_customer_field', true )
        )
    );
}

// Save custom field on product variation save 
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );

function save_custom_field_variations( $variation_id, $i ) {
    $_dose_customer_field = $_POST['_dose_customer_field'][$i];
    if ( isset( $_dose_customer_field ) ) update_post_meta( $variation_id, '_dose_customer_field', esc_attr( $_dose_customer_field ) );   
}

// Add metadata to cart item post.
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_cart_item_data', 10, 2 );

function add_custom_fields_cart_item_data( $cart_item_data, $product_id ){    

    $cart_item_data['custom_data']['_dose_customer_field'] = 'TEST DATA' /* HOW TO GET DATA FROM CUSTOM FIELD HERE */       

    $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
    WC()->session->set( 'custom_data', $cart_item_data['custom_data'] );

    return $cart_item_data;
}

// Display custom meta data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'display_custom_fields_cart_item_data', 10, 2 );

function display_custom_fields_cart_item_data($item_data, $cart_item){

    if( ! array_key_exists( 'custom_data', $cart_item ) )
        return $item_data;

    if( array_key_exists( '_dose_customer_field', $cart_item['custom_data'] ) )
        $item_data[] = array(
            'key'   => __('Dose (Customer)', 'woocommerce'),
            'value' => $cart_item['custom_data']['_dose_customer_field']
        );

    return $item_data;
}

// Add metadata to the order.
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_meta', 20, 4);

function save_custom_fields_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if( ! isset($values['custom_data']) )
        return;

    $text_domain ='woocommerce';

    if( array_key_exists('_dose_customer_field', $values['custom_data']) ){
        $item->update_meta_data( __('Dose (Customer)', $text_domain), $values['custom_data']['_dose_customer_field'] );
    }    
}

// Frontend & emails: Display data on notifications
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_commodity_code', 10, 4 );
function vp_order_item_display_commodity_code( $item_id, $item, $order, $plain_text ) {
    // Not on admin
    //if( is_admin() ) return;

    if( $value = $item->get_meta('_dose_customer_field') ) {
        $value = '<strong>' . __("Dose (Customer)", "woocommerce") . ':</strong> ' . esc_attr( $value );

        // On orders
        if( is_wc_endpoint_url() )
            echo '<div class="vp-ccode"><small>' . $value . '</small></div>';
        // On Emails
        else
            echo '<div>' . $value . '</div>';
    }
}

因此,除了从自定义字段中获取数据之外,其他所有操作均正常。当产品变体上的数据已经可用时,我必须创建一个新数组(这里称为“ custom_data”)并将其添加到cart_item_data中,这似乎很奇怪。

我的问题是如何在add_custom_fields_cart_item_data()中获取元数据?这甚至是正确的方法吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

由于该产品存在该自定义数据,因此您可以轻松获取它并将其保存为订单项自定义数据(这样它将显示在所有位置):

完整代码(更改了最后两个功能):

// Add custom field inputs to each product variation
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input( 
        array(
            'id' => '_dose_customer_field[' . $loop . ']',
            'label'       => __( 'Dose (Customer)', 'woocommerce' ), 
            'placeholder' => __('Enter the treatment dosage','woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Enter dosage info for this variation', 'woocommerce' ),
            'class' => '',
            'value' => get_post_meta( $variation->ID, '_dose_customer_field', true )
        )
    );
}

// Save custom field on product variation save 
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
    $_dose_customer_field = $_POST['_dose_customer_field'][$i];
    if ( isset( $_dose_customer_field ) ) update_post_meta( $variation_id, '_dose_customer_field', esc_attr( $_dose_customer_field ) );   
}

// Display custom meta data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'display_custom_fields_cart_item_data', 10, 2 );
function display_custom_fields_cart_item_data( $item_data, $cart_item ) {
    if( $value = $cart_item['data']->get_meta( '_dose_customer_field' ) )
        $item_data[] = array(
            'key'   => __('Dose (Customer)', 'woocommerce'),
            'value' => $value
        );

    return $item_data;
}

// Add metadata to the order item (and display it everywhere).
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_fields_as_order_item_meta', 10, 4);
function save_custom_fields_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if( $meta_value = $values['data']->get_meta( '_dose_customer_field' ) )
        $item->update_meta_data( __('Dose (Customer)', 'woocommerce'), $meta_value );  
}

代码进入活动子主题(或活动主题)的functions.php文件中。应该可以。