在 WooCommerce 中为 WC_Order 添加自定义函数 [英] Add custom function to WC_Order in WooCommerce
问题描述
我刚刚为 WooCommerce 创建了一个自定义插件,所以我可以添加一种新的手动付款方式,到目前为止我已经可以运行它了.但我现在面临的问题是我得到了一个 0 值.
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.
我创建了一个扩展WC_Order()
的类,这是代码
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 )
);
}
这是在thankyou.php
页面中调用price_in_btc
值的代码
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
一开始我以为是因为返回0.00*****
币,所以四舍五入到0
,所以我以为是小数点的问题,所以我设置prop时添加wc_format_decimal
,但返回0,即使实际返回比特币价格为0.0004***.
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?
编辑
存储在扩展$order
编辑 2
证明price_in_btc()
函数返回一个值console.log()
这是代码
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'];
}
这里是 wc_format_decimal()
编辑 3
我在set_price_in_btc()
下面添加了$order_extended->save();
:
$order_extended->set_price_in_btc( $this->get_bitcoin_rate( $order_extended->get_total() ) );
$order_extended->save();
但它仍然返回一个 0
值.请帮助我不知道该怎么办.
But it's still return a 0
value. Please help I don't know what to do.
推荐答案
在谷歌上徘徊,寻找答案后,我终于有点放弃我目前的编码,删除订单扩展器
然后创建一个非常非常简单的代码,我不知道这样行不行.
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 );
这很好地解决了我的问题,尽管这样做有点愚蠢,而且在thankyou.php页面中,我只需要使用get_post_meta($order->get_id(),'_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 )
希望这个回答对你有用!干杯!:D
Hope this answer useful for you! Cheers! :D
这篇关于在 WooCommerce 中为 WC_Order 添加自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!