亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果之前在 WooCommerce 上購買過產品,請避免訪客購買

如果之前在 WooCommerce 上購買過產品,請避免訪客購買

PHP
侃侃無極 2023-08-26 17:30:01
由于各種原因,網上商店需要允許訪客訂單,但如果電子郵件已注冊,則將數據保存到現有用戶。但是,一個產品(父變量產品)不應該購買兩次。在這種情況下,客戶應該返回結帳頁面并收到該產品已購買的通知。到目前為止,我有以下代碼(不完整):add_action('woocommerce_checkout_process', function () {    if (!is_user_logged_in() && $_POST['billing_email'] && $_POST['createaccount'] == 1) {        $user = get_user_by('email', $_POST['billing_email']);        $order = wc_get_order( $order_id );        $items = $order->get_items();        foreach ( $items as $item ) {    $product_id = $item->get_product_id();        if (!empty($user)){            if (wc_customer_bought_product($user->user_email,$user->ID,$product_id))            return false;        wp_set_current_user($user->ID);        }        if (!empty($user)) {        if (wc_customer_bought_product($user->user_email,$user->ID,$product_id))             return true;        exit;    }    else{        ///guest, continue    }}}});但這只給出了內部服務器錯誤。任何人?
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

首先,您可以使用wc_customer_bought_product()中的以下增強條件函數(基于與 相同的源代碼?,而不是使用。此功能將處理您的可變產品情況。?wc_customer_bought_product()

// Conditional function( stackOverFlow answer: https://stackoverflow.com/a/46217461/3730754 )

function has_bought_items( $user_var = 0,? $product_ids = 0 ) {

? ? global $wpdb;

? ??

? ? // Based on user ID (registered users)

? ? if ( is_numeric( $user_var) ) {?

? ? ? ? $meta_key? ? ?= '_customer_user';

? ? ? ? $meta_value? ?= $user_var == 0 ? (int) get_current_user_id() : (int) $user_var;

? ? }?

? ? // Based on billing email (Guest users)

? ? else {?

? ? ? ? $meta_key? ? ?= '_billing_email';

? ? ? ? $meta_value? ?= sanitize_email( $user_var );

? ? }

? ??

? ? $paid_statuses? ? = array_map( 'esc_sql', wc_get_is_paid_statuses() );

? ? $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-" . implode( "','wc-", $paid_statuses ) . "' )

? ? ? ? AND pm.meta_key = '$meta_key'

? ? ? ? AND pm.meta_value = '$meta_value'

? ? ? ? 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;

}

您還無法獲取訂單或訂單商品...相反,您將檢查購物車商品。


然后這是您重新訪問的代碼,用于按照要求處理所有類型的客戶和可變產品:


add_action('woocommerce_checkout_process', 'wc_action_checkout_process' );

function wc_action_checkout_process() {

? ? $user_id = get_current_user_id();


? ? // 1. - For logged in users

? ? if( $user_id > 0 ) {

? ? ? ? $user_var = $user_id;

? ? }

? ? // 2. - For others

? ? else {

? ? ? ? if ( isset($_POST['billing_email']) && ! empty($_POST['billing_email']) ) {

? ? ? ? ? ? $email = sanitize_email( $_POST['billing_email'] );

? ? ? ? ? ? $user? = get_user_by( 'email', $email );


? ? ? ? ? ? // 2.a - When a user exist for the billing email return an error notice

? ? ? ? ? ? if ( is_a($user, 'WP_User') ) {

? ? ? ? ? ? ? ? wc_add_notice( sprintf( __('The email "%s" is already registered for "%s" user name. Please login.'), $email, $user->user_login ), 'error' );

? ? ? ? ? ? }

? ? ? ? ? ? // 2.b - User doesn't exits (real "guest")

? ? ? ? ? ? else {

? ? ? ? ? ? ? ? $user_var = $email;

? ? ? ? ? ? }

? ? ? ? }

? ? }


? ? // Loop through cart items to check products

? ? foreach ( WC()->cart->get_cart() as $cart_item ) {

? ? ? ? if ( has_bought_items( $user_var,? $cart_item['product_id'] ) ) {

? ? ? ? ? ? wc_add_notice( sprintf( __('You have already purchased "%s" product before. You can only purchase it once.'), $cart_item['data']->get_name() ), 'error' );

? ? ? ? }

? ? }

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。




查看完整回答
反對 回復 2023-08-26
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號