php - 根据 WooCommerce 统一费率运输方法的自定义字段计算添加交货时间

标签 php wordpress woocommerce checkout shipping-method

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

这是代码:

// 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 .= '<br /><div class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</div>';
}

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 .= '<br /><span class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</span>';
}

return $item_name;
}

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

例如,客户将几道菜添加到购物车并结帐订单。他知道要花多少时间来 cooking 这道菜或那道菜。

但是当客户选择通过 express (统一费率)送货时,在同一个区 block 中,会显示送货时间。

统一费率 = 10 美元

送餐时间=(从订单中选择最长 cooking 时间)分钟。 + 45 分钟。

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

我请求你的帮助!我希望这个问题的答案对许多开发者有用。

最佳答案

尝试以下代码,当在结账页面选择“固定费率”运送方式时,该代码将显示运送时间(根据最高的元素 cooking 时间值 + 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 '<br><small style="margin-left:2em;border:solid 1px #ccc;padding:2px 5px;"><strong>' . __("Delivery time", "woocommerce") . '</strong>: ' . $delivery_time . ' min.</small>';
        }
    }
}

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

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

enter image description here


enter image description here

关于php - 根据 WooCommerce 统一费率运输方法的自定义字段计算添加交货时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55358124/

相关文章:

php - 将 WooCommerce 产品简短描述限制为仅文本(禁用tinymce 编辑器)

php - 如果使用优惠券则不含税,否则商品含税

php - android中的RSA生成正确的 key 但解密错误

php - 不同身份证的处理

javascript - 在页面上的 Wordpress 循环中从一个帖子滚动到另一个帖子,显示所有帖子

javascript - 将我的 php 数组转换为等效的 jQuery 数组

javascript - 使用 PHP 或 JS 将 BSON 检索到 MongoDB 镜像

php - 我该如何优化这个 MySQL 查询?

mysql - 带有 WooCommerce 插件的 WordPress 数据库未显示所有数据

php - 通过 AJAX 添加可变产品后更新 WooCommerce 购物车