根据产品类别

时间:2017-07-15 11:07:41

标签: php wordpress woocommerce cart categories

我正在使用WooCommerce创建在线食品配送网站,需要创建一个" Make it a Meal"为客户输入加售类型。

我创造了一种名为"使其成为膳食的单品"用户可以选择饮料和他们选择的一面(使用变化)。然而,我需要的是每个主餐只能购买其中一种产品。

示例:

  • 产品"主餐"类别已添加到购物车
  • 然后,用户可以添加"将其作为膳食"产品到购物车
  • 用户无法添加第二个"使其成为一顿饭"产品,除非他们有来自"膳食的第二种产品"类别,即每个产品只有一个
  • 如果"主餐"将产品从购物车中取出,然后"将其制成膳食"也被删除了。

之前有没有人实现过这种功能?我尝试了各种插件,但目前没有运气。

1 个答案:

答案 0 :(得分:0)

<强>更新

可以先检查添加到购物车的事件(可选择显示错误通知):

add_filter( 'woocommerce_add_to_cart_validation', 'checking_products_added_to_cart', 10, 3 );
function checking_products_added_to_cart( $passed, $product_id, $quantity) {

    $cat_add = 'Make it a Meal';
    $cat_src = 'Main Meal';

    if(has_term( $cat_add, 'product_cat', $product_id )):

        $main_meal_count = 0;
        $make_it_a_meal_count = 0;

        foreach (WC()->cart->get_cart() as $cart_item ){

            // Counting 'Main Meal' products in cart (with quantity)
            if( has_term( $cat_src, 'product_cat', $cart_item['product_id'] ))
                $main_meal_count += $cart_item['quantity'];

            // Counting 'Make it a Meal' products in cart (with quantity)
            if( has_term( $cat_add, 'product_cat', $cart_item['product_id'] ))
                $make_it_a_meal_count += $cart_item['quantity'];
        }

        if( $main_meal_count < ($make_it_a_meal_count + $quantity) ) {
            $passed = false;

            // Displaying a message (optionnal)
            wc_add_notice( 'my custom error message…', 'error' );
        }
    endif;

    return $passed;
}

然后,您还需要检查购物车数量的更改或购物车物品的移除情况:

add_action( 'woocommerce_calculate_totals', 'check_removed_cart_items', 10, 1);
function check_removed_cart_items( $cart_object ) {

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

    $cat_add = 'Make it a Meal';
    $cat_src = 'Main Meal';

    $main_meal_count = 0;
    $make_it_a_meal_count = 0;

    // First loop: Counting cart items 'Main Meal' and 'Make it a Meal'
    foreach ($cart_object->get_cart() as $item_values ){

        // Counting 'Main Meal' products in cart (with quantity)
        if( has_term( $cat_src, 'product_cat', $item_values['product_id'] ))
            $main_meal_count += $item_values['quantity'];

        // Counting 'Make it a Meal' products in cart (with quantity)
        if( has_term( $cat_add, 'product_cat', $item_values['product_id'] )){
            $make_it_a_meal_count += $item_values['quantity'];
        }
    }

    $difference = intval($make_it_a_meal_count - $main_meal_count);

        echo '<p>$main_meal_count is '.$main_meal_count.'</p>';
        echo '<p>$make_it_a_meal_count is '.$make_it_a_meal_count.'</p>';
        echo '<p>$difference is '.$difference.'</p>';

    if( $main_meal_count < $make_it_a_meal_count ) {

        echo '<p>case1</p>';


        // Second Loop: Make necessary actions
        foreach ($cart_object->get_cart() as $cart_item_key => $cart_item ){

            // Targeting 'Make it a Meal'
            if( has_term( $cat_add, 'product_cat', $cart_item['product_id'] )){

                $item_qty = intval( $cart_item['quantity'] );

                if( $item_qty == 1 && $difference == 1 ){
                    $cart_object->remove_cart_item($cart_item_key);
                    break;
                } else {
                    if( $item_qty > 1 && $difference <= $item_qty ){
                        $cart_object->set_quantity( $cart_item_key, ($item_qty - $difference) );
                        break;
                    } else {
                        $cart_object->remove_cart_item($cart_item_key);
                    }
                }
            }
        }
    }
}

使用这2个钩子函数,您可以获得完整的解决方案。

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码经过测试并有效。