跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

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 = "
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.
"; echo $html; } // endif } // endif }
代码解释: add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' ); : 将 delivery_estimate 函数挂载到 woocommerce_before_add_to_cart_form 这个 action hook 上,以便在添加到购物车表单之前执行该函数。 global $product; : 获取全局的 $product 对象,该对象包含了当前产品的信息。 if ( 'product' == $product->post_type ) : 确认当前页面展示的是产品页面,避免在其他页面执行。 $terms = wp_get_post_terms( $product->id, 'available_now' ); : 获取当前产品 available_now 这个自定义分类的所有信息。请将 available_now 替换成你实际使用的分类名。 if ( $terms instanceof WP_Error ) : 检查获取分类信息时是否发生错误。如果发生错误,可以记录错误日志或者进行其他处理。 elseif ( ! empty( $terms ) ) : 确认分类信息不为空,即产品确实属于 available_now 这个分类。 $term = $terms[ 0 ]; : 获取第一个分类信息。通常一个产品只会属于一个 available_now 分类,所以取第一个即可。 if ( 'ready-to-ship' == $term->slug ) : 检查分类的别名(slug)是否为 ready-to-ship。 请将 ready-to-ship 替换成你实际使用的分类别名。 时间计算部分 : 根据当前日期和时间计算预计的交货日期。 $html= ... : 构建HTML消息,显示预计交货时间和剩余时间。 echo $html; : 输出HTML消息,将其显示在产品页面上。 注意事项: 请将代码中的 'available_now' 替换成你实际使用的自定义分类名。 请将代码中的 'ready-to-ship' 替换成你实际使用的分类别名。 date_default_timezone_set( 'Europe/Tallinn' ); 设置时区,请根据实际情况修改。 可以根据实际需求修改HTML消息的内容和样式。 错误处理部分可以根据实际需求进行更详细的处理,例如记录错误日志、发送邮件通知等。 处理产品库存状态 除了根据产品分类显示交货时间,还需要考虑产品库存状态。如果产品缺货,则不应该显示交货时间信息。以下代码展示了如何在产品缺货时隐藏交货时间提示。 php版微信js-sdk支付接口类 php版微信js-sdk支付接口类 下载
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 = "
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.
"; echo $html; } // endif } // endif } // endif } }
代码解释: if ( $product->is_in_stock() ) : 使用 $product->is_in_stock() 函数检查产品是否在库存中。只有当产品在库存中时,才会显示交货时间信息。 总结: 通过以上代码示例,你可以在WooCommerce中根据产品分类和库存状态动态显示预计交货时间。这可以帮助你更好地向客户展示产品信息,提高用户体验,并最终促进销售。请务必根据你的实际需求修改代码,并进行充分的测试。

相关文章