Php 如果重量大于或等于224kg,则在Woocommerce结账时显示通知

Php 如果重量大于或等于224kg,则在Woocommerce结账时显示通知,php,wordpress,function,woocommerce,checkout,Php,Wordpress,Function,Woocommerce,Checkout,我已经看到了一些类似的答案,但不是我所需要的。基本上,我只想显示一个通知显示在结帐时,总订单重量为225kg或以上 根据答案代码,以下是我的修改代码: // Display a custom shipping message when cart weight exeeds a limit add_filter( 'woocommerce_before_cart', 'display_total_weight_notice' ); add_filter( 'woocommerce_befo

我已经看到了一些类似的答案,但不是我所需要的。基本上,我只想显示一个通知显示在结帐时,总订单重量为225kg或以上

根据答案代码,以下是我的修改代码:

    // Display a custom shipping message when cart weight exeeds a limit
add_filter( 'woocommerce_before_cart', 'display_total_weight_notice' );
add_filter( 'woocommerce_before_checkout_form', 'display_total_weight_notice' );
function display_total_weight_notice( $message ) {
    // DEFINE the allowed weight limit
    $cart_total_weight = WC()->cart->get_cart_contents_weight();

    if( cart_total_weight >= 225 ) :

    wc_print_notice( sprintf(
        __( 'Your order has a total weight of %s. This will be shipped via Pallet Delivery, by continuing you agree to the following:<br><br> 
        > Ground/Road around the property will be flat, level and solid. <br> 
        > Standard Delivery will take place between 9am-5pm and will require a signature. You can advise a safe place to leave your order, however this is at the driver’s discretion to do so, we hold no responsibility if left. <br> 
        > Where a delivery cannot be made due to access, a re-delivery can be made at your request for a charge of £45.00 per pallet on the following day or when arranged. <br> 
        > In any cases of Damages/Shortages you will note this on the delivery note when you Sign for it, and then contact us right away. No refunds/replacements will be given if damages are not noted at delivery. <br> 
        > Pallet Orders cancelled en route, or returned due to ground Conditions/Access Restrictions will incur a return fee of £45 per pallet (subject to location), deducted from any refunds given. <br> 
       >  Orders over 1000kg will require your own forklift. 

' ),
        '<strong>' . wc_format_weight($cart_total_weight) . '</strong>',
    ),'notice' );

    endif;
}
//当购物车重量超过限制时显示自定义装运消息
添加过滤器(“购物车前的woocommerce”、“显示总重量通知”);
添加过滤器('woocommerce'u before'u checkout'u form','display'u total'u weight'u notice');
功能显示\总重量\通知($message){
//定义允许的重量限制
$cart_total_weight=WC()->cart->get_cart_contents_weight();
如果(大车总重量>=225):
wc_打印通知(sprintf)(
__('您的订单总重量为%s。如果您继续同意以下内容,将通过货盘交货方式发货:

>物业周围的地面/道路应平坦、平坦、坚实。
>标准交付时间为上午9点至下午5点,需要签名。您可以建议一个安全的地方离开订单,但这由驾驶员自行决定,如果离开,我们不承担任何责任。
>如果由于进入而无法交货,可根据您的要求在第二天或安排后重新交货,每托盘收费45.00英镑。
>在任何损坏/短缺的情况下,您将在签收时在送货单上注明,然后立即与我们联系。如果在交货时未注明损坏,则不会退款/更换。
>在途中取消的托盘订单,或由于地面条件/访问限制而退回的托盘订单,将产生每个托盘45英镑的退回费(取决于位置),从任何退款中扣除。
>超过1000公斤的订单将需要您自己的叉车。 ' ), “”.wc\u格式\u重量($cart\u总重量)。”, )"通知";; endif; }

但它对我不起作用。

我刚刚测试了您的代码,它在购物车页面上正确地显示了一条消息

您的代码中有一个输入错误,您缺少变量前面的
$

if( $cart_total_weight >= 225 ) :
当设置至少一个产品的重量时,我从
$cart\u total\u weight=WC()->cart->get\u cart\u contents\u weight()获取正确的总重量。如果购物车中没有产品定义为重量,则正确返回0

如果您没有编辑购物车模板,它应该可以工作。如果您编辑了它,请检查
do\u action()
是否仍在此处

在这里,我将限制编辑为20公斤以显示信息,并使用25公斤的产品来匹配标准。

// Display a custom shipping message when cart weight exeeds a limit
add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight_mood' );
function checkout_required_min_weight_mood () {
    
    // only on the cart and check out pages
    if( ! ( is_cart() || is_checkout() ) ) return;

    // Get the Cart's content total weight
    $total_weight = WC()->cart->get_cart_contents_weight();

    // here the minimum weight
    $minimum_weight = 225; // 225 kg

    // If the total weight is less than the minimum, we avoid payment and display an error notice
    if( $total_weight >= $minimum_weight  ) {
        // Visualizza un avviso di errore dinamico
        wc_add_notice( sprintf(
            'MESSAGE TO DISPLAY HERE',
            wc_format_weight($total_weight),
            wc_format_weight($minimum_weight)
        ), 'notice' );
    }
}

你的代码有什么问题?没有显示任何内容?它是否至少在结帐或购物车页面上执行?购物车页面上不会显示任何通知,即使在添加总计超过225kgI的项目时,如果您在if语句中的变量
$cart\u total\u weight
上添加缺少的
$
,则代码有效…您的答案代码将从此答案中复制(第二应答码):