避免在Woocommerce中结帐混合的缺货和常规商品 [英] Avoid checkout for mixed backorder and normal items in Woocommerce

查看:115
本文介绍了避免在Woocommerce中结帐混合的缺货和常规商品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在库存项目中混有延期交货项目而禁用结帐. 到目前为止,如果购物车中有混合物品,则代码会显示消息,但它们仍然可以签出订单.

Is it possible to disable checkout if there is backorder item mixed with in stock items. The code so far is displaying message if there is mixed items in the cart, but they still can checkout the order.

我们使用的是Preorder插件,在设置中,不能在购物车中混合使用preorder和现有商品.以下是插件的设置.

We are using Preorder plugin and on the settings, the preorder and onhand cant be mixed in cart. Below are settings of plugin.

防止混合产品:如果启用此选项,则购物车不能同时包含预购产品和常规产品.(已启用)但仅当购物车中没有商品时,该购物车才有效.购物车.

Prevent mixing products If you enable this option, the cart cannot contain Pre-Order products and regular products at the same time.(enabled) But it only work if there is no items present in the cart.

允许销售缺货产品.通过启用此选项,可以购买无库存的预购产品. (已启用和允许的缺货订购)一旦库存为零,所有项目都可以预订.

Allow sales of out of stock products By enabling this option, Pre-Order products with no stock can be purchased. (enabled and allowed backorder) All items can be Preorder once stocks become zero.

问题是,如果购物车中已经有商品,他们可以签出预购商品和普通商品.请检查下面的示例

我将产品A (5个库存)和 B (10个库存)放入购物车,但我不想立即结帐.

I put Product A(5 stocks) and B(10 stocks) in the cart but I dont want to checkout right away.

然后有人购买了产品A ,库存变成 0 (并且产品A 变成预购商品)

Then some one purchased the Product A and stocks become 0 (and Product A turn to preorder)

但是,如果我继续结帐产品A (0个库存和预购商品)和 B (10个库存),所以它已经混入购物车中了,我可以继续结帐,因为设置中允许补货.

But if I proceed to checkout Product A(0 stocks and preorder) and B(10 stocks)so its already mixed in cart and I can proceed to checkout because backorder is allowed in settings.

是否可以自动删除购物车中的产品A 或禁用结帐?

Is it possible to automatically delete the Product A in cart or disable checkout?

add_action( 'woocommerce_review_order_before_payment', 'es_checkout_add_cart_notice' );

function es_checkout_add_cart_notice() {

    $message = "You have a PREORDER item/s in your cart! Do not mix it if you're ordering on-hand item/s or IGNORE this message if you are ordering all pre-order item/s.";

    if ( es_check_cart_has_backorder_product() ) 
        wc_add_notice( $message, 'error' );

}

function es_check_cart_has_backorder_product() {
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $cart_product =  wc_get_product( $values['data']->get_id() );
        if( $cart_product->is_on_backorder() )
            return true;
    }

    return false;
}

推荐答案

尝试以下操作,它将真正检查混合项目并抛出错误消息,避免:

Try the following, that will really check for mixed items and will throw an error message avoiding:

  • 进行结帐"(在购物车页面中)
  • 下订单(结帐页面)

代码:

// Display a custom notice when mixed items (backorder items and normal) avoiding checkout and "proceed to checkout" too
add_action( 'woocommerce_checkout_process', 'display_custom_error_notice' );
add_action( 'woocommerce_check_cart_items', 'display_custom_error_notice' );
function display_custom_error_notice() {
    $message = __("You have a PREORDER item/s mixed with normal items. They can not be mixed.", "woocommerce");

    if ( has_mixed_products() )
        wc_add_notice( $message, 'error' );

}

// Utility function checking for mixed items (backorder items and normal)
function has_mixed_products() {
    $on_backorder = $normal = false;

    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->is_on_backorder() )
            $on_backorder = true;
        else $normal = true;
    }
    return $on_backorder && $normal ? true : false;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

在购物车页面上:

在结帐页面上:

现在也可以从发出通知的购物车中删除混合物品了……

Now is also possible to remove mixed items from cart throwing a notice…

对于woocommerce来说,几乎一切皆有可能,这取决于您的技能和花费的时间.

With woocommerce mostly everything is possible, depending on your skills and on time to spend.

这篇关于避免在Woocommerce中结帐混合的缺货和常规商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆