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

Programmatically Remove Product From Cart Using Product Id

Want to remove specific WooCommerce product from a cart?
WooCommerce provide ‘WC()->cart->remove_cart_item(string $cart_item_key)’ function to remove a product from cart. if we go through WooCommerce Documentation , wewill find that it accepts cart_item_key as parameter.

Disable Repeat Purchase Of Product

To disable repeat purchase of products in WooCommerce, you can implement a custom solution using a combination of code snippets and WooCommerce hooks. The idea is to prevent customers from adding the same product to the cart if it already exists in the cart. Below are the steps to achieve this: