woocomerce:状态完成后获取自定义产品输入字段的单个值

时间:2019-01-29 03:42:03

标签: php wordpress hook

我在获取产品自定义输入字段的值以用于在邮件中发送“ customer_number”值时遇到问题。 我已经可以将其发送到购物车并订购详细信息,但是 我如何在代码底部检索客户_number的变量值以进行电子邮件操作,以供以后使用(例如mysql查询)。

我在电子邮件中只得到空值。

// add custom input field for single product //    
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_fields_single_product', 20 );  
function add_custom_fields_single_product(){  
global $product;  
?>  
    <div class="product-custom-fields">  
        Customer Number: <input type="text" placeholder="<?php _e('Customer Number', 'woocommerce'); ?>" name="customer_number">  
    </div>  

    <div class="clear"></div>  
<?php  
}  

// add cart item data //  
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 ){  
if(isset($_POST['customer_number']))  
    $cart_item_data['custom_data']['customer_number'] = sanitize_text_field($_POST['customer_number']);  


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

return $cart_item_data;  
}

// show custom input field data in cart //  
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( 'customer_number', $cart_item['custom_data'] ) )  
    $item_data[] = array(  
        'key'   => __('Customer Number', 'woocommerce'),  
        'value' => $cart_item['custom_data']['customer_number']  
    );



return $item_data;  
}

// Save values as Order item data and display them everywhere //    
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';  

// Save values:   

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


}  


// send mail with customer_number   
add_action( 'woocommerce_order_status_completed', 'mail_order', 10, 1 );  
function mail_order( $order_id) {  
$adminmail = 'xxx@xxx.com';  
$customer_number =  $values['custom_data']['customer_number'] ;  

wp_mail( $adminmail, "Order has completed ",  "Customer Number: $customer_number");  

return $item_data;  
}  

1 个答案:

答案 0 :(得分:0)

add_action( 'woocommerce_order_status_completed', 'mail_order', 10, 1 );  
function mail_order( $order_id) 
{ 
    $order          = new WC_Order($order_id);
    $values         = $order->get_items();
    //echo '<pre>'. print_r($items, 1). '</pre>'; /* fetch all items */
    $adminmail = 'xxx@xxx.com';  
    $customer_number =  $values['custom_data']['customer_number'] ;  

    wp_mail( $adminmail, "Order has completed ",  "Customer Number: $customer_number");  

    //return $item_data;

}