I'm using the below function which currently accepts a single (or array of) static coupon codes. How can I change this to work with any valid (i.e. not already used the maximum times etc.) coupon code that exists in the site?
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'gifts' ); // Category ID or slug of targeted category
// NEED TO EDIT THIS PART TO GET THE LIST OF VALID CODES
$coupon_code = 'testing1'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
Edit:
Further to the comments and information from this link, I've tried the below code but this isn't working:
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'gifts' ); // Category ID or slug of targeted category
$coupon_code = array();
foreach ( $coupons as $coupon ) {
$coupon_name = $coupon->post_title;
array_push( $coupon_code, $coupon_name );
}
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
As wooCommerce already check for coupons validity when they are applied, If they are not valid it displays different warning messages depending on the reason for invalidity, removing invalid applied coupons??�
So you don't need to get all valid coupons in your code??� You just need the following:
add_action( 'woocommerce_check_cart_items', 'mandatory_redemption_coupon_code_for_gifted_items' );
function mandatory_redemption_coupon_code_for_gifted_items() {
$product_categories = array( 'gifts' ); // Term ID, slug or name of targeted category
$applied_coupons = WC()->cart->get_applied_coupons();
// When no coupon has been applied
if( empty($applied_coupons) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ){
// Check for gifted items
if( has_term( $product_categories, 'product_cat', $item['product_id'] ) ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Now if the required coupons needs to be specific, you will have make a custom SQL query to check if any applied coupon is valid for redemption.
Note: on your Edit, the coupon query is missing, so that's why it doesn't work.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.