我正在try 在 checkout 页面上强制填写订单/发货备注字段,以用于特定的发货方式.

在这个帖子后面:Make Order notes required if shipping method is local pickup in Woocommerce,我使用以下代码:

add_action( 'woocommerce_checkout_process', 'danskefragtmand_order_comment_validation', 20 );
function danskefragtmand_order_comment_validation() {
    $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping = explode(':', $chosen_shipping);
  
    if ( $chosen_shipping[0] == 'shipmondo:26' && empty($_POST['order_comments']) ){
        wc_add_notice( __( "Please tell the courier where he can leave your package in case you are not at home during time of delivery.", "woocommerce" ), 'error' );
    }
}

如果我使用标准的WooCommerce发货术语,如local_pickupfree_shipping,并且如果您将我自己的发货术语shipmondo:26更改为shipmondo,则上面的代码工作得很好,但随后通知将显示为所有其他shipmondo种发货方式.我正试图锁定特定的ID 26.

任何建议都会受到重视.

其他有用的帖子:Disable only specific flat rate shipping method when free shipping is available in WooCommerce

推荐答案

您需要在代码中替换该行:

if ( $chosen_shipping[0] == 'shipmondo:26' && empty($_POST['order_comments']) ){

有:

if ( $chosen_shipping === 'shipmondo:26' && empty($_POST['order_comments']) ){

现在,它应该奏效了.

To make the code even better, replace your code 有:

add_action( 'woocommerce_checkout_process', 'danskefragtmand_order_comment_validation', 20 );
function danskefragtmand_order_comment_validation() {
    $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');

    if ( in_array('shipmondo:26', $chosen_shipping_methods) && empty($_POST['order_comments']) ){
        wc_add_notice( __( "Please tell the courier where he can leave your package in case you are not at home during time of delivery.", "woocommerce" ), 'error' );
    }
}

Php相关问答推荐

当我们使用PHPUnit时,控制台中的--uses param到底起了什么作用以及如何使用?

产品ACF值在WooCommerce我的账户订单的附加列中为空

PHP Perl正则表达式—URL前面没有等号和可能的单引号或双引号

Laravel集合块()方法

如何将WooCommerce产品属性术语名称显示为链接术语?

我必须对所有的SQL查询使用预准备语句吗?

模式 bootstrap 未显示

拉威尔10有许多通过获取所有祖父母数据的子元素S图像

为什么PHP PDO忽略NOT NULL约束?

PHP 数组中第一级 node 到 XML 的唯一名称

在 PHP MySQL 中自动生成账单编号

服务器升级到新的mysql版本8.0.34后查询错误

如何将不同的 Woocommerce 费用合并为一个名称?

多个产品类别 Select 自定义 WooCommerce 插件设置页面

计算添加到购物车的点击次数并将其显示在 WooCommerce 管理产品列表中

跟踪通过 WooCommerce 分层定价表插件进行的购买

php var_dump(1) 返回字符串(4)

从 CSV 文件 seeder 数据库时的额外行

Laravel 响应 html 默认页面忽略控制器

一个php应用程序可以实例化多个RNG吗