Pawan Kumawat

How to know “Who created this coupon?”

By default, WooCommerce does not store information about the user who created a specific coupon. However, you can customize your WooCommerce store to track and store this information. Here’s an example of how you can implement this functionality:

Step 1: Open your theme’s functions.php file or create a custom plugin.

Step 2: Add the following code to the file:

// Store the user ID who created the coupon
function save_coupon_creator($post_id) {
    $coupon = get_post($post_id);
    $user_id = get_current_user_id();

    // Add custom meta data to store the user ID
    update_post_meta($post_id, 'coupon_creator', $user_id);
}
add_action('woocommerce_new_coupon', 'save_coupon_creator');

// Display the user who created the coupon
function display_coupon_creator($coupon) {
    $creator_id = get_post_meta($coupon->get_id(), 'coupon_creator', true);

    if ($creator_id) {
        $creator_name = get_the_author_meta('display_name', $creator_id);
        echo '<p>Coupon Creator: ' . $creator_name . '</p>';
    }
}
add_action('woocommerce_coupon_options_usage', 'display_coupon_creator');

Step 3: Save the file and upload it to your theme directory or activate the custom plugin.

With these steps, the save_coupon_creator() function is triggered when a new coupon is created. It retrieves the current user’s ID using get_current_user_id() and stores it as custom meta data for the coupon using update_post_meta().

The display_coupon_creator() function is triggered when editing a coupon. It retrieves the stored user ID from the custom meta data and displays the creator’s name using get_the_author_meta().

 

To display the information about who created a specific coupon in the WooCommerce coupon table, you can modify the coupon list table by adding a new column for the creator. Here’s an example of how you can implement this functionality:

 

Step 1: Open your theme’s functions.php file or create a custom plugin.

Step 2: Add the following code to the file:

 

// Add custom column to the coupon table
function add_coupon_creator_column($columns) {
    $columns['coupon_creator'] = __('Coupon Creator', 'your-text-domain');
    return $columns;
}
add_filter('manage_edit-shop_coupon_columns', 'add_coupon_creator_column');

// Display the creator in the coupon table
function display_coupon_creator_column($column, $coupon_id) {
    if ($column === 'coupon_creator') {
        $creator_id = get_post_meta($coupon_id, 'coupon_creator', true);

        if ($creator_id) {
            $creator_name = get_the_author_meta('display_name', $creator_id);
            echo $creator_name;
        } else {
            echo '-';
        }
    }
}
add_action('manage_shop_coupon_posts_custom_column', 'display_coupon_creator_column', 10, 2);

// Make the coupon creator column sortable
function make_coupon_creator_column_sortable($columns) {
    $columns['coupon_creator'] = 'coupon_creator';
    return $columns;
}
add_filter('manage_edit-shop_coupon_sortable_columns', 'make_coupon_creator_column_sortable');

// Modify the query for sorting the coupon creator column
function modify_coupon_creator_column_sorting($query) {
    if (is_admin() && $query->is_main_query() && ($orderby = $query->get('orderby')) === 'coupon_creator') {
        $query->set('meta_key', 'coupon_creator');
        $query->set('orderby', 'meta_value_num');
    }
}
add_action('pre_get_posts', 'modify_coupon_creator_column_sorting');

 

Step 3: Save the file and upload it to your theme directory or activate the custom plugin.

With these steps, the add_coupon_creator_column() function adds a new column for “Coupon Creator” to the coupon table. The display_coupon_creator_column() function populates the column with the creator’s name retrieved from the custom meta data.

Additionally, the make_coupon_creator_column_sortable() function makes the coupon creator column sortable, and the modify_coupon_creator_column_sorting() function modifies the query to handle sorting based on the coupon creator column.

By implementing this code, you can view the information about who created each coupon directly in the coupon table in the WooCommerce admin area. This provides quick visibility and easy reference for the creator of each coupon.

 

Categories

Related Blogs

Add same product to cart twice instead of changing quantity in Cart

By default, WooCommerce allows customers to change the quantity of a product in the cart by updating the quantity input field on the cart page. If the quantity is increased, the product will be added to the cart only once with the updated quantity. However, if you want to allow customers to add the same product multiple times as separate items in the cart, you’ll need to customize the cart behavior.

Calculate distance between 2 address using latitude and longitude

To calculate the distance between two locations using their latitude and longitude coordinates, you can use the Haversine formula. The Haversine formula is commonly used to calculate the great-circle distance between two points on the surface of a sphere (such as the Earth) given their latitude and longitude.