2017-07-19 6 views
1

旧バージョンのWooCommerceでget_id()メソッドをサードパーティのプラグインに追加することができるかどうかは誰にも知られているので、タグマネージャプラグイン?WooCommerceとの互換性 - サードパーティのプラグインで古いバージョンでget_id()メソッドを使用する

現時点ではプラグインを更新する必要はありません。

誰でも知ることができますか?

おかげ

答えて

1

はい、あなたはWC_ProductまたはWC_Orderオブジェクトなどの任意のWooCommerceオブジェクト上でPHP関数method_exists()を使用することができます...ここで

WC_Productオブジェクト$productと例です。

// WooCommerce retro-compatibility (compact) 
$product_id = method_exists($product, 'get_id') ? $product->get_id() : $order->id; 

また、PHP関数version_compare()も使用できます:

コンパクトな方法で
// WooCommerce retro-compatibility (2) 
if (version_compare(WC_VERSION, '3.0', '<')) { 
    // Older than 3.0 
    $product_id = $product->id; 
} else { 
    // 3.0 and above 
    $product_id = $product->get_id(); 
} 

と同じ:

$product_id = version_compare(WC_VERSION, '3.0', '<') ? $product->id : $product->get_id(); 

$productが既存であるとWC_Productオブジェクトを定義し...)

+0

だから、これは動作しますか? '$ product_id = method_exists($ order、 'get_id');'これの代わりに? '$ product_id = $ product-> get_id();'プロダクトは既に定義されていますか? –

+0

@ciaranpflanagan私は '$ order'ではなく' $ product'で答えを更新しました...そして、 '$ product'は定義された既存のWC_Productオブジェクト*(旧バージョンのWooCommerceと同じです)* ...あなたのコードにバージョンの互換性を与えます。あなたのコードはWC 2.6.xとWC 3+でも使えます – LoicTheAztec

+0

上記のコメントをクリアするには、製品varが既に定義されています。どうして私はなぜ? –

関連する問題