Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Woocommerce: when code applied auto add linked products into cart WordPress

  • SOLVED

Hi,

Following my previous question: http://wpquestions.com/question/showChronoLoggedIn/id/16702

<strong>I would like than when a visitor use that form to enter a coupon, he is redirected to the cart page with the linked product auto added in the cart https://cloudup.com/cA0scOlA4wS</strong>
<em>Please use rempty answer from previous question as basis if possible.
</em>

I founded 2 articles that could help :
http://www.sitepoint.com/creating-a-woocommerce-redeem-coupon-page/
http://wordpress.ahlekhabar.com/support/topic/auto-add-discounted-item-to-cart

Thank you,

Answers (2)

2016-03-22

Reigel Gallarde answers:

change function rempty_coupon_page to this...

function rempty_coupon_page() {

$code = $_REQUEST['coupon_code'];
if( empty( $code ) || !isset( $code ) ) {
$response = array(
'result' => 'error',
'message' => 'Code text field can not be empty.'
);

header( 'Content-Type: application/json' );
echo json_encode( $response );

exit();

}

$coupon = new WC_Coupon( $code );

if( !$coupon->is_valid() ) {
$response = array(
'result' => 'error',
'message' => 'Invalid code entered. Please try again.'
);

header( 'Content-Type: application/json' );
echo json_encode( $response );
exit();

} else {

global $woocommerce;

$woocommerce->cart->add_discount( $code );

foreach( $coupon->product_ids as $prod_id ) {
$woocommerce->cart->add_to_cart( $prod_id );
}

$response = array(
'result' => 'success',
'href' => $woocommerce->cart->get_cart_url()
);

header( 'Content-Type: application/json' );
echo json_encode( $response );
exit();

}

}


gorbatch comments:

Thank you both for your answers, I doubled the price pool.

2016-03-23

Rempty answers:

Hello Gorbatch, sorry for late response please replace my function:

function rempty_coupon_page() {
$code = $_REQUEST['coupon_code'];
$coupon = new WC_Coupon( $code );
global $woocommerce;
if(!empty($coupon->product_ids)){
foreach( $coupon->product_ids as $prod_id ) {
$woocommerce->cart->add_to_cart( $prod_id );
}
}
if( empty( $code ) || !isset( $code ) ) {
$response = array(
'result' => 'error',
'message' => 'Code text field can not be empty.'
);
header( 'Content-Type: application/json' );
echo json_encode( $response );
exit();
}

if( !$coupon->is_valid() ) {
$response = array(
'result' => 'error',
'message' => 'Invalid code entered. Please try again.'
);
header( 'Content-Type: application/json' );
echo json_encode( $response );
exit();

} else {
$woocommerce->cart->add_discount( $code );
$response = array(
'result' => 'success',
'href' => $woocommerce->cart->get_cart_url()
);
header( 'Content-Type: application/json' );
echo json_encode( $response );
exit();
}
}


And the js function

jQuery( document ).ready( function() {
jQuery( '#ajax-coupon-redeem input[type="submit"]').click( function( ev ) {
var code = jQuery( 'input#coupon').val();
data = {
action: 'rempty_coupon_page',
coupon_code: code
};
jQuery.post( woocommerce_params.ajax_url, data, function( returned_data ) {
if( returned_data.result == 'error' ) {
jQuery( 'p.result' ).html( returned_data.message );
} else {
window.location.href = returned_data.href;
}
});
ev.preventDefault();
});
});


Will be nice if you rise a bit the price pool ;)


gorbatch comments:

Thank you both for your answers, I doubled the price pool.