1 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
我想在您的情況下,您想在“本地取貨”WooCommerce 啟用的運(yùn)輸方式下顯示一些字段。
除了覆蓋cart-shipping.php模板,您還可以更好地使用運(yùn)輸方式的專用操作掛鉤,這將允許您顯示特定運(yùn)輸方式的字段。
選擇以下代碼后,您可以在“本地取貨”送貨方式下顯示 2 個(gè)單選按鈕。如果客戶忘記選擇商店,則會(huì)有一條消息提醒他選擇商店取貨。然后在下訂單時(shí),所選商店將保存為自定義訂單元數(shù)據(jù),顯示在帳單地址下的管理員訂單中以及所選送貨方式附近的任何地方(訂單和電子郵件通知):
// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'pickup_store_custom_field', 100, 2 );
function pickup_store_custom_field( $method, $index ) {
// Only on checkout page and for Local pickup shipping method
if( is_cart() || $method->method_id !== 'local_pickup' )
return;
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods')[ $index ];
$chosen_method_id = explode(':', $chosen_shipping_methods);
$chosen_method_id = reset($chosen_method_id);
// Only when the chosen shipping method is "local_pickup"
if( $chosen_method_id !== 'local_pickup' )
return;
echo '<div class="wrapper-pickup_store" style="margin-top:16px">
<label class="title">' . __("Choose your pickup store") . ':</label><br>
<label for="barsha-store">
<input type="radio" id="barsha-store" name="pickup_store" value="Barsha">'
. __("Barsha") . '<a href="#"><small> '. __("Check Location") . '</small></a>
</label><br>
<label for="deira-store">
<input type="radio" id="deira-store" name="pickup_store" value="Deira">'
. __("Deira") . '<a href="#"><small> '. __("Check Location") . '</small></a>
</label>
</div>';
}
// Pickup store field validation
add_action('woocommerce_checkout_process', 'pickup_store_checkout_validation');
function pickup_store_checkout_validation() {
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods')['0'];
$chosen_method_id = explode(':', $chosen_shipping_methods);
$chosen_method_id = reset($chosen_method_id);
if( $chosen_method_id === 'local_pickup' && empty( $_POST['pickup_store'] ) )
wc_add_notice( __("Please choose a pickup store"), "error" );
}
// Save chosen Pickup store as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'pickup_store_update_order_meta' );
function pickup_store_update_order_meta( $order ) {
if( isset( $_POST['pickup_store'] ) && ! empty( $_POST['pickup_store'] ) )
$order->update_meta_data( 'pickup_store', esc_attr( $_POST['pickup_store'] ) );
}
// Display the chosen pickup store under billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_pickup_store_on_order_edit_pages' );
function display_pickup_store_on_order_edit_pages( $order ){
if( $pickup_store = $order->get_meta( 'pickup_store' ) )
echo '<p><strong>Pickup store:</strong> '.$pickup_store.'</p>';
}
// Display the chosen pickup store below the chosen shipping method everywhere
add_filter( 'woocommerce_get_order_item_totals', 'display_pickup_store_on_order_item_totals', 1000, 3 );
function display_pickup_store_on_order_item_totals( $total_rows, $order, $tax_display ){
if( $pickup_store = $order->get_meta( 'pickup_store' ) ) {
$new_total_rows = [];
// Loop through order total rows
foreach( $total_rows as $key => $values ) {
$new_total_rows[$key] = $values;
// Inserting the pickup store under shipping method
if( $key === 'shipping' ) {
$new_total_rows['pickup_store'] = array(
'label' => __("Pickup store"),
'value' => $pickup_store
);
}
}
return $new_total_rows;
}
return $total_rows;
}
代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件。測(cè)試和工作。
在結(jié)帳頁(yè)面上:
關(guān)于客戶訂單(和電子郵件通知):
在管理員訂單編輯頁(yè)面(在賬單地址下):
- 1 回答
- 0 關(guān)注
- 150 瀏覽
添加回答
舉報(bào)