由於 #BloomerArmada 粉絲的 WooCommerce 片段請求不斷來到我的收件箱,現在是發佈非常有用的功能的時候了。
今天,我們將看到如何拒絕向給定的帳單電子郵件地址購買,如果這是一個用户,並且恰好有一個待處理訂單!
PHP 代碼段:如果用户有待處理訂單,拒絕結帳| WooCommerce
/** * @snippet Deny Checkout to User With Pending Orders | WooCommerce * @sourcecode https://businessbloomer.com/?p=55387 * @author Rodolfo Melogli * @testedwith WooCommerce 3.0.5 */ add_action('woocommerce_after_checkout_validation', 'bbloomer_deny_checkout_user_pending_orders'); function bbloomer_deny_checkout_user_pending_orders( $posted ) { global $woocommerce; $checkout_email = $posted['billing_email']; $user = get_user_by( 'email', $checkout_email ); if ( ! empty( $user ) ) { $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $user->ID, 'post_type' => 'shop_order', // WC orders post type 'post_status' => 'wc-pending' // Only orders with status "completed" ) ); foreach ( $customer_orders as $customer_order ) { $count++; } if ( $count > 0 ) { wc_add_notice( 'Sorry, please pay your pending orders first by logging into your account', 'error'); } } }
如何添加此代碼?
1 、您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部 (如果是 CSS 代碼,請添加到子主題的 style.css 文件底部) 修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。
2 、 WordPress 4.9 後改進了主題編輯器,對於 CSS 代碼也可打開網站前台編輯器的 【自定義】,複製代碼添加到自定義 css 中。
此代碼是否可用?
如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用户也希望您的參與。