phpwordpresswoocommercetypescoupon

Allow displaying a custom coupon type in WooCommerce admin coupons list


I have added a new option in the Coupon type select field WooCommerce coupon single page.

enter image description here

Now I want to show that option below the Coupon type column of all coupon page. enter image description here

The value is being saved in the database as 'discount_type' meta value.

So what filter should I use and how to use.

I have used the below code but it does not work.

add_filter('manage_shop_coupon_posts_custom_column', 'custom_coupon_type_column_content', 10, 2);

function custom_coupon_type_column_content($column, $post_id) {
    if ($column === 'type') {
        $coupon = new WC_Coupon($post_id);

        if ($coupon) {
            // Get the coupon type
            $coupon_type = $coupon->get_discount_type();

            // Customize the output based on the coupon type
            switch ($coupon_type) {
                case 'fixed_cart':
                    echo 'Fixed Cart Discount';
                    break;
                case 'percent':
                    echo 'Percentage Discount';
                    break;
                case 'fixed_product':
                    echo 'Fixed Product Discount';
                    break;
                case 'custom_option':
                    echo 'Custom Option';
                    break;
                default:
                    echo 'Unknown Type';
            }
        } else {
            echo 'N/A';
        }
    }
}

Solution

  • To add a custom coupon discount type that is also displayed in admin coupons list, simply use the following code snippet:

    add_filter( 'woocommerce_coupon_discount_types', 'add_custom_coupon_discount_type',10, 1);
    function add_custom_coupon_discount_type( $discount_types ) {
        $discount_types['custom_option'] =__( 'Custom option', 'woocommerce' );
    
        return $discount_types;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    enter image description here

    enter image description here