简体   繁体   English

Woocommerce - 自动将特定产品添加到新客户的已完成订单中

[英]Woocommerce - Automatically add specific product to completed order from a new customer

I am trying to automatically add a product to an order in WooCommerce if the customer is a new one.如果客户是新客户,我正在尝试自动将产品添加到 WooCommerce 中的订单中。 I am using " Checking if customer has already bought something in WooCommerce " answer code to check if the customer has already completed an order and add the product to his current order.我正在使用检查客户是否已经在 WooCommerce 中购买过东西回答代码来检查客户是否已经完成订单并将产品添加到他当前的订单中。 I think this should be just before the confirmation page but cannot make it work.我认为这应该在确认页面之前,但不能使其工作。

// 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(); 

You can't get the current user ID using get_current_user_id() .无法使用get_current_user_id()获取当前用户 ID。 Instead you will better get the user ID from the existing order (where you want to add a product) .相反,您最好从现有订单(要添加产品的位置)中获取用户 ID

Update: I have updated my original function has_bought() to handle guest customer orders from their billing email.更新:我已经更新了我原来的 function has_bought()来处理来自他们计费 email 的来宾客户订单。 Now as you don't ask in your question to handle guests, I haven't implemented that on the following answer.现在,由于您没有在问题中提出处理客人的问题,因此我没有在以下答案中实现这一点。

// 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;
}

Here is your revisited function code:这是您重新访问的 function 代码:

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
}

It should better work now.它现在应该更好地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 中为新客户订单添加订单备注 - Add order note to new customer orders in WooCommerce 将可变产品的“描述”字段内容添加到 woocommerce “已完成订单” email - Add "description" field contents for variable product to woocommerce "Completed order" email 在 WooCommerce 'Order Completed' 电子邮件中添加自定义产品字段 - Add Custom Product Field in WooCommerce 'Order Completed' Emails 基于 WooCommerce 客户完成订单 email 通知中的产品类别的条件文本 - Conditional text based on product category in WooCommerce customer completed order email notification 每周一自动更新WooCommerce中的订单状态从处理到完成 - Update order status from processing to completed in WooCommerce automatically on every Monday 如何在 WooCommerce 中的已完成订单 email 模板中添加新帐户信息 - How to add new account info on order completed email template in WooCommerce 将产品从 WooCommerce 中的 WP 管理员添加到客户的购物车 - Add product to customer's cart from WP admin in WooCommerce WooCommerce中特定产品和特定客户的折扣 - Discount for a specific product and a specific customer in WooCommerce 避免 WooCommerce 专门针对特定产品的新订单通知 - Avoid WooCommerce new order notification for specific product exclusively 将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中 - Add recipients from product custom fields to Woocommerce new order email notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM