簡體   English   中英

根據WooCommerce統一運費方式的自定義字段計算添加交貨時間

[英]Add a delivery time based on custom fields calculations for WooCommerce Flat rate Shipping method

我使用代碼在產品編輯頁面上顯示自定義字段。 此文本框顯示單個產品頁面上的烹飪時間。

這是代碼:

// 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;
}

如何根據此代碼獲得以下功能?

例如,客戶將多個菜餚添加到購物車和結賬訂單。 他看到多少時間烹飪這道菜或那道菜。

但是當客戶通過快遞(統一費率)選擇交貨時,在同一區塊中,顯示交貨時間。

統一費率= 10美元

交貨時間=(最長的烹飪時間從訂單中選擇)min。 + 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 '<br><small style="margin-left:2em;border:solid 1px #ccc;padding:2px 5px;"><strong>' . __("Delivery time", "woocommerce") . '</strong>: ' . $delivery_time . ' min.</small>';
        }
    }
}

代碼位於活動子主題(或活動主題)的function.php文件中。 經過測試和工作。

要在購物車頁面中啟用它,請從IF語句中刪除is_checkout() &&

在此輸入圖像描述


在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM