Giới thiệu
hocvietcode.com là website chia sẻ và cập nhật tin tức công nghệ, chia sẻ kiến thức, kỹ năng. Chúng tôi rất cảm ơn và mong muốn nhận được nhiều phản hồi để có thể phục vụ quý bạn đọc tốt hơn !
Liên hệ quảng cáo: trienkhaiweb@gmail.com
こんにちは皆さん、ワードプレスのテーマを作成するときに、テーマのフォルダー名を編集したいという状況に遭遇したはずですが、テーマの名前を変更すると、テーマはすべての古い構成を失います。この記事では、それを解決する方法を説明 […]
利用可能なフィルターを介してwoocommerceチェックアウトページのフィールド(入力フィールド)を追加、更新、または削除するための手順
今日、woocommerceチェックアウトにフィールドを追加する必要があるプロジェクトがあります。コアを直接編集すると、コードを更新するたびに息が切れるので、書き直すと便利です。将来的に時間を節約できること。
ドキュメント: https ://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
説明は、通常のwoocommerceチェックアウトページ内に、入力フィールドのリストを含む2つの列があり、最初の列は請求、2番目の列は配送先住所であるということです。それらはすべて配列変数に格納されています。フィールドを追加、更新、削除して、次の関数を使用してwoocommerceフォームに入力を追加できます。
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields'); function custom_override_checkout_fields($fields) { /* todo, biến fields là biến mảng có cấu trúc, bên trong là các phần tử billing,shipping,account,order và các phần tử này lại là các mảng con, mô tả cấu trúc của các fields nhập liệu khi người dùng checkout */ }
フィルタを介してキャプチャされた$fields変数をダンプすると、次のように構成されます。
array(4) { ["billing"]=> array(11) { ["billing_first_name"]=> array(5) { ["label"]=> string(4) "Tên" ["required"]=> bool(true) ["class"]=> array(1) { [0]=> string(14) "form-row-first" } ["autocomplete"]=> string(10) "given-name" ["priority"]=> int(10) } ["billing_last_name"]=> array(5) { ["label"]=> string(4) "Họ" ["required"]=> bool(true) ["class"]=> array(1) { [0]=> string(13) "form-row-last" } ["autocomplete"]=> string(11) "family-name" ["priority"]=> int(20) } ["billing_company"]=> array(5) { ["label"]=> string(13) "Tên công ty" ["class"]=> array(1) { [0]=> string(13) "form-row-wide" } ["autocomplete"]=> string(12) "organization" ["priority"]=> int(30) ["required"]=> bool(false) } ["billing_country"]=> array(6) { ["type"]=> string(7) "country" ["label"]=> string(16) "Country / Region" ["required"]=> bool(true) ["class"]=> array(3) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" [2]=> string(23) "update_totals_on_change" } ["autocomplete"]=> string(7) "country" ["priority"]=> int(40) } ["billing_address_1"]=> array(6) { ["label"]=> string(12) "Địa chỉ" ["placeholder"]=> string(12) "Địa chỉ" ["required"]=> bool(true) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["autocomplete"]=> string(13) "address-line1" ["priority"]=> int(50) } ["billing_address_2"]=> array(5) { ["placeholder"]=> string(39) "Apartment, suite, unit, etc. (optional)" ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["autocomplete"]=> string(13) "address-line2" ["priority"]=> int(60) ["required"]=> bool(false) } ["billing_postcode"]=> array(6) { ["label"]=> string(16) "Mã bưu điện" ["required"]=> bool(false) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["validate"]=> array(1) { [0]=> string(8) "postcode" } ["autocomplete"]=> string(11) "postal-code" ["priority"]=> int(65) } ["billing_city"]=> array(5) { ["label"]=> string(21) "Tỉnh / Thành phố" ["required"]=> bool(true) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["autocomplete"]=> string(14) "address-level2" ["priority"]=> int(70) } ["billing_state"]=> array(9) { ["type"]=> string(5) "state" ["label"]=> string(12) "Bang / Hạt" ["required"]=> bool(false) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["validate"]=> array(1) { [0]=> string(5) "state" } ["autocomplete"]=> string(14) "address-level1" ["priority"]=> int(80) ["country_field"]=> string(15) "billing_country" ["country"]=> string(2) "VN" } ["billing_phone"]=> array(7) { ["label"]=> string(20) "Số điện thoại" ["required"]=> bool(true) ["type"]=> string(3) "tel" ["class"]=> array(1) { [0]=> string(13) "form-row-wide" } ["validate"]=> array(1) { [0]=> string(5) "phone" } ["autocomplete"]=> string(3) "tel" ["priority"]=> int(100) } ["billing_email"]=> array(7) { ["label"]=> string(18) "Địa chỉ email" ["required"]=> bool(true) ["type"]=> string(5) "email" ["class"]=> array(1) { [0]=> string(13) "form-row-wide" } ["validate"]=> array(1) { [0]=> string(5) "email" } ["autocomplete"]=> string(14) "email username" ["priority"]=> int(110) } } ["shipping"]=> array(9) { ["shipping_first_name"]=> array(5) { ["label"]=> string(4) "Tên" ["required"]=> bool(true) ["class"]=> array(1) { [0]=> string(14) "form-row-first" } ["autocomplete"]=> string(10) "given-name" ["priority"]=> int(10) } ["shipping_last_name"]=> array(5) { ["label"]=> string(4) "Họ" ["required"]=> bool(true) ["class"]=> array(1) { [0]=> string(13) "form-row-last" } ["autocomplete"]=> string(11) "family-name" ["priority"]=> int(20) } ["shipping_company"]=> array(5) { ["label"]=> string(13) "Tên công ty" ["class"]=> array(1) { [0]=> string(13) "form-row-wide" } ["autocomplete"]=> string(12) "organization" ["priority"]=> int(30) ["required"]=> bool(false) } ["shipping_country"]=> array(6) { ["type"]=> string(7) "country" ["label"]=> string(16) "Country / Region" ["required"]=> bool(true) ["class"]=> array(3) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" [2]=> string(23) "update_totals_on_change" } ["autocomplete"]=> string(7) "country" ["priority"]=> int(40) } ["shipping_address_1"]=> array(6) { ["label"]=> string(12) "Địa chỉ" ["placeholder"]=> string(12) "Địa chỉ" ["required"]=> bool(true) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["autocomplete"]=> string(13) "address-line1" ["priority"]=> int(50) } ["shipping_address_2"]=> array(5) { ["placeholder"]=> string(39) "Apartment, suite, unit, etc. (optional)" ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["autocomplete"]=> string(13) "address-line2" ["priority"]=> int(60) ["required"]=> bool(false) } ["shipping_postcode"]=> array(6) { ["label"]=> string(16) "Mã bưu điện" ["required"]=> bool(false) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["validate"]=> array(1) { [0]=> string(8) "postcode" } ["autocomplete"]=> string(11) "postal-code" ["priority"]=> int(65) } ["shipping_city"]=> array(5) { ["label"]=> string(21) "Tỉnh / Thành phố" ["required"]=> bool(true) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["autocomplete"]=> string(14) "address-level2" ["priority"]=> int(70) } ["shipping_state"]=> array(9) { ["type"]=> string(5) "state" ["label"]=> string(12) "Bang / Hạt" ["required"]=> bool(false) ["class"]=> array(2) { [0]=> string(13) "form-row-wide" [1]=> string(13) "address-field" } ["validate"]=> array(1) { [0]=> string(5) "state" } ["autocomplete"]=> string(14) "address-level1" ["priority"]=> int(80) ["country_field"]=> string(16) "shipping_country" ["country"]=> string(2) "VN" } } ["account"]=> array(0) { } ["order"]=> array(1) { ["order_comments"]=> array(4) { ["type"]=> string(8) "textarea" ["class"]=> array(1) { [0]=> string(5) "notes" } ["label"]=> string(20) "Ghi chú đơn hàng" ["placeholder"]=> string(107) "Ghi chú về đơn hàng, ví dụ: thời gian hay chỉ dẫn địa điểm giao hàng chi tiết hơn." } } }
Woocommerceに組み込まれており、要素を追加するか既存の要素を更新することにより、fields変数を更新します。サンプルのコード例は次のとおりです。誕生日というラベルが付けられ、日付が入力されたフィールドを追加して、ユーザーが日付を選択できるようにします。
// Hook in add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields'); // Our hooked in function - $fields is passed via the filter! function custom_override_checkout_fields($fields) { // add birthday fields $fields['billing']['birthday'] = array( 'label' => __('Birthday', 'woocommerce'), // nội dung text thẻ label cho input 'type' => 'date', // type của input 'required' => false, // trường required hay không 'class' => array('form-row-wide'), // class html trong input 'clear' => true ); return $fields; }
フィールドを追加した後、更新するときに、注文のためにデータベースのカスタムフィールドにデータを保存する必要があります
/** * Update the order meta with field value */ add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' ); function my_custom_checkout_field_update_order_meta( $order_id ) { if ( ! empty( $_POST['birthday'] ) ) { update_post_meta( $order_id, 'birthday', sanitize_text_field( $_POST['birthday'] ) ); } }
データベースにデータを取得したら、次のコードを使用してバックエンドの順序を表示します
/** * Display field value on the order edit page */ add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); function my_custom_checkout_field_display_admin_order_meta($order){ echo '<p><strong>'.__('Sinh nhật').':</strong> ' . get_post_meta( $order->id, 'birthday', true ) . '</p>'; }
また、バックエンドでクイックビューの順序でデータを表示する場合は、次のコードを参照してください。
/** * Add to quickview order */ add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_billing_data', 10, 2 ); function admin_order_preview_add_custom_billing_data( $data, $order ) { $custom_billing_data = []; // initializing // Custom field 1: Replace '_custom_meta_key1' by the correct custom field metakey if( $custom_value1 = $order->get_meta('birthday') ) { $custom_billing_data[] = $custom_value1; } ## ……… And so on (for each additional custom field). // Check that our custom fields array is not empty if( count($custom_billing_data) > 0 ) { // Converting the array in a formatted string $formatted_custom_billing_data = implode( '<br>', $custom_billing_data ); if( $data['formatted_billing_address'] === __( 'N/A', 'woocommerce' ) ) { $data['formatted_billing_address'] = $formatted_custom_billing_data; } else { $data['formatted_billing_address'] .= '<br>' .'<strong>Sinh nhật</strong>'. $formatted_custom_billing_data; } } return $data; }
上記のコードは、 https ://stackoverflow.com/questions/57846511/show-custom-fields-in-woocommerce-quick-order-previewで入手できます。
幸運を !
hocvietcode.com là website chia sẻ và cập nhật tin tức công nghệ, chia sẻ kiến thức, kỹ năng. Chúng tôi rất cảm ơn và mong muốn nhận được nhiều phản hồi để có thể phục vụ quý bạn đọc tốt hơn !
Liên hệ quảng cáo: trienkhaiweb@gmail.com