Cap Coupon

Home » Snippets » Cap Coupon
0

Created with:

Visibility: 

public

Creator: Abhishek

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: WP Plugin
 * Version: 1.0.0
 * Author: WP Turbo
 * Author URI: https://wpturbo.dev
 * Description: Custom WordPress plugin.
 * Text Domain: wpturbo
 */

// Enqueue admin scripts and styles
function wpturbo_enqueue_admin_scripts() {
    wp_enqueue_script( 'wpturbo-admin-script', plugin_dir_url( __FILE__ ) . 'admin-script.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'admin_enqueue_scripts', 'wpturbo_enqueue_admin_scripts' );

// Add capping amount field to coupon edit page
function wpturbo_coupon_capping_amount_field() {
    global $post;
    
    // Get the saved capping amount
    $capping_amount = get_post_meta( $post->ID, '_wpturbo_capping_amount', true );
    
    // Output the capping amount field
    echo '<div class="form-field">';
    echo '<label for="wpturbo-capping-amount">' . __( 'Capping Amount', 'wpturbo' ) . '</label>';
    echo '<input type="number" id="wpturbo-capping-amount" name="wpturbo_capping_amount" value="' . esc_attr( $capping_amount ) . '">';
    echo '</div>';
}
add_action( 'woocommerce_coupon_options', 'wpturbo_coupon_capping_amount_field' );

// Save capping amount field value
function wpturbo_save_coupon_capping_amount( $post_id ) {
    $capping_amount = isset( $_POST['wpturbo_capping_amount'] ) ? sanitize_text_field( $_POST['wpturbo_capping_amount'] ) : '';
    update_post_meta( $post_id, '_wpturbo_capping_amount', $capping_amount );
}
add_action( 'woocommerce_coupon_options_save', 'wpturbo_save_coupon_capping_amount' );

// Cap the coupon amount
function wpturbo_cap_coupon_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
    // Get the capping amount for the coupon
    $capping_amount = get_post_meta( $coupon->get_id(), '_wpturbo_capping_amount', true );
    
    // Cap the discount amount if capping amount is set
    if ( ! empty( $capping_amount ) && $discount > $capping_amount ) {
        $discount = $capping_amount;
    }
    
    return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'wpturbo_cap_coupon_amount', 10, 5 );
				

This code adds a capping amount field to the coupon edit page in the WooCommerce admin area. You can specify the maximum amount to which the coupon discount should be capped. When calculating the discount amount for a cart item, the code checks if the capping amount is set for the coupon and limits the discount to that amount if it exceeds it.

To use this code, simply replace the existing code in the 'wp-plugin.php' file with the provided code. Make sure you have the WooCommerce plugin installed and activated.

Please note that this code assumes you are using the WooCommerce plugin. Also, make sure to sanitize and validate the capping amount input value before saving it to the database to ensure security and prevent any potential issues.

Register an account to save your snippets or go Pro to get more features.