当前位置:首页文章笔记建站教程给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 方法把刚添加的字段调出来了。

温馨提示:

文章标题:给WooCommerce的运费(Shipping Method)增加自定义描述字段

文章链接:https://www.wuyanshuo.cn/699.html

更新时间:2022年04月08日

本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读无言说网络服务协议

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

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

2022-4-7 23:28:40

建站教程

WordPress后台开发,无插件增加一键复制文章页面功能

2022-4-8 11:02:33

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'