分享笔记:

WooCommerce 添加预计送达时间

货物抵达时间

通过代码添加产品送达时间

如果你想要一个快速、简单、易于实现的方法,那没有什么比这段代码更合适了。你只需按照你平时在网站中添加代码的方式,把它加入即可:

/* 产品页送达时间 */
add_action('before_woocommerce_init', function () {
if (class_exists('AutomatticWooCommerceUtilitiesFeaturesUtil')) {
AutomatticWooCommerceUtilitiesFeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
}
});
// 单个产品运输时间
function jhchen_delivery_field_simple_product() {
woocommerce_wp_text_input([
'id' => '_shipping_time',
'label' => __('运达时间', 'wc-shipping-time'),
'description' => __('输入预计送达时间,例如 24 小时、48 小时、3 天', 'wc-shipping-time'),
'desc_tip' => true,
]);
}
add_action('woocommerce_product_options_general_product_data', 'jhchen_delivery_field_simple_product');
// 保存单个产品运输时间
function 
jhchen_save_delivery_field_simple_product($post_id) {
if (isset($_POST['_shipping_time'])) {
update_post_meta($post_id, '_shipping_time', sanitize_text_field($_POST['_shipping_time']));
}
}
add_action('woocommerce_process_product_meta', 'jhchen_save_delivery_field_simple_product');
// 变体产品运输时间
function jhchen_delivery_field_variaciones($loop, $variation_data, $variation) {
woocommerce_wp_text_input([
'id' => "variation_shipping_time_$variation->ID",
'label' => __('运达时间', 'wc-shipping-time'),
'description' => __('输入预计送达时间,例如 24 小时、48 小时、3 天', 'wc-shipping-time'),
'desc_tip' => true,
'type' => 'text',
'value' => get_post_meta($variation->ID, '_shipping_time', true),
]);
}
add_action('woocommerce_variation_options_pricing', 'jhchen_delivery_field_variaciones', 10, 3);
// 保存变体产品运输时间
function 
jhchen_save_delivery_field_variaciones($variation_id) {
if (isset($_POST["variation_shipping_time_$variation_id"])) {
update_post_meta($variation_id, '_shipping_time', sanitize_text_field($_POST["variation_shipping_time_$variation_id"]));
}
}
add_action('woocommerce_save_product_variation', 'jhchen_save_delivery_field_variaciones', 10, 2);

function jhchen_save_date_variaciones($data, $product, $variation) {
if (!is_array($data)) {
$data = [];
}
$shipping_time = get_post_meta($variation->get_id(), '_shipping_time', true);
$data['shipping_time'] = !empty($shipping_time) ? $shipping_time : '';
return $data;
}
add_filter('woocommerce_available_variation', 'jhchen_save_date_variaciones', 10, 3);
// 产品页显示送达时间
function jhchen_show_delivery_field() {
global $product;
if (!$product || !is_a($product, 'WC_Product')) {
return;
}
$shipping_time = '';
// 
if ($product->is_type('variable')) {
$default_attributes = $product->get_default_attributes();
foreach ($product->get_available_variations() as $variation) {
$match = true;
foreach ($default_attributes as $attribute => $value) {
if ($variation['attributes']['attribute_' . $attribute] !== $value) {
$match = false;
break;
}
}
if ($match) {
$variation_id = $variation['variation_id'];
$shipping_time = get_post_meta($variation_id, '_shipping_time', true);
break;
}
}
}
// 如果变体没有送达时间,则使用父产品的送达时间。
if (empty($shipping_time)) {
$shipping_time = get_post_meta($product->get_id(), '_shipping_time', true);
}
?>
<p id="jhchen-estimated-delivery" style="display: <?php echo empty($shipping_time) ? 'none' : 'block'; ?>;">
<strong><?php echo esc_html__('预计送达时间:', 'wc-shipping-time'); ?></strong>
<span><?php echo esc_html($shipping_time); ?></span>
</p>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('form.variations_form').on('found_variation', function(event, variation) {
if (variation.shipping_time) {
$('#jhchen-estimated-delivery').show();
$('#jhchen-estimated-delivery span').text(variation.shipping_time);
} else {
$('#jhchen-estimated-delivery').hide();
}
});
$('form.variations_form').on('reset_data', function() {
$('#jhchen-estimated-delivery').hide();
});
});
</script>
<?php
}
add_action('woocommerce_single_product_summary', 'jhchen_show_delivery_field', 20);

这个代码的作用是:

  • 简单商品添加一个自由填写的字段,用于输入预计送达时间,并带有提示信息,说明可以填写哪些内容。

  • 可变商品的每个变体添加一个同样的自由填写字段,也有提示说明。

  • 将你填写的预计送达时间信息保存到数据库里,并且与 WooCommerce 的高性能订单存储(HPOS)兼容。

  • 在商品详情页显示你为该商品设置的预计送达时间,让顾客可以清楚看到。

woocommerce产品运输送达时间woocommerce产品运输送达时间

通过插件添加产品送达时间

如果你更喜欢通过插件的方式来在 WooCommerce 商店中添加预计送达时间,有不少选择可供参考,像 Order Delivery Date for WooCommerce 或者 Delivery & Pickup Date Time for WooCommerce

为笔记评分

平均评分 5 / 5. 摘星者: 2

有疑问?留个言吧!

更多结果...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
?>

更多结果...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors