如果订单少于1kg,我将使用以下代码在购物车页面上添加通知.

// Add notice on cart when order is less than 1kg
add_action('woocommerce_check_cart_items','check_cart_minweight');
 
function check_cart_minweight(){
    global $woocommerce;

    $minweight = $woocommerce->cart->cart_contents_weight;

    if( $minweight < 1 ){ 
        wc_add_notice( sprintf( __( 'You have selected %sKg, add more items, orders over 1kg get free shipping', 'woocommerce' ), $minweight ), 'success' );
    }
}

问题是,如果客户体重没有超过1公斤(很多时候他们没有),并且继续结帐,那么结帐时通知就不清楚了.有什么建议可以防止这种情况发生吗?

推荐答案

有几种 Select .例如,您可以确保代码不会在签出页面上执行:

function action_woocommerce_check_cart_items() {
    // NOT for checkout page
    if ( is_checkout() ) return;

    // Get total weight
    $total_weight = WC()->cart->get_cart_contents_weight();

    // Less
    if ( $total_weight < 1 ) { 
        wc_add_notice( sprintf( __( 'You have selected %sKg, add more items, orders over 1kg get free shipping', 'woocommerce' ), $total_weight ), 'notice' );
    }
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10 );

另一个选项是确保在满足条件之前无法访问签出页面:

function action_woocommerce_check_cart_items() {
    // Get total weight
    $total_weight = WC()->cart->get_cart_contents_weight();

    // Less
    if ( $total_weight < 1 ) { 
        wc_add_notice( sprintf( __( 'You have selected %sKg, add more items, orders over 1kg get free shipping', 'woocommerce' ), $total_weight ), 'error' );

        // Remove proceed to checkout button
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
    }
}   
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10 );

Php相关问答推荐

添加自定义Metabox到WooCommerce管理订单,启用HPOS

有条件的运费率基于购物车小计与WooPayments货币转换

如何在WordPress中为特定自定义帖子类型自定义URL struct

Symfony/Panther Web抓取不适用于登录后的内容(云功能)

如何限制WordPress自定义分类术语页面中的术语数量

Laravel关系-为用户获取属于组织的所有团队

在WooCommerce管理中 for each 运输方法设置添加自定义字段

Laravel 10中的Validator类

将购物车按钮重定向到Checkout-Wooccommerce

ANTLR4-lexer如何消耗更多的 token 并停止现有规则?

在 WooCommerce 订阅续订订单中设置送货方式

使用帖子 ID 更新 wp 帖子

API 平台 - UI 服务器端点定义

如果 Select 的县不是store 所在的县,如何隐藏本地自提?

使用自定义插件中的 WooCommerce 模板发送自定义邮箱

如何更新 Laravel Eloquent 列强制转换为集合

在 DateTime 或 DateTimeImmutable 对象上调用modify() 时是否必须使用空格?

使用 wp_footer 挂钩添加一些 CSS 不起作用

如何在 laravel 中的同一个 slug 上显示不同的内容?

如何在 Laravel 中保存多对多