WordPress主题WooCommerce如何自定义最低订单量

发布时间:2022-4-08 10:42

本文介绍了Woocommerce为用户角色设置最低订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着来一起学习吧!

WordPress主题WooCommerce如何设定最低订单量,显示一个通知提示客户:

  /**
     * Set a minimum order amount for checkout
     */
    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
 
    function wc_minimum_order_amount() {
        https:// Set this variable to specify a minimum order value
        $minimum = 50;
 
        if ( WC()->cart->total < $minimum ) {
 
            if( is_cart() ) {
 
                wc_print_notice( 
                    sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                        wc_price( WC()->cart->total ), 
                        wc_price( $minimum )
                    ), 'error' 
                );
 
            } else {
 
                wc_add_notice( 
                    sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' , 
                        wc_price( WC()->cart->total ), 
                        wc_price( $minimum )
                    ), 'error' 
                );
 
            }
        }
    }

但未达到最低金额时如何禁用结帐按钮?参考以下:

    add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
    function required_min_cart_subtotal_amount() {
        https:// Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {
 
            https:// HERE Set minimum cart total amount
            $min_total = 250;
 
            https:// Total (before taxes and shipping charges)
            $total = WC()->cart->subtotal;
 
            https:// Add an error notice is cart total is less than the minimum required
            if( $total <= $min_total  ) {
                https:// Display an error message
                wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($min_total) ) . '<strong>', 'error' );
            }
        }
    }

将 PHP 代码放在主题或子主题 functions.php 文件的底部。

WordPress纯代码实现文章相关推荐功能 WordPress

WordPress纯代码实现文章相关推荐功能

这两天准备把的相关推荐功能进行了重写,将原来的文章相关推荐功能做了自我感觉非常优秀的改进,相比用其它 WordPress 相关文章推荐的插件来说,我更喜欢自己来折腾,经过这一番的重写 WordPres...