简体   繁体   中英

Add custom function to WC_Order in WooCommerce

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.

Have you also tried to replace wc_format_decimal with number_format ?

public function set_price_in_btc( $value ) {

        $this->set_prop( 'price_in_btc', number_format( $value, 7 ) );
    }

After wandering around across the google, and finding for some answer, finally I kinda give up with my curent coding, delete the order extender and then create a really-really simple codes, that I don't know is this OK or not.

For anyone that having some troubles the same with me and didn't found the answer, use this code:

add_action( 'woocommerce_checkout_create_order', 'wc_add_custom_meta_order', 20, 2 );

function wc_add_custom_meta_order( $order, $data ) {

    $order->update_meta_data( '_price_in_btc', $data );
}

For inserting the new custom meta data

$price_in_btc = number_format( $this->get_bitcoin_rate( $order->get_total() ), 5 );

before_checkout_create_order( $order, $price_in_btc );

this solves my problem very well even though it's kinda the stupid way to do it, and in the thankyou.php page I just need to call the meta directly using get_post_meta( $order->get_id(), '_price_in_btc', true )

Hope this answer useful for you! Cheers! :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM