php - 有条件地删除 Woocommerce 中的特定购物车商品

标签 php wordpress woocommerce cart custom-fields

我正在开发一个 WooCommerce 插件,用于从日历(如每周列表)中订购特定日期的食物。

订单有时间限制,每天14:00前均可订购明天的订单。它适用于您可以订购商品的页面(如果达到限制,添加到购物车按钮将不会显示),但如果您的购物车中已经有商品将不可用,您可以在 14:00 之后订购。

我在某处读到,此类操作有一个操作钩子(Hook),所以我试图自己弄清楚,但被卡住了。

简单介绍一下它的工作原理。当您单击“添加到购物车”按钮时,将使用自定义元标记在购物车中创建该项目,其中包含我需要的以下格式的信息:MenuCode-Year-WeekOfYear-DayOfWeek。

该函数的名称是wc_minimum_order_amount,因为它还包含一段代码,用于在订单金额小于指定金额时显示 wc_notice。这部分已从下面的代码中删除,因为它与问题无关,并且它有效。

这是我使用的钩子(Hook)和代码:

add_action('woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action('woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
    $cart = WC()->cart->get_cart();
    foreach($cart as $item => $values) {
        $co   = explode('-', $values['_custom_options']);
        $year = $co[1];
        $week = $co[2];
        $day  = $co[3];
        $hour = date('H');

        if($year < date('Y')) {
            WC()->cart->remove_cart_item($item);
        }
        else if($year == date('Y') && $week < date('W')) {
            WC()->cart->remove_cart_item($item);
        }
        else if($year == date('Y') && $week == date('W') && $day < date('w')) {
            WC()->cart->remove_cart_item($item);
        }
        else if($year == date('Y') && $week == date('W') && $day == date('w') && $hour >= 14) {
            WC()->cart->remove_cart_item($item);
        }
    }
}

但它并没有像我预期的那样从购物车中删除商品。

我怎样才能让它发挥作用?

谢谢

最佳答案

这里我只使用 woocommerce_before_calculate_totals 操作 Hook ,它应该在任何地方都有效。

我对您的代码做了一些小更改:

add_action('woocommerce_before_calculate_totals', 'wc_minimum_order_amount', 10, 1 );
function wc_minimum_order_amount( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Set here the time offset (in hours)
    $hours_offset = 14;

    $now_year      = date('Y');
    $now_year_week = date('W');
    $now_day_week  = date('w');
    $now_hour      = date('H');

    foreach($cart->get_cart() as $cart_item_key => $cart_item ) {
       if( isset($cart_item['_custom_options']) && ! empty($cart_item['_custom_options']) ){
            $date = explode('-', $cart_item['_custom_options']);
            $date_year      = $date[1];
            $date_year_week = $date[2];
            $date_day_week  = $date[3];


            if( $date_year < $now_year ) {
                $remove_cart_item = true;
                wc_add_notice( __( 'CASE1', 'woocommerce' ), 'notice' ); // Just for testing (to be removed)
            }
            elseif( $date_year == $now_year && $date_year_week < $now_year_week ) {
                $remove_cart_item = true;
                wc_add_notice( __( 'CASE2', 'woocommerce' ), 'notice' ); // Just for testing (to be removed)
            }
            elseif( $date_year == $now_year && $date_year_week == $now_year_week && $date_day_week < $now_day_week ) {
                $remove_cart_item = true;
                wc_add_notice( __( 'CASE3', 'woocommerce' ), 'notice' ); // Just for testing (to be removed)
            }
            elseif( $date_year == $now_year && $date_year_week == $now_year_week && $date_day_week == $now_day_week && $now_hour >= $hours_offset ) {
                $remove_cart_item = true;
                wc_add_notice( __( 'CASE4', 'woocommerce' ), 'notice' ); // Just for testing (to be removed)
            }
            else {
                $remove_cart_item = false;
                wc_add_notice( __( 'CASE0', 'woocommerce' ), 'notice' ); // Just for testing (to be removed)
            }

            if($remove_cart_item) {
                $cart->remove_cart_item($cart_item_key);
            }
        }
    }
}

代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。

我无法测试它,因为这非常具体,但这应该可行。

关于php - 有条件地删除 Woocommerce 中的特定购物车商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43201567/

相关文章:

php - 整齐地加载数十个数据 fixture

image - 如何在 Wordpress 中添加特色图片说明

php - 如何在 Woocommerce 3.3.4 上删除添加到购物车

javascript - 为每种类型的用户提供不同内容的网站(关于安全性)

php - 创建文件的文件所有权

php - 使用 RS Form Pro 获取页面 &lt;title&gt; - Joomla

php - 在 WooCommerce 3 中获取产品属性详细信息

javascript - 根据下拉选择刷新 div 容器的全部内容

php - Woocommerce 仅在前端为登录用户提供价格折扣

php - 在 Woocommerce 中添加自定义字段作为订单备注