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

时间:2020-06-04 15:54:19

标签: php wordpress woocommerce product orders

如果客户是新客户,我正在尝试自动将产品添加到WooCommerce的订单中。我正在使用Checking if customer has already bought something in 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(); 

1 个答案:

答案 0 :(得分:1)

您使用get_current_user_id() 无法获取当前用户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
}

现在应该更好地工作。

相关问题