![](/img/trans.png)
[英]woocommerce should show same price for eu but net worth for other countries
[英]If else statement for EU countries in filter for WooCommerce breaks REST API
更新:通過將我的 object 包裝在這個 if 語句中,它不會中斷 API 調用:
if ( WC()->customer ) {
}
我使用的插件顯示所有價格相同,而不管 WooCommerce 中的增值稅。這很好用 - 但我只需要歐盟國家/地區。
該插件有一個過濾器:
add_filter('wc_aelia_tdbc_keep_prices_fixed', function($keep_prices_fixed): bool {
if() {
$keep_prices_fixed = true;
}
return $keep_prices_fixed;
});
到目前為止我得到了什么:
add_filter('wc_aelia_tdbc_keep_prices_fixed', function($keep_prices_fixed): bool {
$countries = new WC_Countries();
$eu_countries = $countries->get_european_union_countries();
if ( in_array( WC()->customer->get_billing_country(), $eu_countries ) ) {
$keep_prices_fixed = true;
} else {
$keep_prices_fixed = false;
}
return $keep_prices_fixed;
});
在前端這很有效——它只適用於歐盟國家的統一價格。 然而,它確實打破了 WooCommerce REST API 並出現錯誤:
Uncaught Error: Call to a member function get_billing_country() on null
我假設WC()->customer->get_billing_country()
object 未在 API 調用中初始化。
在訪問之前,如何檢查此 object 是否已初始化?
默認Woocommerce不會加載rest api中的用戶session數據,以提高性能。 為此,您需要初始化用戶 session,然后獲取客戶信息。
if ( null === WC()->session ) {
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
WC()->session = new $session_class();
WC()->session->init();
}
WC()->customer = new WC_Customer( get_current_user_id(), true );
echo WC()->customer->get_billing_country();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.