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

我有以下自定义元键,它是结帐时的一个选择复选框:


//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 in the order meta, when checkbox has been checked

add_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 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 ){

    $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 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'] = '<p align="center">Opted in?</p>'; // title

    $columns['order_actions'] = $action_column;

    return $columns;

}

}


神不在的星期二
浏览 79回答 1
1回答

慕神8447489

我已经稍微重新访问了您现有的代码并为“营销选择”自定义字段添加了一个下拉过滤器://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() {&nbsp; &nbsp; echo '<div id="marketing_opting_field">';&nbsp; &nbsp; woocommerce_form_field( 'marketing_opting', array(&nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; &nbsp; => 'checkbox',&nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp;=> array('input-checkbox'),&nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp;=> __('Yes, sign me up'),&nbsp; &nbsp; &nbsp; &nbsp; 'default'&nbsp; &nbsp;=> 1,&nbsp; &nbsp; ),&nbsp; WC()->checkout->get_value( 'marketing_opting' ) );&nbsp; &nbsp; echo '</div>';}// Save the optin field as custom order meta, when checkbox has been checkedadd_action( 'woocommerce_checkout_create_order', 'action_checkout_update_order_meta', 10, 2 );function action_checkout_update_order_meta( $order, $data ) {&nbsp; &nbsp; if( isset($_POST['marketing_opting']) )&nbsp; &nbsp; &nbsp; &nbsp; $order->update_meta_data( '_marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );}// Save the optin field as custom user meta, when checkbox has been checkedadd_action( 'woocommerce_checkout_update_customer', 'action_checkout_update_customer_meta', 10, 2 );function action_checkout_update_customer_meta( $customer, $data ) {&nbsp; &nbsp; if( isset($_POST['marketing_opting']) )&nbsp; &nbsp; &nbsp; &nbsp; $customer->update_meta_data( 'marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );}// 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 ){&nbsp; &nbsp; if( $order->get_meta( '_marketing_opting' ) === 'yes' )&nbsp; &nbsp; &nbsp; &nbsp; 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){&nbsp; &nbsp; $action_column = $columns['order_actions'];&nbsp; &nbsp; unset($columns['order_actions']);&nbsp; &nbsp; //add the new column "Opt in"&nbsp; &nbsp; $columns['order_marketing'] = '<div align="center">' .__("Opted in?") . '</div>'; // title&nbsp; &nbsp; $columns['order_actions'] = $action_column;&nbsp; &nbsp; return $columns;}// Add the data for each orderadd_action( 'manage_shop_order_posts_custom_column' , 'custom_order_list_column_content', 10, 2 );function custom_order_list_column_content( $column, $post_id ){&nbsp; &nbsp; global $post, $the_order;&nbsp; &nbsp; if ($column ==='order_marketing') {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $value = $the_order->get_meta( '_marketing_opting' );&nbsp; &nbsp; &nbsp; &nbsp; $label = $value === 'yes' ? __('Signed Up') : ucfirst($value);&nbsp; &nbsp; &nbsp; &nbsp; $color = $value === 'yes' ? 'color:#00cc00;' : 'color:#bbbbbb;';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p align="center" style="'.$color.'"><span class="dashicons dashicons-'.$value.'"></span><span style="font-weight:600;">'.$label.'</span></p>';&nbsp; &nbsp; }}// 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 fieldadd_filter( 'woocommerce_shop_order_search_fields', 'marketing_search_fields', 10, 1 );function marketing_search_fields( $meta_keys ){&nbsp; &nbsp; $meta_keys[] = '_marketing_opting';&nbsp; &nbsp; return $meta_keys;}// 4. Add a dropdown filter to get orders by marketing optin meta value// Add a dropdown to filter orders by Marketing optinadd_action( 'restrict_manage_posts', 'display_admin_shop_order_marketing_opting_filter' );function display_admin_shop_order_marketing_opting_filter(){&nbsp; &nbsp; global $pagenow, $post_type;&nbsp; &nbsp; if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {&nbsp; &nbsp; &nbsp; &nbsp; $domain&nbsp; &nbsp; = 'woocommerce';&nbsp; &nbsp; &nbsp; &nbsp; $current&nbsp; &nbsp;= isset($_GET['filter_shop_order_marketing'])? $_GET['filter_shop_order_marketing'] : '';&nbsp; &nbsp; &nbsp; &nbsp; echo '<select name="filter_shop_order_marketing">&nbsp; &nbsp; &nbsp; &nbsp; <option value="">' . __('Filter Marketing optin', $domain) . '</option>';&nbsp; &nbsp; &nbsp; &nbsp; $options = ['yes' => __('Signed Up'), 'no' => __('No')];&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $options as $key => $label ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( '<option value="%s"%s>%s</option>', $key,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $key === $current ? '" selected="selected"' : '', $label );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo '</select>';&nbsp; &nbsp; }}// Process the filter dropdown for orders by Marketing optinadd_filter( 'request', 'process_admin_shop_order_marketing_opting_filter', 99 );function process_admin_shop_order_marketing_opting_filter( $vars ) {&nbsp; &nbsp; global $pagenow, $typenow;&nbsp; &nbsp; if ( $pagenow == 'edit.php' && isset( $_GET['filter_shop_order_marketing'] )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; && $_GET['filter_shop_order_marketing'] != '' && 'shop_order' === $typenow ) {&nbsp; &nbsp; &nbsp; &nbsp; $vars['meta_key']&nbsp; &nbsp;= '_marketing_opting';&nbsp; &nbsp; &nbsp; &nbsp; $vars['meta_value'] = wc_clean( $_GET['filter_shop_order_marketing'] );&nbsp; &nbsp; }&nbsp; &nbsp; return $vars;}注意:我已经将 meta_key 的顺序更改为_marketing_opting以下划线开头,因为大多数其他现有的元键......此外,我还添加了一个功能,用于在用户元数据中注册“营销选择”值,因为它将在结账时用于已下WC()->checkout->get_value( 'marketing_opting' )订单的客户。现场验证(可选)如果您将此结帐字段设置为必填,则需要字段验证……然后添加以下内容:// Custom Checkout field validationadd_action('woocommerce_checkout_process', 'custom_checkout_field_validation');function custom_checkout_field_validation() {&nbsp; &nbsp; if ( isset($_POST['marketing_opting']) ) {&nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( '<strong>'. __("Please select a value", "woocommerce") . '</strong> | '.$_POST['marketing_opting'], 'error' );&nbsp; &nbsp; }}&nbsp; &nbsp;&nbsp;代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP