Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a field to coupon settings and display the value on Woocommerce admin order list

I am having some really hard times trying to make it work. I've had an idea in back of my head to:

  1. be able to assign single user to a coupon code through an extra field in general coupon tab, which is listing unassigned users to coupon codes

    I dont want to use third party extensions, custom-fields, etc. I was hoping I'll be able to do it through meta data but I failed. Not sure how to get it done properly.

  2. add two extra columns on orders page and display both coupon code and user assigned to it.

    After some time reading docs and xDebugging in phpstorm I have also failed to get it done.

     function order_sellers_and_coupons_columns_values($column)
     {
        global $post, $the_order;
    
        if ($column == 'order_coupon_code') {
            $coupons = $the_order->get_used_coupons(); // not sure how to get coupon object by the coupon code
            echo (count($coupons)) ? $coupons[0] : '';
        }
     }
    
     // even though I see the order objects have an items and coupon lines property, 
     // which is an object, i can't get access to it
     $the_order->items["coupon_lines"]
    

I am not asking for ready-to-go solution, but to show me the way how to get it done.

Thanks in advance for any kind of help.

like image 680
ThereIsNoSpoon Avatar asked Jul 30 '20 18:07

ThereIsNoSpoon


People also ask

How do I display custom fields in WooCommerce orders in admin panel?

First, to create a field, go to WooCommerce > Custom Order Fields. Click “Add Field” and begin creating your order field. The “label” is the field name, and will be displayed in the order details. The “description” will be displayed to the user upon hovering over the “?” symbol.


1 Answers

In WooCommerce admin single coupon pages, we add an extra field for the seller (dealer):

// Add a custom field to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_text_field', 10 );
function add_coupon_text_field() {
    woocommerce_wp_text_input( array(
        'id'                => 'seller_id',
        'label'             => __( 'Assing a seller (dealer)', 'woocommerce' ),
        'placeholder'       => '',
        'description'       => __( 'Assign a seller / dealer to a coupon', 'woocommerce' ),
        'desc_tip'    => true,

    ) );
}

// Save the custom field value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_text_field', 10, 2 );
function save_coupon_text_field( $post_id, $coupon ) {
    if( isset( $_POST['seller_id'] ) ) {
        $coupon->update_meta_data( 'seller_id', sanitize_text_field( $_POST['seller_id'] ) );
        $coupon->save();
    }
}

Then using the following you will add to admin orders list the coupon code (when it's used in the order) with the seller / dealer name:

// Adding a new column to admin orders list
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column' );
function custom_shop_order_column($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['coupons'] = __( 'Coupon','theme_domain');
        }
    }
    return $reordered_columns;
}

// Adding used coupon codes
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    global $the_order;

    if ( $column == 'coupons' ) {
        $coupons = (array) $the_order->get_used_coupons();
        $dealers = [];

        foreach( $coupons as $coupon_code ) {
            $coupon    = new WC_Coupon( $coupon_code );
            $dealers[] = $coupon->get_meta('seller_id');
        }

        if( count($coupons) > 0 )
            echo implode( ', ', $coupons );

        if( count($dealers) > 0 )
            echo '<br><small>(' . implode( ', ', $dealers ) . ')</small>';
    }
}

All code goes in functions.php file of your active child theme (or active theme). Tested and works.


On Admin coupon single pages:

enter image description here

On Admin edit orders list:

enter image description here

like image 113
LoicTheAztec Avatar answered Oct 02 '22 21:10

LoicTheAztec