防止访客用户在WooCommerce中多次订购产品



我要做的是利用我的子主题的functions.php中的代码来阻止客人再次订购特定的产品。如果他们的购物车中有一个特定的product_id(他们之前订购过),他们将被重定向到一个特定的url,而不是正在完成的订单。

我们的网站使用电话号码(第一个数字没有零)作为用户名,为了使订购过程尽可能简单,用户在结帐时不需要登录。

我们正试图找到一种方法来检查billing_phone是一个已存在的用户名,然后检查user_id之前是否购买过特定的产品。如果他们这样做了,尝试防止这种情况并将他们重定向到特定的url,在他们点击结帐页面的下订单按钮后。

灵感来自

:

  • 如何避免在WooCommerce中再次购买相同的产品,以客人身份将产品添加到购物车中并在结帐时登录

  • Woocommerce After a Order Before Payment Hook

我们尝试构造这段代码,但是没有成功。没有阻止顺序重复:

function action_woocommerce_check_cart_items() {

// Retrieve the current user from billing_phone

// get all the order data
$order = new WC_Order($order_id);

//get the user phone from the order
$order_phone = $order->billing_phone;

//phone without zero in first digit
$phone_nozero = substr($order_phone, 1);

//gets user_id using phone number and saves it to $current_user
$current_user = get_user_by('login', $phone_nozero);


// Initialize
$flag = false;

// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_id = "916";
// Checks if a user (by email or ID or both) has bought an item
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
// Flag becomes true
$flag = true;

// Break loop
break;
}
}

// True
if ( $flag ) {
//Here, we likely require a code to prevent the order from being completed.
//redirect to specific url
wp_redirect( 'https://myspecificurl.com' );
}
}  
//hook for after clicking place order button. inspired by : 
add_action( 'woocommerce_new_order' , 'action_woocommerce_check_cart_items', 1, 1 );

如有任何建议,不胜感激。

您的代码尝试包含多个错误,因此有些注释:

  • woocommerce_new_order将在新创建的订单事件中执行,因此对于您的问题来说"太晚了">
  • 使用woocommerce_check_cart_items动作钩子代替
  • $order->billing_phone是自WooCommerce 3.0取代$order->get_billing_phone(),但不适用在这种情况下。这是因为$order对象只有在下订单后才知道,我们将使用WC()->session->get( 'customer' )来代替
  • 您提到用户名是基于电话号码的,但是您的问题是关于"仅供客人使用"的。客人没有用户名
  • 在你的代码尝试中,你只检查一个产品ID(916),而订单通常由几个不同的产品组成
  • wc_customer_bought_product()功能只适用于有帐号的用户。所以我们将不得不使用/编写一个自定义函数,它是基于现有的wc_customer_bought_product()函数
  • 使用wp_safe_redirect()wp_redirect()

得到:

function has_bought_items_by_phone_number( $phone_number, $product_ids ) {
global $wpdb;

$product_ids = is_array( $product_ids ) ? implode( ',', $product_ids ) : $product_ids;
$line_meta_value = $product_ids != 0 ? 'AND woim.meta_value IN (' . $product_ids . ')' : 'AND woim.meta_value != 0';
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_status IN ( 'wc-processing' )
AND pm.meta_key = '_billing_phone'
AND pm.meta_value = '$phone_number'
AND woim.meta_key IN ( '_product_id', '_variation_id' ) $line_meta_value 
" );
// Return true if count is higher than 0 (or false)
return $count > 0 ? true : false;
}
function action_woocommerce_check_cart_items() {
// Only for guests
if ( is_user_logged_in() ) return;
// Get session
$customer = WC()->session->get( 'customer' );
// NOT empty phone field
if ( ! empty( $customer['phone'] ) ) {
// Sanatize
$phone_number = wc_sanitize_phone_number( $customer['phone'] );

// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Initialize (do not change)
$product_ids = array();

// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Get product ID and push to array
$product_ids[] = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
}

// NOT empty
if ( ! empty ( $product_ids ) ) {
// Call function, and if true
if ( has_bought_items_by_phone_number( $phone_number, $product_ids ) ) {
// Performs a safe redirect
wp_safe_redirect( 'https://yoursite.com/custom-url-1' );
exit;
}
}
}
}
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10 );

注1)只在他们点击结帐页面上的下订单按钮后才应用此操作(重定向)需要使用与woocommerce_check_cart_items相对的woocommerce_checkout_process钩子。

因为根据我目前的答案,这只会发生在某些情况下,如果电话号码是未知的。

然而,重定向会显示一个错误消息,而不会被执行。因此,如果您仍然希望这样做,您应该通过显示一条消息来替换重定向,该消息随后包含到页面

的链接。

注2)我的答案将适用于所有产品,仅将此应用于特定产品:

替换:

if ( ! is_null( WC()->cart ) ) {
// Initialize (do not change)
$product_ids = array();

// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Get product ID and push to array
$product_ids[] = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
}

:

// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Specific product IDs
$specific_product_ids = array( 30, 823, 53, 57 );
// Initialize (do not change)
$product_ids = array();
// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Get product ID
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
// Checks if a value exists in an array
if ( in_array( $product_id, $specific_product_ids ) ) {
// Push to array
$product_ids[] = $product_id;
}
}

注3)has_bought_items_by_phone_number()功能基于WooCommerce应答码

检查用户是否购买了特定产品

最新更新