我需要检查WooCommerce是否处于活动状态,并检查其版本号,以使其在新旧版本中的工作方式有所不同。
我发现WC_VERSION
存储了版本号,因此,如果存在版本号,则也意味着WooCommerce处于活动状态。问题在于,只有在加载插件后,它才能返回任何内容。我想检查版本,然后woocommerce_ver_check function
在一般范围之外使用。我可以没有全局变量吗?
我想做这样的事情,但woocommerce_ver_check()
始终为null,因为它在WooCommerce加载后执行,这与未分配给任何钩子的其余代码不同:
// Check WooCommerce version.
add_action('plugins_loaded', 'woocommerce_ver_check');
function woocommerce_ver_check() {
if (defined('WC_VERSION')) return WC_VERSION;
}
// Only if WooCommerce is active.
if (! woocommerce_ver_check() == null ) {
if ( version_compare( WC_VERSION, '3.0', '>=' ) ) {
// new version code
} else {
// old version code
}
}
您也可以尝试使用以下内容(用于WooCommerce第三方插件):
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
// Old version code (example)
$product_id = $cart_item['data']->id
} else {
// New version code (example)
$product_id = $cart_item['data']->get_id();
}
}
对于新的WooCommerce 3 CRUD对象方法,您还可以使用method_exists()
,例如:
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句