【问题标题】:One purchase allowed per week in WoocommerceWoocommerce 每周允许购买一次
【发布时间】:2017-04-08 18:45:42
【问题描述】:

我正在创建一个电子商务网站。我可以在我的主题的 functions.php 文件中添加什么,以允许客户仅从商店购买一种产品,然后在 1 周内禁用任何其他购买?

客户可以购买任何产品,但购买后一周内不能再购买任何类型的产品。客户只能在一周后进行任何其他购买。

我只能禁用使用此代码对产品进行的购买:

add_filter( 'woocommerce_add_cart_item_data', 'woo_allow_one_item_only' );

function woo_allow_one_item_only( $cart_item_data ) {

global $woocommerce;
$woocommerce->cart->empty_cart();

// Do nothing with the data and return
return $cart_item_data;

} 

$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key'    => '_customer_user',
'meta_value'  => get_current_user_id(),
'post_type'   => wc_get_order_types(),
'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed' ),

) );

// Order count
$order_count = 1;

if ( count( $customer_orders ) >= $order_count ) {
add_filter( 'woocommerce_is_purchasable', false );
}

【问题讨论】:

    标签: php wordpress woocommerce product orders


    【解决方案1】:

    2018 年 7 月更新 - 增强的更轻更有效的版本代码

    • 简化的产品添加到购物车验证
    • 更好、更轻松地检查用户是否在过去一周内购买了商品(嵌入在可重复使用的条件函数中)
    • 添加了购物车和结帐验证。

    以下代码将允许客户仅将一件商品添加到购物车,并且将允许每周仅购买一次,并在需要时显示错误通知,避免任何被禁止的购买。

    代码:

    // Utility conditional function (Check if user has purchased in the passed week)
    function has_week_purshases( $user_id = 0 ){
        global $wpdb;
    
        $customer_id = $user_id > 0 ? $user_id : get_current_user_id();
    
        $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
            WHERE p.post_type LIKE 'shop_order'
            AND pm.meta_key LIKE '_customer_user'
            AND pm.meta_value = $customer_id
            AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400 * 7))
        " );
    
        return $count > 0 ? true : false;
    }
    
    // product add to cart validation
    add_filter( 'woocommerce_add_to_cart_validation', 'conditionally_allowing_product_added_to_cart', 10, 3 );
    function conditionally_allowing_product_added_to_cart( $passed, $product_id, $quantity) {
        // If cart is not empty, customer will not be allowed and a notice will be displayed
        if ( ! WC()->cart->is_empty() ){
            // Display an error notice if there is already an item in cart
            wc_add_notice( __("You can only add an item to cart"), 'error' );
            $passed = false; // Not Allowed
        } elseif ( is_user_logged_in() && has_week_purshases() ) {
            // Display an error notice when customer is not allowed yet
            wc_add_notice( __("You are not allowed yet to add any product in cart"), 'error' );
            $passed = false; // Not Allowed
        }
        return $passed;
    }
    
    // Cart and checkout validation
    add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
    add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
    function conditionally_allowing_checkout() {
        if ( sizeof(WC()->cart->get_cart()) > 1 ) {
            // Display an error notice if there is more than 1 item in cart
            wc_add_notice( _("You can only have only one item in cart"), 'error' );
        } elseif ( is_user_logged_in() && has_week_purshases() ) {
            // Display an error notice when customer is not allowed yet
            wc_add_notice( __("You are not allowed yet to make purchases"), 'error' );
        }
    }
    

    代码在您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 我在一个新用户身上尝试过,但现在我什至无法进行第一次购买。我在第一次购买时收到错误消息“您还不能在购物车中添加任何产品”。请帮忙。谢谢。
    • 我再次测试了它,但我仍然无法进行第一次购买。请帮忙。感谢您的快速回复!
    • @Abhi 我已经对代码进行了重大更新,以供参考……
    猜你喜欢
    • 2019-09-13
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多