php商城 配送时间,php-根据WooCommerce统一费率送货方式的自定义字段计算添加送货时间...

我使用在产品编辑页面上显示自定义字段的代码.此文本框在单个产品页面上显示烹饪时间.

这是代码:

// Backend: Display additional product fields

add_action( 'woocommerce_product_options_general_product_data', 'add_time_field_general_product_data' );

function add_time_field_general_product_data() {

// Custom Time Field

woocommerce_wp_text_input( array(

'id' => '_custom_time',

'label' => __( 'Time for cooking', 'woocommerce' ),

));

}

// Backend: Save the data value from the custom fields

add_action( 'woocommerce_admin_process_product_object', 'save_time_custom_fields_values' );

function save_time_custom_fields_values( $product ) {

// Save Custom Time Field

if( isset( $_POST['_custom_time'] ) ) {

$product->update_meta_data( '_custom_time', sanitize_text_field( $_POST['_custom_time'] ) );

}

}

// Display custom fields values under item name in checkout

add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_time_field_checkout_item_name', 10, 3 );

function custom_time_field_checkout_item_name( $item_qty, $cart_item, $cart_item_key ) {

if( $value4 = $cart_item['data']->get_meta('_custom_time') ) {

$item_qty .= '

' . __("Time for cooking", "woocommerce") . ': ' . $value4 . ' min.
';

}

return $item_qty;

}

// Display custom fields values on orders and email notifications

add_filter( 'woocommerce_order_item_name', 'custom_time_field_order_item_name', 10, 2 );

function custom_time_field_order_item_name( $item_name, $item ) {

$product = $item->get_product();

if( $value4 = $product->get_meta('_custom_time') ) {

$item_name .= '' . __("Time for cooking", "woocommerce") . ': ' . $value4 . ' min.';

}

return $item_name;

}

如何根据此代码获得以下功能?

例如,客户向购物车和结帐订单添加了几道菜.他看到有多少时间在煮这道菜.

但是,当客户选择按快递方式(统一费率)交货时,在同一块中显示交货时间.

固定费率= $10

交货时间=(从订单中选择最长的烹饪时间)分钟. 45分钟

据我了解,下订单时需要获取自定义字段“ _custom_time”的数据.然后,以某种方式需要获得该字段的最大值并增加45分钟.

我寻求您的帮助!我希望这个问题的答案对许多开发人员有用.

解决方法:

尝试以下代码,当在结帐页面上选择“统一费率”运输方式时,该代码将显示交付时间(根据最高商品烹饪时间值45分钟计算):

add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate_callback', 10, 2 );

function action_after_shipping_rate_callback( $method, $index ) {

$chosen_shipping_id = WC()->session->get( 'chosen_shipping_methods' )[$index];

if( is_checkout() && $method->method_id === 'flat_rate' && $method->id === $chosen_shipping_id ) {

$extra_time = 45; // Additional time to be added

$data_array = []; // Initializing

// Loop through car items

foreach ( WC()->cart->get_cart() as $cart_item ) {

if( $cooking_time = $cart_item['data']->get_meta('_custom_time') ) {

$data_array[] = (int) $cooking_time;

}

}

if ( sizeof($data_array) ) {

$max_time = (int) max($data_array);

$delivery_time = $max_time $extra_time;

echo '' . __("Delivery time", "woocommerce") . ': ' . $delivery_time . ' min.';

}

}

}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

To enable that in cart page too, remove is_checkout() && from the IF statement.

SKs1L.png

vyeif.png来源:https://www.icode9.com/content-1-551401.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值