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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WooCommerce 購物車和結(jié)賬中運輸方式的額外承運人字段

WooCommerce 購物車和結(jié)賬中運輸方式的額外承運人字段

PHP
慕的地10843 2023-09-08 18:23:01
受Woocommerce 結(jié)帳頁面答案代碼中的運輸承運人自定義字段驗證的啟發(fā),我使用以下代碼顯示運輸公司的選擇字段(僅當我選擇特定的運輸方式時才顯示該字段):add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );function carrier_custom_fields( $method, $index ) {    if( ! is_checkout()) return; // Only on the checkout page    $customer_carrier_method = 'flat_rate:14';    if( $method->id != $customer_carrier_method ) return; // Mostrar solo para "flat_rate:14"    $chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];    // If the chosen shipping method is 'flat_rate: 14', we will show    if($chosen_method_id == $customer_carrier_method ):    echo '<div class="custom-carrier2">';    woocommerce_form_field( 'carrier_name1', array(        'type'          => 'select',        'class'         => array('carrier_name2-class form-row-wide'),        'label'         => __('<strong>Shipping Company</strong>'),        'required'      => 'true',        'options'       => array(            '1'                     => '', // no data means that the field is not selected            'Shipping Company 1'    => 'Shipping Company 1',            'Shipping Company 2'    => 'Shipping Company 2',            'Shipping Company 3'    => 'Shipping Company 3',            'Shipping Company 4'    => 'Shipping Company 4'        )    ), WC()->checkout->get_value( 'carrier_name1' ) );    echo '</div>';    endif;}// Validate the custom selection fieldadd_action('woocommerce_checkout_process', 'carrier_checkout_process');function carrier_checkout_process() {    if( isset( $_POST['carrier_name1'] ) && empty( $_POST['carrier_name1'] ) )        wc_add_notice( ( "<strong>Shipping Company</strong> it is a required field." ), "error" );}問題是它只顯示在結(jié)帳頁面上,我希望它在購物車頁面中顯示它,將購物車頁面上選定的值保留到結(jié)帳頁面。我想我發(fā)現(xiàn)了一些內(nèi)容,表明購物車和支付頁面之間所選數(shù)據(jù)的傳輸是通過 Ajax 完成的,但我不熟悉 Ajax,也不知道如何實現(xiàn)這一點。
查看完整描述

1 回答

?
MM們

TA貢獻1886條經(jīng)驗 獲得超2個贊

為了使其在購物車和結(jié)賬頁面上工作,您將需要一些使用 jQuery、Ajax 和 WC Session 變量的附加代碼:


最終更新- 為了使代碼更加動態(tài),我們從一個自定義函數(shù)開始,它將處理所有必需的設(shè)置:


// Custom function that handle your settings

function carrier_settings(){

    return array(

        'targeted_methods' => array('flat_rate:14'), // Your targeted shipping method(s) in this array

        'field_id'         => 'carrier_name', // Field Id

        'field_type'       => 'select', // Field type

        'field_label'      => '', // Leave empty value if the first option has a text (see below).

        'label_name'       => __("Carrier company","woocommerce"), // for validation and as meta key for orders

        'field_options'    => array(

             // The option displayed at first ( or keep an empty value '',)

            __("Choose a carrier company", "woocommerce"),

            // The carrier companies below (one by line)

            'Company name 1',

            'Company name 2',

            'Company name 3',

            'Company name 4',

        ),

    );

}

然后我們可以將該設(shè)置加載到任何需要的函數(shù)上。


現(xiàn)在,購物車和結(jié)帳頁面上的“選擇”字段中顯示了特定運輸方式的承運商公司:


// Display the custom checkout field

add_action( 'woocommerce_after_shipping_rate', 'carrier_company_custom_select_field', 20, 2 );

function carrier_company_custom_select_field( $method, $index ) {

    extract( carrier_settings() ); // Load settings and convert them in variables


    $chosen  = WC()->session->get('chosen_shipping_methods'); // The chosen methods

    $value   = WC()->session->get($field_id);

    $value   = WC()->session->__isset($field_id) ? $value : WC()->checkout->get_value('_'.$field_id);

    $options = array(); // Initializing


    if( ! empty($chosen) && $method->id === $chosen[$index] && in_array($method->id, $targeted_methods)  ) {

        echo '<div class="custom-carrier">';


        // Loop through field otions to add the correct keys

        foreach( $field_options as $key => $option_value ) {

            $option_key = $key == 0 ? '' : $key;

            $options[$option_key] = $option_value;

        }


        woocommerce_form_field( $field_id, array(

            'type'     => $field_type,

            'label'    => '', // Not required if the first option has a text.

            'class'    => array('form-row-wide ' . $field_id . '-' . $field_type ),

            'required' => true,

            'options'  => $options,

        ), $value );


        echo '</div>';

    }

}

Ajax 部分:所選運營商公司的 jQuery 發(fā)送器 + PHP WordPress 管理 Ajax 接收器代碼:


// jQuery code (client side) - Ajax sender 

add_action( 'wp_footer', 'carrier_company_script_js' );

function carrier_company_script_js() {

    // Only cart & checkout pages

    if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ):


    // Load settings and convert them in variables

    extract( carrier_settings() );


    $js_variable = is_cart() ? 'wc_cart_params' : 'wc_checkout_params';


    // jQuery Ajax code

    ?>

    <script type="text/javascript">

    jQuery( function($){

        if (typeof <?php echo $js_variable; ?> === 'undefined')

            return false;


        $(document.body).on( 'change', 'select#<?php echo $field_id; ?>', function(){

            var value = $(this).val();

            $.ajax({

                type: 'POST',

                url: <?php echo $js_variable; ?>.ajax_url,

                data: {

                    'action': 'carrier_name',

                    'value': value

                },

                success: function (result) {

                    console.log(result); // Only for testing (to be removed)

                }

            });

        });

    });

    </script>

    <?php

    endif;

}


// The Wordpress Ajax PHP receiver

add_action( 'wp_ajax_carrier_name', 'set_carrier_company_name' );

add_action( 'wp_ajax_nopriv_carrier_name', 'set_carrier_company_name' );

function set_carrier_company_name() {

    if ( isset($_POST['value']) ){

        // Load settings and convert them in variables

        extract( carrier_settings() );


        if( empty($_POST['value']) ) {

            $value = 0;

            $label = 'Empty';

        } else {

            $value = $label = esc_attr( $_POST['value'] );

        }


        // Update session variable

        WC()->session->set( $field_id, $value );


        // Send back the data to javascript (json encoded)

        echo $label . ' | ' . $field_options[$value];

        die();

    }

}

然后在結(jié)帳頁面上進行字段驗證并將所選承運公司保存到訂單中:


// Conditional function for validation

function has_carrier_field(){

    $settings = carrier_settings();

    return array_intersect(WC()->session->get( 'chosen_shipping_methods' ), $settings['targeted_methods']);

}


// Validate the custom selection field

add_action('woocommerce_checkout_process', 'carrier_company_checkout_validation');

function carrier_company_checkout_validation() {

    // Load settings and convert them in variables

    extract( carrier_settings() );


    if( has_carrier_field() && isset( $_POST[$field_id] ) && empty( $_POST[$field_id] ) )

        wc_add_notice(

            sprintf( __("Please select a %s as it is a required field.","woocommerce"),

            '<strong>' . $label_name . '</strong>'

        ), "error" );

}


// Save custom field as order meta data

add_action( 'woocommerce_checkout_create_order', 'save_carrier_company_as_order_meta', 30, 1 );

function save_carrier_company_as_order_meta( $order ) {

    // Load settings and convert them in variables

    extract( carrier_settings() );


    if( has_carrier_field() && isset( $_POST[$field_id] ) && ! empty( $_POST[$field_id] ) ) {

        $order->update_meta_data( '_'.$field_id, $field_options[esc_attr($_POST[$field_id])] );

        WC()->session->__unset( $field_id ); // remove session variable

    }

}

在管理訂單頁面、客戶訂單和電子郵件通知上顯示所選承運商:


// Display custom field in admin order pages

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'admin_order_display_carrier_company', 30, 1 );

function admin_order_display_carrier_company( $order ) {

    // Load settings and convert them in variables

    extract( carrier_settings() );


    $carrier = $order->get_meta( '_'.$field_id ); // Get carrier company


    if( ! empty($carrier) ) {

        // Display

        echo '<p><strong>' . $label_name . '</strong>: ' . $carrier . '</p>';

    }

}


// Display carrier company after shipping line everywhere (orders and emails)

add_filter( 'woocommerce_get_order_item_totals', 'display_carrier_company_on_order_item_totals', 1000, 3 );

function display_carrier_company_on_order_item_totals( $total_rows, $order, $tax_display ){

    // Load settings and convert them in variables

    extract( carrier_settings() );


    $carrier = $order->get_meta( '_'.$field_id ); // Get carrier company


    if( ! empty($carrier) ) {

        $new_total_rows = [];


        // Loop through order total rows

        foreach( $total_rows as $key => $values ) {

            $new_total_rows[$key] = $values;

            

            // Inserting the carrier company under shipping method

            if( $key === 'shipping' ) {

                $new_total_rows[$field_id] = array(

                    'label' => $label_name,

                    'value' => $carrier,

                );

            }

        }

        return $new_total_rows;

    }

    return $total_rows;

}

所有代碼都位于活動子主題(或主題)的functions.php 文件中。經(jīng)過測試并有效

在購物車頁面上 (針對所選的特定運輸方式)

https://img1.sycdn.imooc.com//64faf64c0001b72605230263.jpg

在結(jié)帳頁面 (針對所選的特定運輸方式)

https://img1.sycdn.imooc.com//64faf655000132a605120423.jpg

關(guān)于客戶訂單 (電子郵件通知和管理訂單頁面)

https://img1.sycdn.imooc.com//64faf66300011c4706190269.jpg


查看完整回答
反對 回復(fù) 2023-09-08
  • 1 回答
  • 0 關(guān)注
  • 86 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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