初回限定でリーズナブルなお値段の商品があります。「初回限定」と注意書きをしても、複数回注文いただくことが時々ありました。
これをなんとか防ぎたい!
WordPressの公式プラグインにはありませんが、以下のリンクに便利なプラグインが公開されていました。
https://github.com/Craftpeak/woocommerce-prevent-repeat-purchases
このプラグインを使うと商品ページに対象とするか否かのチェックボックスが追加されます。
また、アカウントを持たないゲスト購入者もチェックされます。
ダウンロードして、プラグインページからアップロード、有効化すればOKです。
欠点としては、バリエーションのある商品には対応していません。
また、翻訳ファイルがありません。当店は翻訳ファイルを作りましたが、ごくわずかですので、ソースファイルを開いて該当部分を書き換えてもいいでしょう。
プラグインはイヤだという場合は、上のプラグインとは動作が異なりますが、下記のコードをfunction.php に追加すると良いでしょう
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('メールアドレス %s は、ユーザ名 %s として登録済みです. ログインしてください.', $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'] ) ) {
if(in_array($cart_item['product_id'], array(61,187) ) ) {
wc_add_notice( sprintf( ' %s は、過去にご購入いただいています。 購入いただけるのは1回限りです。', $cart_item['data']->get_name() ), 'error' );
}
}
}
}
// 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;
}
