Search code examples
phpjquerywordpresswoocommercecoupon

Display a custom checkbox in a specific location in WooCommerce admin single coupon settings


I'm adding a new field in the Usage Restriction tab in the coupon single page of WooCommerce. The newly added field is being displayed at the bottom of the tab. I want it to be displayed before the exclude products field.

I'm using the woocommerce_coupon_options_usage_restriction action hook to add the new field and the following function to add a new checkbox:

woocommerce_wp_checkbox(
[
  'id' => 'apply_cart_condition_for_customer_on_products',
  'label' => esc_html__( 'Product Cart Condition', 'hexcoupon' ),
  'description' => esc_html__( 'Check this box to to add a cart condition for the customer based on product.', 'hexcoupon' ),
  'value' => $apply_cart_condition_on_products,
]
);

So I want this checkbox before the default exclude product field. I can't find a way to do that. I Need your help.

I looked into the file 'class-wc-meta-box-coupon-data.php' of woocommerce to see if there is any clue. But, I didn't find anything there.


Solution

  • The following adds a custom checkbox to admin coupon setting in "Usage Restrictions" section just after "Products", and before "Exclude products" multi select fields, using a bit of JavaScript/jQuery code to set it in the right location:

    // Add a checkbox to admin single coupon setting in "Usage Restrictions" section
    add_action('woocommerce_coupon_options_usage_restriction', 'add_coupon_custom_option_usage_restriction', 10, 2 );
    function add_coupon_custom_option_usage_restriction( $coupon_id, $coupon ) {
        woocommerce_wp_checkbox( array(
            'id'            => 'apply_cart_condition',
            'label'         => esc_html__( 'Product Cart Condition', 'hexcoupon' ),
            'description'   => esc_html__( 'Check this box to to add a cart condition for the customer based on product.', 'hexcoupon' ),
            'wrapper_class' => 'cart-condition',
        ));
    }
    
    // Save the checkbox value
    add_action( 'woocommerce_coupon_options_save', 'save_coupon_custom_option_usage_restriction', 10, 2 );
    function save_coupon_custom_option_usage_restriction( $post_id, $coupon ) {
        $coupon->update_meta_data( 'apply_cart_condition', isset($_POST['apply_cart_condition']) ? 'yes' : 'no' );
        $coupon->save();
    }
    
    // Move the checkbox just after "products" field
    add_action('admin_footer', 'apply_cart_condition_js');
    function apply_cart_condition_js() {
        global $pagenow, $typenow;
        if ( in_array($pagenow, array('post-new.php', 'post.php')) && 'shop_coupon' === $typenow ) :
        ?>
        <script>
        jQuery(function($){
            const cartConditionCB = $('.cart-condition').prop("outerHTML");
            $('.cart-condition').remove();
            $('select[name^="product_ids"]').parent().after(cartConditionCB);
        });
        </script>
        <?php
        endif;
    }
    

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

    You will get:

    enter image description here