2017-06-03 12 views
2

function.phpには、ウェブサイトにアクセスしたときに自動的にIDでウーコム商品を追加する機能があります。functions.phpの言語を確認する[wpml]

ウェブサイトはバイリンガルであり、翻訳された商品IDを特定することはできません。

だから、私は、関数を実行し、フロントエンドの最初の言語を決定する、のfunctions.phpにWPML機能を追加することが可能であるかどうかを知りたい、このような何か:

<?php if(wpml_getLanguage()=='en'); ?> 
---do function for product 22--- 
<?php elseif(wpml_getLanguage()=='it'); ?> 
---do function for product 45-- 
<?php endif; ?> 

マイコード:

add_action('template_redirect', 'add_product_to_cart'); 
function add_product_to_cart() { 
    if (! is_admin()) { 
     $product_id = 22; 
     $found = false; 
     //check if product already in cart 
     if (sizeof(WC()->cart->get_cart()) > 0) { 
      foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
       $_product = $values['data']; 
       if ($_product->id == $product_id) 
        $found = true; 
      } 
      // if product not found, add it 
      if (! $found) 
       WC()->cart->add_to_cart($product_id); 
     } else { 
      // if no products in cart, add it 
      WC()->cart->add_to_cart($product_id); 
     } 
    } 
} 
+0

にドロップすることができます[関数にローカル言語に基づいてWordPressのヘッダにコードを追加します.php - WPML](https://stackoverflow.com/questions/43788386/adding-a-code-to-wordpress-header-based-on-local-language-into-functions-php) – LoicTheAztec

答えて

2

ネイティブのWPML変数ICL_LANGUAGE_CODEを使用して簡単に取得できます。

次のページでこのトピックの詳細情報を見つけることができます: https://wpml.org/documentation/support/wpml-coding-api/

+0

私はすでにやっている将来この話題を見る人には完全な答えではありません。よろしくお願いします。 –

0

これはのfunctions.php

add_action('init', 'add_product_on_language'); 

function add_product_on_language(){ 
    if (! is_admin()) { 

     if(ICL_LANGUAGE_CODE == 'en'){ 
      $product_id = 22; 
     } elseif (ICL_LANGUAGE_CODE == 'it') { 
      $product_id = 45; 
     } 

     $found = false; 
     //check if product already in cart 
     if (sizeof(WC()->cart->get_cart()) > 0) { 
      foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
       $_product = $values['data']; 
       if ($_product->id == $product_id) 
        $found = true; 
      } 
      // if product not found, add it 
      if (! $found) 
       WC()->cart->add_to_cart($product_id); 
     } else { 
      // if no products in cart, add it 
      WC()->cart->add_to_cart($product_id); 
     } 
    }  
} 
+0

私はこのコードを知っていますが、functions.phpはそれをサポートしていません... –

+0

@ Nick_n1 functions.phpに落とせるようにコードを調整しました – alev

関連する問題