第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 WooCommerce 結(jié)賬時(shí)將單選按鈕添加到特定的送貨方式

在 WooCommerce 結(jié)賬時(shí)將單選按鈕添加到特定的送貨方式

PHP
12345678_0001 2023-04-28 15:55:05
我添加了一種新的送貨方式“從商店取貨”,可以免費(fèi)送貨到 WooCommerce。我有 2 家商店,“Barsha”和“Deira”。我希望當(dāng)客戶選擇從商店取貨時(shí)能夠選擇他將訪問(wèn)的商店。這是我添加到cart-shipping.php模板文件中的內(nèi)容:<?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?
查看完整描述

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è)面上:

http://img1.sycdn.imooc.com//644b7c0400016bde05240230.jpg

關(guān)于客戶訂單(和電子郵件通知):

http://img1.sycdn.imooc.com//644b7c0e0001149405860051.jpg

在管理員訂單編輯頁(yè)面(在賬單地址下)

http://img1.sycdn.imooc.com//644b7c170001e64c01480074.jpg




查看完整回答
反對(duì) 回復(fù) 2023-04-28
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)