!
也想出现在这里? 联系我们
广告位

给WooCommerce的运费(Shipping Method)增加自定义描述字段

如果一个 WooCommerce 网站存在多种不同的运费(Shipping Method),则可能有必要让用户在运费选择的模块中了解不同运费的区别。你可以解释运输的时长,或者解释价格的构成等,总之就是提供更多信息帮助用户做出最好的选择。比如在这里,我就对每个运费添加了时长的解释:
给WooCommerce的运费(Shipping Method)增加自定义描述字段
一个便捷的办法是通过以下代码实现的:

  1. add_filter('woocommerce_cart_shipping_method_full_label', 'brain1981_custom_shipping_method_label', 10, 2);
  2. function brain1981_custom_shipping_method_label( $label, $method ){
  3. $txt = "";
  4. if( $method->id=="flat_rate:1" || $method->id=="free_shipping:4" ){
  5. $txt = 'In 15 Business days';
  6. }else if( $method->id=="flat_rate:2" || $method->id=="free_shipping:3" ){
  7. $txt = '7-10 Business days';
  8. }
  9. return $label . '<br /><small>' . $txt . '</small>';
  10. }

需注意的是,WooCommerce 的运费 ID 的格式都是以这样的形式呈现的:

  1. flat_rate:1
  2. free_shipping:2

以上这段代码就是事先辨认出这些运费 ID,通过 woocommerce_cart_shipping_method_full_label 这个钩子在运费的标题后面增加一小段描述。如果你的运费数量不多且比较固定,这段代码就足够用了。

那么如果一个网站有很多种运费,并且经常会修改运费种类,上面这种 hard codding 的写法就会变得很臃肿且不易维护了。我们就需要给每个运费添加一个自定义的描述字段,实现后台管理描述,方便运维人员自己去修改运费设置

首先实现给后台添加自定义字段:

  1. add_action( 'woocommerce_init', 'brain1981_shipping_instance_form_fields_filters' );
  2. function brain1981_shipping_instance_form_add_extra_fields( $settings ) {
  3. $settings['shipping_extra_field_description'] = array(
  4. 'title' => esc_HTML__( 'Description', 'woocommerce' ),
  5. 'type' => 'textarea',
  6. 'placeholder' => esc_html__( 'Your shipping method description', 'woocommerce' ),
  7. 'description' => '',
  8. );
  9. return $settings;
  10. }
  11. function brain1981_shipping_instance_form_fields_filters() {
  12. $shipping_methods = WC()->shipping->get_shipping_methods();
  13. foreach ( $shipping_methods as $shipping_method ) {
  14. add_filter( 'woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'brain1981_shipping_instance_form_add_extra_fields' );
  15. }
  16. }

以上代码实现了这样的功能:

接下来是在开头第一张图的位置,调用出这个字段的内容:

  1. add_filter('woocommerce_cart_shipping_method_full_label', 'brain1981_custom_shipping_method_label', 10, 2);
  2. function brain1981_custom_shipping_method_label( $label, $method ){
  3. $rate_id = $method->id; // The Method rate ID (Method Id + ':' + Instance ID)
  4. $rate_id = str_replace(':', '_', $rate_id);
  5. $txt = get_option('woocommerce_'.$rate_id.'_settings')['shipping_extra_field_description'];
  6. return $label . '<br /><small>' . $txt . '</small>';
  7. }

从以上代码中可以看出,WooCommerce 的 Shipping Method 并不是像自定义文章类型那样的存储方式,而是存储于 option 表的,其键名是这种形式:

  1. woocommerce_flat_rate_1_settings //对应flat_rate:1
  2. woocommerce_free_shipping_2_settings //对应free_shipping:2

键值是一串 JSON 字符串,自定义字段就存于其中。知道了这个规律,就可以通过 get_option 方法把刚添加的字段调出来了。

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress 创建自定义文章类型设置固定链接

2022-5-24 13:55:42

WordPress教程

WordPress 最简单的手动输入页号跳转翻页的方法

2022-5-25 1:56:22

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索