ok so I am trying to make an auto redeem page like the one in this link: https://www.sitepoint.com/creating-a-woocommerce-redeem-coupon-page/
and it’s going great: everything else is working perfectly.. except one thing!
When a user inserts a not valid coupon it should say underneath the field to retry with a valid coupon but that doesn’t happen. All the following codes are in functions.php of my theme.
This is what the guy posted on that link.
——————–
// Create an instance of WC_Coupon with our code
$coupon = new WC_Coupon( $code );
if( ! $coupon->id && ! isset( $coupon->id ) ) {
// Build our response
$response = array(
'result' => 'error',
'message' => 'Invalid code entered. Please try again.'
);
header( 'Content-Type: application/json' );
echo json_encode( $response );
// Always exit when doing ajax
exit();
———————–
it looks like the “if statement” just never happens,
so i tried to change it like this:
———————–
// Create an instance of WC_Coupon with our code
$coupon = new WC_Coupon( $code );
// Check coupon to make determine if its valid or not
if( ! $coupon->is_valid() ) {
exc exc
———————–
and now it looks like $coupon->is_valid() is always false
so it doesn’t even see when a coupon exist or not!
in both cases $coupon->is_valid() will give false
what am i missing?
I don’t think u need the big picture of what I am doing! i just want a statement in functions.php of my theme that return true only if a coupon is actually valid. I’ll type ! in front of it and the job will be done. Please help me ??