为了账号安全,请及时绑定邮箱和手机立即绑定

在 WooCommerce 管理员订单上为自定义元键添加下拉过滤器

在 WooCommerce 管理员订单上为自定义元键添加下拉过滤器

PHP
神不在的星期二 2023-03-04 17:00:29
我有以下自定义元键,它是结帐时的一个选择复选框://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条经验 获得超1个赞

我已经稍微重新访问了您现有的代码并为“营销选择”自定义字段添加了一个下拉过滤器:


//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;

}

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

注意:我已经将 meta_key 的顺序更改为_marketing_opting以下划线开头,因为大多数其他现有的元键......


此外,我还添加了一个功能,用于在用户元数据中注册“营销选择”值,因为它将在结账时用于已下WC()->checkout->get_value( 'marketing_opting' )订单的客户。


现场验证(可选)


如果您将此结帐字段设置为必填,则需要字段验证……然后添加以下内容:


// 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 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信