I just created a custom plugin for WooCommerce, so I can add a new manual payment method, so far I already can get it running. But the problem I face right now is I get a 0 value.
I create a class that extend WC_Order()
, here is the code
class WC_Order_Extender extends WC_Order {
public function __construct( $order_id ) {
parent::__construct( $order_id );
$this->data['price_in_btc'] = 0.0;
print_r($this->data);
}
public function get_price_in_btc() {
return $this->get_prop( 'price_in_btc' );
}
public function set_price_in_btc( $value ) {
$this->set_prop( 'price_in_btc', wc_format_decimal( $value, 7 ) );
}
}
and here is when the function is called
public function process_payment( $order_id ) {
//$order = new WC_Order( $order_id );
$order_extended = new WC_Order_Extender( $order_id );
// get_price_in_btc() always return 0 in thankyou.php page
$order_extended->set_price_in_btc( $this->get_bitcoin_rate( $order_extended->get_total() ) );
// Mark as On-Hold (We're waiting for the payment)
$order_extended->update_status( 'on-hold', __( 'Awaiting for manual BTC payment ', 'wc-manual-btc-gateway' ) );
// Reduce item stocks
wc_reduce_stock_levels( $order_id );
// Clean up the cart
WC()->cart->empty_cart();
// Return thank you redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order_extended )
);
}
Here is the code to call price_in_btc
value in thankyou.php
page
$extended_order = new WC_Order_Extender( $order->get_id() );
echo $extended_order->get_price_in_btc(); // Always return 0
at first I thought because it return 0.00*****
coin, so it rounded to 0
, so I thought it's a problem with the decimal, so I add wc_format_decimal
when set the prop, but it's still return as 0, even the actual return bitcoin price is 0.0004***.
Please help, where is the mistake I made in my code?
EDIT
the data that stored in the extended $order
EDIT 2
The prove that price_in_btc()
function is returning a value the console.log()
and here is the code
public function get_bitcoin_rate( $total_price ) {
$cUrl = curl_init();
curl_setopt_array( $cUrl, array(
CURLOPT_URL => 'https://api.coindesk.com/v1/bpi/currentprice/IDR.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
)
) );
$data = json_decode( curl_exec( $cUrl ), true );
$err = curl_error( $cUrl );
curl_close( $cUrl );
echo "<script>console.log( '" . wc_format_decimal( $total_price / $data['bpi']['IDR']['rate_float'], 7 ) . "' );</script>";
return $total_price / $data['bpi']['IDR']['rate_float'];
}
and here is with the wc_format_decimal()
EDIT 3
I add $order_extended->save();
below the set_price_in_btc()
:
$order_extended->set_price_in_btc( $this->get_bitcoin_rate( $order_extended->get_total() ) );
$order_extended->save();
But it's still return a 0
value. Please help I don't know what to do.