【问题标题】:On cart items stock issue disable WooCommerce Place order checkout button在购物车商品库存问题上禁用 WooCommerce 下订单结帐按钮
【发布时间】:2021-01-21 21:09:22
【问题描述】:

目前,如果某件商品缺货,结帐页面中会显示一条消息:

抱歉,我们没有足够的“xxx”库存来完成您的订单(2 个可用)。对于给您带来的任何不便,我们深表歉意。

因此,如果出现该消息,那么我想通过简单地删除来禁用页面上的“继续结帐”按钮。

如果没有出现该消息,则显示“继续结帐”按钮。

我显然会创建一个 IF 语句,如果它是真的然后显示,如果不是,不显示等等。

这是我的“继续结帐”按钮代码:

<input type="submit" name="cart_submit" class="checkout-button button alt wc-forward" style="text-transform: capitalize;" value="<?php esc_html_e( 'Proceed to checkout', 'woocommerce' ); ?>" />

那么有没有我可以在 Woocommerce 中调用的方法来查看是否调用了该错误消息?

我尝试扫描整个网页以查找该消息,如果为真,则执行 xyz 但这不起作用(可能是会话)。

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce cart stock


    【解决方案1】:

    基于WC_Cart check_cart_item_stock() method source code,显示您的问题的错误代码,第一个函数将检查购物车商品库存作为条件函数,如果一切正常,则返回 true;如果显示错误并指出存在错误,则返回 false购物车商品的库存问题。

    如果购物车商品出现库存问题,第二个功能将显示禁用的灰色“下订单”按钮。

    function wc_check_cart_item_stock() {
        $product_qty_in_cart      = WC()->cart->get_cart_item_quantities();
        $current_session_order_id = isset( WC()->session->order_awaiting_payment ) ? absint( WC()->session->order_awaiting_payment ) : 0;
    
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $product = $values['data'];
    
            // Check stock based on stock-status.
            if ( ! $product->is_in_stock() ) {
                return false;
            }
    
            // We only need to check products managing stock, with a limited stock qty.
            if ( ! $product->managing_stock() || $product->backorders_allowed() ) {
                continue;
            }
    
            // Check stock based on all items in the cart and consider any held stock within pending orders.
            $held_stock     = wc_get_held_stock_quantity( $product, $current_session_order_id );
            $required_stock = $product_qty_in_cart[ $product->get_stock_managed_by_id() ];
    
            if ( $product->get_stock_quantity() < ( $held_stock + $required_stock ) ) {
                return false;
            }
        }
        return true;
    }
    
    add_filter( 'woocommerce_order_button_html', 'disable_order_button_html' );
    function disable_order_button_html( $button ) {
        if( wc_check_cart_item_stock() ) {
            return $button;
        } else {
            return '<a class="button alt disabled" style="cursor:not-allowed; text-align:center">' .__('Place order', 'woocommerce') . '</a>';
        }
    }
    

    已禁用结帐页面上的灰色“下订单”按钮

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


    如果您想删除结帐“下订单”按钮,请替换:

    return '<a class="button alt disabled" style="cursor:not-allowed; text-align:center">' .__('Place order', 'woocommerce') . '</a>';
    

    与:

    return '';
    

    相关:Customizing checkout "Place Order" button output html

    【讨论】:

      【解决方案2】:

      Woocommerce 具有使用 global $product 具有 product 价值的特殊功能。 您可以使用以下代码访问您的产品库存数量。

      global $product;
      $quantity = $product->get_stock_quantity();
      if($quantity < 1){
        //take decision. 
      }
      

      【讨论】:

      • 谢谢你,这很有帮助。是否可以扫描整个购物车?所以如果有一件商品缺货,那么做 xyz 吗?
      • 要获取购物车和购物车物品,您应该点击此链接作为参考stackoverflow.com/questions/28576667/…
      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 2020-11-03
      • 2018-12-17
      • 2011-04-30
      • 2021-12-16
      • 2019-08-19
      • 1970-01-01
      相关资源
      最近更新 更多