WooCommerce教程:根据产品分类显示预计交货时间,并处理库存状态

聖光之護
发布: 2025-09-07 22:30:30
原创
193人浏览过

woocommerce教程:根据产品分类显示预计交货时间,并处理库存状态

本文旨在帮助WooCommerce开发者根据产品所属的特定分类(taxonomy)来显示预计交货时间,并提供代码示例,同时涵盖了如何根据当前时间动态调整交货日期、自定义显示信息以及在产品缺货时隐藏交货提示的方法。通过学习本文,你将能够灵活地控制WooCommerce产品页面的交货信息展示,提升用户体验。

根据产品分类显示预计交货时间

在WooCommerce中,经常需要根据产品的不同属性来展示不同的信息。例如,针对特定分类的产品,显示预计的交货时间。以下代码展示了如何根据产品所属的自定义分类(taxonomy)来显示交货时间信息。

add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );

function delivery_estimate() {

   global $product;

   // 检查产品类型是否为 'product'
   if ( 'product' == $product->post_type ) {

       // 获取产品的自定义分类 'available_now'
       $terms = wp_get_post_terms( $product->id, 'available_now' );

       // 错误处理
       if ( $terms instanceof WP_Error ) {
           // 在这里记录错误信息,或者进行其他处理
           error_log("Error getting terms: " . $terms->get_error_message());

       } elseif ( ! empty( $terms ) ) {

           $term = $terms[ 0 ]; // 假设产品只有一个 'available_now' 分类

           // 检查分类的别名 (slug) 是否为 'ready-to-ship'
           if ( 'ready-to-ship' == $term->slug ) {

               date_default_timezone_set( 'Europe/Tallinn' );

               // 如果是周五/周六/周日,则交货时间为下周一
               if ( date( 'N' ) >= 5 ) {
                  $del_day = date( "l jS F", strtotime( "next tuesday" ) );
                  $order_by = "Monday";
               }

               // 如果是周一/周四下午4点后,则交货时间为后天
               elseif ( date( 'H' ) >= 16 ) {
                  $del_day = date( "l jS F", strtotime( "tomorrow + 1 day" ) );
                  $order_by = "tomorrow";
               }

               // 如果是周一/周四下午4点前,则交货时间为明天
               else {
                  $del_day = date( "l jS F", strtotime( "tomorrow" ) );
                  $order_by = "today";
               }

               // 获取剩余时间,假设截止时间是每天的16:00
               $now = time();
               $end_of_day = strtotime('today 16:00');
               $hours_left = round(($end_of_day - $now) / 3600);

               // 构建HTML消息
               $html = "<br><div class='woocommerce-message' style='clear:both'>Ready for delivery between " . date("jS F", strtotime($del_day)) . " & " . date("jS F", strtotime($del_day . " + 3 days")) . " when you order within {$hours_left} hours.</div>";

               echo $html;

        } // endif

    } // endif

}
登录后复制

代码解释:

  1. add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );: 将 delivery_estimate 函数挂载到 woocommerce_before_add_to_cart_form 这个 action hook 上,以便在添加到购物车表单之前执行该函数。
  2. global $product;: 获取全局的 $product 对象,该对象包含了当前产品的信息。
  3. if ( 'product' == $product->post_type ): 确认当前页面展示的是产品页面,避免在其他页面执行。
  4. $terms = wp_get_post_terms( $product->id, 'available_now' );: 获取当前产品 available_now 这个自定义分类的所有信息。请将 available_now 替换成你实际使用的分类名。
  5. if ( $terms instanceof WP_Error ): 检查获取分类信息时是否发生错误。如果发生错误,可以记录错误日志或者进行其他处理。
  6. elseif ( ! empty( $terms ) ): 确认分类信息不为空,即产品确实属于 available_now 这个分类。
  7. $term = $terms[ 0 ];: 获取第一个分类信息。通常一个产品只会属于一个 available_now 分类,所以取第一个即可。
  8. if ( 'ready-to-ship' == $term->slug ): 检查分类的别名(slug)是否为 ready-to-ship。 请将 ready-to-ship 替换成你实际使用的分类别名。
  9. 时间计算部分: 根据当前日期和时间计算预计的交货日期。
  10. $html = ...: 构建HTML消息,显示预计交货时间和剩余时间。
  11. echo $html;: 输出HTML消息,将其显示在产品页面上。

注意事项:

  • 请将代码中的 'available_now' 替换成你实际使用的自定义分类名。
  • 请将代码中的 'ready-to-ship' 替换成你实际使用的分类别名。
  • date_default_timezone_set( 'Europe/Tallinn' ); 设置时区,请根据实际情况修改。
  • 可以根据实际需求修改HTML消息的内容和样式。
  • 错误处理部分可以根据实际需求进行更详细的处理,例如记录错误日志、发送邮件通知等。

处理产品库存状态

除了根据产品分类显示交货时间,还需要考虑产品库存状态。如果产品缺货,则不应该显示交货时间信息。以下代码展示了如何在产品缺货时隐藏交货时间提示。

Symanto Text Insights
Symanto Text Insights

基于心理语言学分析的数据分析和用户洞察

Symanto Text Insights 84
查看详情 Symanto Text Insights
add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );

function delivery_estimate() {

   global $product;

   // 检查产品类型是否为 'product'
   if ( 'product' == $product->post_type ) {

        // 检查产品是否在库存中
        if ( $product->is_in_stock() ) {

           // 获取产品的自定义分类 'available_now'
           $terms = wp_get_post_terms( $product->id, 'available_now' );

           // 错误处理
           if ( $terms instanceof WP_Error ) {
               // 在这里记录错误信息,或者进行其他处理
               error_log("Error getting terms: " . $terms->get_error_message());

           } elseif ( ! empty( $terms ) ) {

               $term = $terms[ 0 ]; // 假设产品只有一个 'available_now' 分类

               // 检查分类的别名 (slug) 是否为 'ready-to-ship'
               if ( 'ready-to-ship' == $term->slug ) {

                   date_default_timezone_set( 'Europe/Tallinn' );

                   // 如果是周五/周六/周日,则交货时间为下周一
                   if ( date( 'N' ) >= 5 ) {
                      $del_day = date( "l jS F", strtotime( "next tuesday" ) );
                      $order_by = "Monday";
                   }

                   // 如果是周一/周四下午4点后,则交货时间为后天
                   elseif ( date( 'H' ) >= 16 ) {
                      $del_day = date( "l jS F", strtotime( "tomorrow + 1 day" ) );
                      $order_by = "tomorrow";
                   }

                   // 如果是周一/周四下午4点前,则交货时间为明天
                   else {
                      $del_day = date( "l jS F", strtotime( "tomorrow" ) );
                      $order_by = "today";
                   }

                   // 获取剩余时间,假设截止时间是每天的16:00
                   $now = time();
                   $end_of_day = strtotime('today 16:00');
                   $hours_left = round(($end_of_day - $now) / 3600);

                   // 构建HTML消息
                   $html = "<br><div class='woocommerce-message' style='clear:both'>Ready for delivery between " . date("jS F", strtotime($del_day)) . " & " . date("jS F", strtotime($del_day . " + 3 days")) . " when you order within {$hours_left} hours.</div>";

                   echo $html;

            } // endif

        } // endif
    } // endif
}
}
登录后复制

代码解释:

  • if ( $product->is_in_stock() ): 使用 $product->is_in_stock() 函数检查产品是否在库存中。只有当产品在库存中时,才会显示交货时间信息。

总结:

通过以上代码示例,你可以在WooCommerce中根据产品分类和库存状态动态显示预计交货时间。这可以帮助你更好地向客户展示产品信息,提高用户体验,并最终促进销售。请务必根据你的实际需求修改代码,并进行充分的测试。

以上就是WooCommerce教程:根据产品分类显示预计交货时间,并处理库存状态的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号