在 WooCommerce 结账时将单选按钮添加到特定的送货方式

我添加了一种新的送货方式“从商店取货”,可以免费送货到 WooCommerce。


我有 2 家商店,“Barsha”和“Deira”。我希望当客户选择从商店取货时能够选择他将访问的商店。


这是我添加到cart-shipping.php模板文件中的内容:


<?php                   

    <hr><p>Please choose the pickup store:</p>

        <input type="radio" id="barsha" sname="store" value="barsha">

        <label for="barsha">Barsha<a href=""> Check Location</a></label><br>

        <input type="radio" id="deira" sname="store" value="deira">

        <label for="deira">Deira<a href=""> Check Location</a></label>


    $sname= sname;

    if ( $sname = 'barsha'); {

        printf ('barsha')

    } else {

        printf ('deira')

    }

?>


But I can't get it working. How can I add some radio buttons to a specific shipping method and save the chosen option when order is submitted?


12345678_0001
浏览 111回答 1
1回答

小唯快跑啊

我想在您的情况下,您想在“本地取货”WooCommerce 启用的运输方式下显示一些字段。除了覆盖cart-shipping.php模板,您还可以更好地使用运输方式的专用操作挂钩,这将允许您显示特定运输方式的字段。选择以下代码后,您可以在“本地取货”送货方式下显示 2 个单选按钮。如果客户忘记选择商店,则会有一条消息提醒他选择商店取货。然后在下订单时,所选商店将保存为自定义订单元数据,显示在帐单地址下的管理员订单中以及所选送货方式附近的任何地方(订单和电子邮件通知):// Add custom fields to a specific selected shipping methodadd_action( 'woocommerce_after_shipping_rate', 'pickup_store_custom_field', 100, 2 );function pickup_store_custom_field( $method, $index ) {&nbsp; &nbsp; // Only on checkout page and for Local pickup shipping method&nbsp; &nbsp; if( is_cart() || $method->method_id !== 'local_pickup' )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods')[ $index ];&nbsp; &nbsp; $chosen_method_id = explode(':', $chosen_shipping_methods);&nbsp; &nbsp; $chosen_method_id = reset($chosen_method_id);&nbsp; &nbsp; // Only when the chosen shipping method is "local_pickup"&nbsp; &nbsp; if( $chosen_method_id !== 'local_pickup' )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; echo '<div class="wrapper-pickup_store" style="margin-top:16px">&nbsp; &nbsp; &nbsp; &nbsp; <label class="title">' . __("Choose your pickup store") . ':</label><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="barsha-store">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="barsha-store" name="pickup_store" value="Barsha">'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . __("Barsha") . '<a href="#"><small> '. __("Check Location") . '</small></a>&nbsp; &nbsp; &nbsp; &nbsp; </label><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="deira-store">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="deira-store" name="pickup_store" value="Deira">'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . __("Deira") . '<a href="#"><small> '. __("Check Location") . '</small></a>&nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp; &nbsp; </div>';}// Pickup store field validationadd_action('woocommerce_checkout_process', 'pickup_store_checkout_validation');function pickup_store_checkout_validation() {&nbsp; &nbsp; $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods')['0'];&nbsp; &nbsp; $chosen_method_id = explode(':', $chosen_shipping_methods);&nbsp; &nbsp; $chosen_method_id = reset($chosen_method_id);&nbsp; &nbsp; if( $chosen_method_id === 'local_pickup' && empty( $_POST['pickup_store'] ) )&nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( __("Please choose a pickup store"), "error" );}// Save chosen Pickup store as custom order meta dataadd_action( 'woocommerce_checkout_create_order', 'pickup_store_update_order_meta' );function pickup_store_update_order_meta( $order ) {&nbsp; &nbsp; if( isset( $_POST['pickup_store'] ) && ! empty( $_POST['pickup_store'] ) )&nbsp; &nbsp; &nbsp; &nbsp; $order->update_meta_data( 'pickup_store', esc_attr( $_POST['pickup_store'] ) );}// Display the chosen pickup store under billing addressadd_action( 'woocommerce_admin_order_data_after_billing_address', 'display_pickup_store_on_order_edit_pages' );function display_pickup_store_on_order_edit_pages( $order ){&nbsp; &nbsp; if( $pickup_store = $order->get_meta( 'pickup_store' ) )&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><strong>Pickup store:</strong> '.$pickup_store.'</p>';}// Display the chosen pickup store below the chosen shipping method everywhereadd_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 ){&nbsp; &nbsp; if( $pickup_store = $order->get_meta( 'pickup_store' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $new_total_rows = [];&nbsp; &nbsp; &nbsp; &nbsp; // Loop through order total rows&nbsp; &nbsp; &nbsp; &nbsp; foreach( $total_rows as $key => $values ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_total_rows[$key] = $values;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Inserting the pickup store under shipping method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $key === 'shipping' ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_total_rows['pickup_store'] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => __("Pickup store"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' => $pickup_store&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $new_total_rows;&nbsp; &nbsp; }&nbsp; &nbsp; return $total_rows;}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。在结帐页面上:关于客户订单(和电子邮件通知):在管理员订单编辑页面(在账单地址下):
打开App,查看更多内容
随时随地看视频慕课网APP