php - Woocommerce - 自动将特定产品添加到新客户的已完成订单中
问题描述
如果客户是新客户,我正在尝试将产品自动添加到 WooCommerce 中的订单中。我正在使用“检查客户是否已经在 WooCommerce 中购买过东西”回答代码来检查客户是否已经完成订单并将产品添加到他当前的订单中。我认为这应该在确认页面之前,但不能使其工作。
// Check if user has already bought
function has_bought( $user_id = 0 ) {
global $wpdb;
$customer_id = $user_id == 0 ? get_current_user_id() : $user_id;
$paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$results = $wpdb->get_col( "
SELECT p.ID FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $paid_order_statuses ) . "' )
AND p.post_type LIKE 'shop_order'
AND pm.meta_key = '_customer_user'
AND pm.meta_value = $customer_id
" );
}
if( has_bought() )
function add_the_product()
{
$order_id = intval($_POST['order_id']);
$product_id = intval($_POST['product_id']);
//getting order Object
$order = wc_get_order($order_id);
// gettting the product
$product = wc_get_product($product_id);
$back_data = array('error' => 0, 'msg' => '');
if($order !== false AND $product !== false)
{
// Add the product to the order
$order->add_product($product, 1);
// Save the order data
$order->save();
解决方案
您无法使用获取当前用户 ID get_current_user_id()
。相反,您最好从现有订单(要添加产品的位置)中获取用户 ID。
更新:我已经更新了我的原始功能
has_bought()
,以处理来自他们的账单电子邮件的来宾客户订单。现在,由于您没有在问题中提出处理客人的问题,因此我没有在以下答案中实现这一点。
// Check if user has already bought
function has_bought( $value = 0 ) {
global $wpdb;
// Based on user ID (registered users)
if ( is_numeric( $value) ) {
$meta_key = '_customer_user';
$meta_value = $value == 0 ? (int) get_current_user_id() : (int) $value;
}
// Based on billing email (Guest users)
else {
$meta_key = '_billing_email';
$meta_value = sanitize_email( $value );
}
$paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
print_pr($meta_key); print_pr($meta_value);
$count = $wpdb->get_var( $wpdb->prepare("
SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $paid_order_statuses ) . "' )
AND p.post_type LIKE 'shop_order'
AND pm.meta_key = '%s'
AND pm.meta_value = %s
", $meta_key, $meta_value ) );
// Return a boolean value based on orders count
return $count > 0 ? true : false;
}
这是您重新访问的功能代码:
function add_the_product()
{
// Checking posted data
if( ! ( isset($_POST['order_id']) && isset($_POST['product_id']) ) )
return; // Exit
$order_id = intval($_POST['order_id']);
$product_id = intval($_POST['product_id']);
// Getting the order Object
$order = wc_get_order($order_id);
// Getting the product Object
$product = wc_get_product($product_id);
$back_data = array('error' => 0, 'msg' => '');
// Checking order and product objects
if( ! is_a($order, 'WC_Order') || ! is_a($product, 'WC_Product') )
return; // Exit
// Checking that customer has not already bought something
if( has_bought( $order->get_customer_id() ) )
return; // Exit
// Add the product to the order
$order->add_product($product, 1);
// Calculate totals and save order data
$order->calculate_totals(); // the save() method is included
}
它现在应该更好地工作。