2017-08-11 3 views
1

私のWooCommerceストアでは、米国でのみ利用可能ないくつかの製品があります。シングル商品ページの[カートに追加]ボタンを非表示にしたいジオロケーションは、米国以外の国を検出します。ユーザー国および製品IDに基づいてカートに追加ボタンをカスタマイズする

それがアメリカであれば、ショーは「「買い物かごに入れる、それは米国ではない場合、非表示 『買い物かごに入れる』ボタンをクリックします。

(PS私はそのためのプラグインを使用しますが、それはのために働いていません

+0

あなたの質問はあまりにも曖昧で動作します。あなたのページにいくつかのコード、スクリーンショット、またはリンクを提供してください。 – vagelis

+0

こんにちは、上記の私の質問を編集しました、あなたは見てみることができますか、私はどのようなコードを子供のテーマで私のfunctions.phpで使用するか分かりません。 – Tanvir

+0

あなたは単一の商品ページクラス(またはそれが米国であることを示すもの)とボタン「カートに追加」のIDまたはクラスを提供できますか? – vagelis

答えて

2

条件が一致すると(特定の商品IDなど)、このコードはショップのアーカイブにあるカートに追加ボタンを商品へのリンクと、商品ページへのリンクで置き換えます(別のプラグインとの競合カスタムテキスト。

あなただけで設定する必要があります。

  • 第一機能に必要な国コード...(あなたのための「米国」とまだ行って)
  • 2番目と3番目の機能配列
であなたの製品IDここで

はコードです:

// Conditional function country code detection 
// @argument $code (string): country code restriction 
// @return boolean 
if(! function_exists('is_from_country_code')){ 

    // ==> HERE below set your country code (Replace 'US' by yours) 
    function is_from_country_code($code = 'US'){ 
     $location = new WC_Geolocation; 
     $user_ip = $location->get_ip_address(); 
     $user_country_code = $location->geolocate_ip($user_ip, false, false)['country']; 
     return $user_country_code == $code ? true : false; 
    } 
} 

// Replace "Add to cart" single product button and quantity by a custom text 
add_action('woocommerce_single_product_summary', 'Custom_single_add_to_cart', 1); 
function Custom_single_add_to_cart(){ 
    global $product; 

    // Set here your product IDS 
    $product_ids = array(56, 53, 50); 

    if(is_from_country_code() || 
     (! is_from_country_code() && ! in_array($product->get_id(), $product_ids))) 
     return; // Continue for foreign countries and the specific products IDs 

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 
    add_action('woocommerce_single_product_summary', function(){ 
     echo '<p class="custom-text">'.__('Not available for your country', 'woocommerce').'</p>'; 
    }, 30); 
} 

// Shop and archives pages: we replace the button add to cart by a link to the product 
add_filter('woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2); 
function custom_text_replace_button($button, $product ) { 

    // Set here your product IDS 
    $product_ids = array(56, 53, 50); 

    if(is_from_country_code() || 
     (! is_from_country_code() && ! in_array($product->get_id(), $product_ids))) 
     return $button; // Continue for foreign countries and the specific products IDs 

    $button_text = __("View product", "woocommerce"); 
    return '<a class="button" href="'.$product->get_permalink().'">'.$button_text.'</a>'; 
} 

コードは、あなたのfunction.phpファイルに行きますアクティブな子のテーマ(またはテーマ)、またはすべてのプラグインファイルに保存されます。

このコードは、woocommerceバージョンでテスト3+と

+0

私の子供のテーマのfunction.phpにそれらを追加する必要がありますか非常にありがとう? – Tanvir

+0

こんにちは、そのアメリカやアメリカ以外のすべての国で働いています。カートに追加されていても、米国でも表示されていませんが、plzをもう一度チェックしてもかまいません。 – Tanvir

+0

@タンビル...テストされて解決されました(3)... ...それを試してみてください:) – LoicTheAztec

関連する問題