cjocode
Forum Replies Created
-
Forum: Plugins
In reply to: [YITH WooCommerce Gift Cards] Paying balance with gift cardThank you for this suggestion. I can see how this would be a good solution, however I do have a requirement to apply a gift card to the balance order when the customer goes to pay for it.
Consequently, I have added custom code to the top of the order-pay screen (using the before_woocommerce_pay_form action hook). This displays a text box and button for the customer to enter their gift card code. The button makes an ajax call to a PHP function passing the gift card code and the order ID. The PHP function verifies the nonce for security, extracts the order id and gift card code from the request and applies to the order using the following:
// The $_REQUEST contains all the data sent via AJAX from the Javascript call
if ( isset($_REQUEST) )
{
//Get the voucher code and orderID from the request
$code = $_REQUEST['code'];
$orderid = $_REQUEST['order_id'];
if (isset($code) && isset($orderid))
{
//Get the order
$order = wc_get_order($orderid);
//Retrieve the gift card
$giftcards = YITH_WooCommerce_Gift_Cards::get_instance();
$giftCard = $giftcards->get_gift_card_by_code($code);
//Check if the gift card is usable
if ($giftCard && $giftCard->can_be_used())
{
// Apply voucher
if ($order)
{
$order->apply_coupon( $code );
//If order total is now 0, complete the order.
if ($order->get_total() == 0)
{
$order->update_status( 'completed' );
//wp_safe_redirect( $order->get_checkout_order_received_url() );
$message = "Gift card (" . $code . ") has been applied to order " . $orderid . " Order total is now " . $order->get_total() . " and status is " . $order->get_status();
$action = "redirect";
$result = build_voucher_success_response($order->get_checkout_order_received_url(), $message, $action);
}
else
{
$message = "Gift card (" . $code . ") has been applied to order " . $orderid . " Order total is now " . $order->get_total() . " and status is " . $order->get_status();
$action = "paybalance";
$result = build_voucher_success_response("", $message, $action);
}
}
}
else
{
//Gift card is not usable, return an error
$result = build_voucher_error_response("Voucher/Gift Card is not valid");
}
}
}
function build_voucher_success_response($redirect, $message, $action)
{
$response = '
{
"result" : "success",
"action" : "' . $action . '",
"redirecturl" : "' . $redirect . '",
"message" : "' . $message . '"
}';
return $response;
}
function build_voucher_error_response($error)
{
$response = '
{
"result" : "error",
"error" : ' . $error . '
}';
return $response;
}The javascript function in the order-pay page then parses the response. The javascript is below:
jQuery(document).ready(function($) {
//Get the order ID from the URL path
var path = window.location.pathname;
var pathparts = path.split('/');
var orderid = pathparts[3];
//Locate the apply-voucher button and hook into it's click event
$( "#my_button" ).click(function() {
// Get voucher code
var code = $("#my_input_box").val();
$.ajax({
url: my_ajax_object.ajax_url, // AJAX URL
type: 'post',
data: {
action:'apply_gift_card', // This is the name of the PHP function
nonce: my_ajax_object.nonce, // This is the nonce
code: code, // This is the gift card code I'm sending via AJAX
order_id: orderid // This is the order ID I'm sending via AJAX
},
success:function(data)
{
try
{
//If the result is success and the action is redirect, do the redirect
const parsedData = JSON.parse(data);
if (parsedData != null)
{
if (parsedData.result == "success" && parsedData.action == "redirect")
{
//Redirect to the order received page
window.location.replace(parsedData.redirecturl);
}
else if (parsedData.result == "success" && parsedData.action == "paybalance")
{
//Reload the current page to reflect the changed balance
window.location.reload();
}
}
}
catch (error)
{
console.log(error);
}
},
error: function(errorThrown){
window.alert(errorThrown);
}
});
});
});I have run a number of tests on this and it appears to be working. Is there anything in the above solution which I should be concerned about?
- This reply was modified 11 months ago by cjocode.
Many thanks for you response. I have investigated all of the suggestions above and cannot see an obvious cause. There is very little custom scripting on the site and nothing I can see which customizes the checkout. I do not have any caching or optimization plugins. All other plugins are up-to-date. I have tested multiple scenarios, including using BrowserStack to test from different devices and I am unable to replicate the issue. The original problem was with 3 orders over a space of 4 days, but I have not observed any occurrences since.
What I really need is a more detailed log which tells me at exactly which point the failure occurred. I cannot even see exactly which piece of code passed the data to stripe and therefore where the error could have occurred.
For now I cannot do much more than continue to monitor this, but if there are any further occurrences then I will need to look into it further.
Thanks Again