首页 > 解决方案 > 将订单限制为来自自定义分类的一定数量的产品(每 24 小时,按 IP 地址)

问题描述

我试图让我的客户每 24 小时在我的 Woocommerce Wordpress 网站上从自定义分类中订购 5 种产品。

我创建了一个名为“product_theme”的自定义分类法,其中产品是

“免费”分类是我想要限制的。


我已经找到并调整了这段代码(在Limit woocommerce orders by IP中找到)以满足我的需要,并且它目前在一定程度上可以工作。

但是,如果我完成了一个不在“免费”期限内的订单,我的 IP 限制基本上会被重置,然后我可以下载无限量的免费下载。也许该代码仅适用于该 IP 的最新订单?

我认为这可能与其中有 2 个“if”语句的事实有关。我需要它是 A。如果订单包含“免费”属性中的产品和 B。如果客户的 IP 地址在过去 24 小时内创建了超过 2 个订单。

这是我目前所拥有的...

function my_ip_checker() {

    $last_24_hours_from_ip_results = wc_get_orders(array(
        'date_created'        => '>=' . (time() - 86400), // time in seconds
        'customer_ip_address' => WC_Geolocation::get_ip_address(),
        'paginate'            => true  // adds a total field to the results
    ));

    if ( has_term( 'free', 'product_theme' ) ) { 
        if($last_24_hours_from_ip_results->total > 2) { 

            wc_add_notice('Daily limit exceeded for free downloads. Please come back in 24 hours.', 'error');
        }
    }
}

add_action('woocommerce_checkout_process', 'my_ip_checker', 10, 0);

编辑:尝试添加它,以便如果购物车总数 < 1,则显示错误,但如果大于 1,则用户可以成功结帐。不起作用,因为用户可以随时结帐。

function my_ip_checker() {
    $last_24_hours_from_ip_orders = wc_get_orders(array(
        'date_created'        => '>=' . (time() - 86400), // time in seconds
        'customer_ip_address' => WC_Geolocation::get_ip_address(),
    ));

    //set cart total
    $total = WC()->cart->get_cart();

    // Set variable
    $counter = 0;

    // Loop trough orders
    foreach ($last_24_hours_from_ip_orders as $last_24_hours_from_ip_order ) {
        // Get order
        $order = wc_get_order( $last_24_hours_from_ip_order );

        // Loop trough items
        foreach ($order->get_items() as $item ) {
            // Product id
            $product_id = $item['product_id'];

            // Has term (a certain category in this case)
            if ( has_term( 'free', 'product_theme', $product_id ) ) {
                $counter++;
            }
       }
    }

    if ( $counter >= 5 && sizeof($total) < 1 ) {

         wc_add_notice( __( 'Daily limit exceeded for free downloads. Please come back in 24 hours.' . $counter, 'woocommerce' ), 'error' );

    }
}
add_action('woocommerce_checkout_process', 'my_ip_checker', 10, 0 );

更新:所以上面的代码不起作用,所以我在'if,has_term'语句中添加了一个额外的'if',它正在工作。我还完全删除了 $total 变量。所以现在,使用下面的代码,客户无法结帐 A. 在过去 24 小时内从自定义分类中下载了超过 5 个产品,B. 如果他们的购物车总数为 0。如果购物车总数高于 0,他们可以结帐成功。


function my_ip_checker() {
    $last_24_hours_from_ip_orders = wc_get_orders(array(
        'date_created'        => '>=' . (time() - 86400), // time in seconds
        'customer_ip_address' => WC_Geolocation::get_ip_address(),
    ));


    // Set variable
    $counter = 0;

    // Loop trough orders
    foreach ($last_24_hours_from_ip_orders as $last_24_hours_from_ip_order ) {
        // Get order
        $order = wc_get_order( $last_24_hours_from_ip_order );

        // Loop trough items
        foreach ($order->get_items() as $item ) {
            // Product id
            $product_id = $item['product_id'];

            // Has term (a certain category in this case)
            if ( has_term( 'free', 'product_theme', $product_id ) ) {

            if ( WC()->cart->total == '0' )   {
             $counter++;
            }

         }
       }
    }

    if ( $counter >= 5 ) {

         wc_add_notice( __( 'Daily limit exceeded for free downloads. Please come back in 24 hours.' . $counter, 'woocommerce' ), 'error' );

    }
}
add_action('woocommerce_checkout_process', 'my_ip_checker', 10, 0 );

标签: phpwordpresswoocommercecheckouthook-woocommerce

解决方案


您发布的代码计算了过去 24 小时内来自特定 IP 地址的订单数量。不是订单中的产品数量。

所以我做了一些调整

ps 我不使用自定义分类法,所以我的代码是基于特定类别的。这很容易调整

function my_ip_checker() {
    $last_24_hours_from_ip_orders = wc_get_orders(array(
        'date_created'        => '>=' . (time() - 86400), // time in seconds
        'customer_ip_address' => WC_Geolocation::get_ip_address(),
    ));

    // Set variable
    $counter = 0;

    // Loop trough orders
    foreach ($last_24_hours_from_ip_orders as $last_24_hours_from_ip_order ) {
        // Get order
        $order = wc_get_order( $last_24_hours_from_ip_order );

        // Loop trough items
        foreach ($order->get_items() as $item ) {
            // Product id
            $product_id = $item['product_id'];

            // Has term (a certain category in this case)
            if ( has_term( 'categorie-1', 'product_cat', $product_id ) ) {
                $counter++;
            }
       }
    }

    if ( $counter >= 5 ) {
         wc_add_notice( __( 'My error: ' . $counter, 'woocommerce' ), 'error' );
    }
}
add_action('woocommerce_checkout_process', 'my_ip_checker', 10, 0 );

推荐阅读