Fixed Bugs SG Paynow QR on Order Pay Woocommerce

Actually, the PayNow QR plugin works well when the user checks out using QR, the QR code displays the payment data correctly, but problems arise when, for example, the user uses the bank transfer payment method, then for example the user wants to change the payment type to PayNow QR on the pay order page. , the total value of the payment will be 0

Read too : Set on-hold PayNow SG Woocommerce

To solve this problem there are 2 things I did 

#1 Javascript

Paynow QR generates QR values ​​using javascript, the way it works is, javascript takes the value on the button then generates it and then displays it in qr form

Just change on function

$('#order_review .wc_payment_methods').on('change', function(){

I think the code on this function there might be an error, look the line how paynow qr get total, i changed this code


var order_total = $(".shop_table tr:last-child .product-total .amount").text().replace('$', '');

change it to


var order_total = $("#paynow-demo").attr("data");

#2 Add Condtional Code

Maybe costum javascript on sg-paynow-public.js is not enough. To generate QR Code, SG Paynow have template with value $template, when checkout page, value on $template is normally but after the order saved, the value on data button is lost when user change to paynow qr on order-pay url. For solve this probalem I have idea, I place two condition in function sg-paynow.php

public function payment_fields()

I take this method cause realized something, when iam go to order-pay page, total taken from WC()->cart->total;  

Condition before


$total = WC()->cart->total;
$date = date("Ymd", strtotime('+8 hours'));
$total_price = 0;
$d = ''; 
$k = ''; 
foreach ( $cart_items as $cart_item ) {
$product = wc_get_product( $cart_item['product_id'] );
$sku = $product->get_sku();
$price = $product->get_price();
//if cart->total error, use total_price
$total_price += $price;
//echo "SKU: $sku, Price: $price";
$d.=$price.' ';
$k.=$sku.' ';
}
					 
$template = '<p>'._e( $this->description, 'sg-paynow' ).'</p><div id="paynow_qr"></div><button type="button" id="paynow-demo" data="'.$total.'" uen="'.$this->paynow_uen.'" ref="'.$name.' '.$k.'" date="'.$date.'">Try it</button>';
			 	
echo $template;

Condition After


$total = WC()->cart->total;
$total_2 = '';
//when $total have value, use $total with cart total function
if(!empty($total)) {
	$total_2 = $total;
} else {
//but when $total nothing or null, get query from order-pay

$order_id = absint(get_query_var('order-pay'));
$order = wc_get_order($order_id);
$totals = $order->get_order_item_totals();
if (isset($totals['order_total'])) {
$total_with_currency = $totals['order_total']['value'];					
$total_2 = preg_replace('/[^\d.]/', '', $total_with_currency);
						
}
}

$date = date("Ymd", strtotime('+8 hours'));
// Handle SKUs
    $sku_list = '';
    $price_list = '';
    if (!empty($cart_items)) {
        foreach ($cart_items as $cart_item) {
            $product = wc_get_product($cart_item['product_id']);
            $sku = $product->get_sku();
            $price = $product->get_price();
            $sku_list .= $sku . ' ';
            $price_list .= $price . ' ';
        }
    } else {
        $order_items = $order->get_items();
        foreach ($order_items as $item_id => $item) {
            $product = $item->get_product();
            $sku = $product->get_sku();
            $price = $product->get_price();
            $sku_list .= $sku . ' ';
            $price_list .= $price . ' ';
        }
    }

    // Generate the template
$template = '<p>' . _e($this->description, 'sg-paynow') . '</p><div id="paynow_qr"></div><button type="button" id="paynow-demo" data="' . $total_2 . '" uen="' . $this->paynow_uen . '" ref="' . $name . ' ' . $sku_list . '" date="' . $date . '">Try it</button>';	 	
 echo $template;


0 Response to "Fixed Bugs SG Paynow QR on Order Pay Woocommerce"

Post a Comment

Komentar yang Anda kirim akan terlebih dahulu di moderasi oleh Admin