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

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

在 WooCommerce 管理員訂單上為自定義元鍵添加下拉過濾器

在 WooCommerce 管理員訂單上為自定義元鍵添加下拉過濾器

PHP
神不在的星期二 2023-03-04 17:00:29
我有以下自定義元鍵,它是結(jié)帳時的一個選擇復選框://1. ADD OPT IN OPTION IN CHECKOUT AND SAVE IN THE ORDER// Add checkbox optin before T&Csadd_action( 'woocommerce_checkout_before_terms_and_conditions', 'marketing_opting_field' );function marketing_opting_field() {    echo '<div id="marketing_opting_field">';    woocommerce_form_field( 'marketing_opting', array(        'type'      => 'checkbox',        'class'     => array('input-checkbox'),        'label'     => __('Yes, sign me up'),        'default'   => 1,    ),  WC()->checkout->get_value( 'marketing_opting' ) );    echo '</div>';}// Save the optin field in the order meta, when checkbox has been checkedadd_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );function custom_checkout_field_update_order_meta( $order_id ) {    if ( ! empty( $_POST['marketing_opting'] ) )        update_post_meta( $order_id, 'marketing_opting', $_POST['marketing_opting'] );}// Display the result of the checked optin in the order under billing addressadd_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );function display_custom_field_on_order_edit_pages( $order ){    $marketing_opting = get_post_meta( $order->get_id(), 'marketing_opting', true );    if( $marketing_opting == 1 )        echo '<p><strong>Has opted in for marketing purposes.</p>';}// 2. SHOW CUSTOM COLUMN FOR THE OPTIN OPTION// Adding custom column titleadd_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 12, 1 );function custom_shop_order_column($columns){    $action_column = $columns['order_actions'];    unset($columns['order_actions']);    //add the new column "Opt in"    $columns['order_marketing'] = '<p align="center">Opted in?</p>'; // title    $columns['order_actions'] = $action_column;    return $columns;}}
查看完整描述

1 回答

?
慕神8447489

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

我已經(jīng)稍微重新訪問了您現(xiàn)有的代碼并為“營銷選擇”自定義字段添加了一個下拉過濾器:


//1. ADD OPT IN OPTION IN CHECKOUT AND SAVE IN THE ORDER


// Add checkbox optin before T&Cs

add_action( 'woocommerce_checkout_before_terms_and_conditions', 'marketing_opting_field' );

function marketing_opting_field() {

    echo '<div id="marketing_opting_field">';

    woocommerce_form_field( 'marketing_opting', array(

        'type'      => 'checkbox',

        'class'     => array('input-checkbox'),

        'label'     => __('Yes, sign me up'),

        'default'   => 1,

    ),  WC()->checkout->get_value( 'marketing_opting' ) );

    echo '</div>';

}


// Save the optin field as custom order meta, when checkbox has been checked

add_action( 'woocommerce_checkout_create_order', 'action_checkout_update_order_meta', 10, 2 );

function action_checkout_update_order_meta( $order, $data ) {

    if( isset($_POST['marketing_opting']) )

        $order->update_meta_data( '_marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );

}


// Save the optin field as custom user meta, when checkbox has been checked

add_action( 'woocommerce_checkout_update_customer', 'action_checkout_update_customer_meta', 10, 2 );

function action_checkout_update_customer_meta( $customer, $data ) {

    if( isset($_POST['marketing_opting']) )

        $customer->update_meta_data( 'marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );

}


// Display the result of the checked optin in the order under billing address

add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );

function display_custom_field_on_order_edit_pages( $order ){

    if( $order->get_meta( '_marketing_opting' ) === 'yes' )

        echo '<p><strong>Has opted in for marketing purposes.</p>';

}


// 2. SHOW CUSTOM COLUMN FOR THE OPTIN OPTION


// Adding custom column title

add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 12, 1 );

function custom_shop_order_column($columns)

{

    $action_column = $columns['order_actions'];

    unset($columns['order_actions']);


    //add the new column "Opt in"

    $columns['order_marketing'] = '<div align="center">' .__("Opted in?") . '</div>'; // title

    $columns['order_actions'] = $action_column;


    return $columns;

}


// Add the data for each order

add_action( 'manage_shop_order_posts_custom_column' , 'custom_order_list_column_content', 10, 2 );

function custom_order_list_column_content( $column, $post_id ){

    global $post, $the_order;



    if ($column ==='order_marketing') { 

        $value = $the_order->get_meta( '_marketing_opting' );

        $label = $value === 'yes' ? __('Signed Up') : ucfirst($value);

        $color = $value === 'yes' ? 'color:#00cc00;' : 'color:#bbbbbb;';


        echo '<p align="center" style="'.$color.'"><span class="dashicons dashicons-'.$value.'"></span><span style="font-weight:600;">'.$label.'</span></p>';

    }

}


// 3. Make marketing optin meta searchable from search field (can't work very well for 'yes' or 'no' values!)


// Make a custom meta field searchable from the admin order list search field

add_filter( 'woocommerce_shop_order_search_fields', 'marketing_search_fields', 10, 1 );

function marketing_search_fields( $meta_keys ){

    $meta_keys[] = '_marketing_opting';

    return $meta_keys;

}


// 4. Add a dropdown filter to get orders by marketing optin meta value


// Add a dropdown to filter orders by Marketing optin

add_action( 'restrict_manage_posts', 'display_admin_shop_order_marketing_opting_filter' );

function display_admin_shop_order_marketing_opting_filter(){

    global $pagenow, $post_type;


    if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {

        $domain    = 'woocommerce';

        $current   = isset($_GET['filter_shop_order_marketing'])? $_GET['filter_shop_order_marketing'] : '';


        echo '<select name="filter_shop_order_marketing">

        <option value="">' . __('Filter Marketing optin', $domain) . '</option>';


        $options = ['yes' => __('Signed Up'), 'no' => __('No')];


        foreach ( $options as $key => $label ) {

            printf( '<option value="%s"%s>%s</option>', $key, 

                $key === $current ? '" selected="selected"' : '', $label );

        }

        echo '</select>';

    }

}


// Process the filter dropdown for orders by Marketing optin

add_filter( 'request', 'process_admin_shop_order_marketing_opting_filter', 99 );

function process_admin_shop_order_marketing_opting_filter( $vars ) {

    global $pagenow, $typenow;


    if ( $pagenow == 'edit.php' && isset( $_GET['filter_shop_order_marketing'] ) 

        && $_GET['filter_shop_order_marketing'] != '' && 'shop_order' === $typenow ) {

        $vars['meta_key']   = '_marketing_opting';

        $vars['meta_value'] = wc_clean( $_GET['filter_shop_order_marketing'] );

    }

    return $vars;

}

http://img1.sycdn.imooc.com//640308f200018dcd08370041.jpg

注意:我已經(jīng)將 meta_key 的順序更改為_marketing_opting以下劃線開頭,因為大多數(shù)其他現(xiàn)有的元鍵......


此外,我還添加了一個功能,用于在用戶元數(shù)據(jù)中注冊“營銷選擇”值,因為它將在結(jié)賬時用于已下WC()->checkout->get_value( 'marketing_opting' )訂單的客戶。


現(xiàn)場驗證(可選)


如果您將此結(jié)帳字段設置為必填,則需要字段驗證……然后添加以下內(nèi)容:


// Custom Checkout field validation

add_action('woocommerce_checkout_process', 'custom_checkout_field_validation');

function custom_checkout_field_validation() {

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

        wc_add_notice( '<strong>'. __("Please select a value", "woocommerce") . '</strong> | '.$_POST['marketing_opting'], 'error' );

    }

}    

代碼進入您的活動子主題(或活動主題)的 functions.php 文件。測試和工作。


查看完整回答
反對 回復 2023-03-04
  • 1 回答
  • 0 關注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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