2017-12-03 6 views
2

定義された商品カテゴリの商品の「カートに追加」ボタンを非表示にする必要があります。WooCommerce 3の選択された商品カテゴリのカートに入れるボタンを削除します。3

私は、Yithリクエストを使用しているため、依頼量がまだ見えるようにしたいと思います。見積もりシステムの数量を使用する引用プラグインです。

目的:数量フィールドを保持している特定の商品カテゴリの「カートに追加」ボタンを非表示にします。

私のfunctions.phpファイルに配置するコードの短い文字列を探しています。

答えて

1

更新:(シンプルな製品でWooCommerce製品を追加しました互換性アドオンプラグイン)。ここで

方法(定義された製品カテゴリのためのシンプルかつ可変の製品タイプ用)にある:アーカイブのページで

  • オプション、:リンクにより追加・トゥ・カートのボタンを交換してくださいボタンを押して製品に接続します。単一の製品ページに
  • :カートボタン(数量フィールドを維持する)

にコードを追加し削除します(アーカイブページ用)

// function add back quantities without button (variable product) 
function add_back_quantities_variable_products(){ 
    global $product; 

    ?> 
    <div class="woocommerce-variation-add-to-cart variations_button"> 
    <?php 

    do_action('woocommerce_before_add_to_cart_quantity'); 

    woocommerce_quantity_input(array(
     'min_value' => apply_filters('woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product), 
     'max_value' => apply_filters('woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product), 
     'input_value' => isset($_POST['quantity']) ? wc_stock_amount($_POST['quantity']) : $product->get_min_purchase_quantity(), 
    )); 

    do_action('woocommerce_after_add_to_cart_quantity'); 
    ?> 
     <input type="hidden" name="add-to-cart" value="<?php echo absint($product->get_id()); ?>" /> 
     <input type="hidden" name="product_id" value="<?php echo absint($product->get_id()); ?>" /> 
     <input type="hidden" name="variation_id" class="variation_id" value="0" /> 
    </div> 
    <?php 
} 


// function add back quantities without button (simple product) 
function add_back_quantities_simple_products(){ 
    global $product; 

    if (! $product->is_purchasable()) return; 

    echo wc_get_stock_html($product); 

    if ($product->is_in_stock()) : ?> 

     <?php do_action('woocommerce_before_add_to_cart_form'); ?> 

     <form class="cart" method="post" enctype='multipart/form-data'> 
      <?php 
       // For WooCommerce Product add-ons (Update) 
       do_action('woocommerce_before_add_to_cart_button'); 

       do_action('woocommerce_before_add_to_cart_quantity'); 

       woocommerce_quantity_input(array(
        'min_value' => apply_filters('woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product), 
        'max_value' => apply_filters('woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product), 
        'input_value' => isset($_POST['quantity']) ? wc_stock_amount($_POST['quantity']) : $product->get_min_purchase_quantity(), 
       )); 

       do_action('woocommerce_after_add_to_cart_quantity'); 
      ?> 
     </form> 

    <?php 
     do_action('woocommerce_after_add_to_cart_form'); 
    endif; 
} 


// Replacing add to cart button and quantities by your custom button in Single product pages 
add_action('woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0); 
function conditionally_replacing_template_single_add_to_cart() { 
    global $product, $post; 

    // Set HERE your product categories in the array 
    $categories = array('t-shirts', 'gloves'); 

    if(has_term($categories, 'product_cat')){ 

     // For variable product types 
     if($product->is_type('variable')){ 
      // Removing add to cart button and quantities 
      remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); 

      // Add back quantities without button 
      add_action('woocommerce_single_variation', 'add_back_quantities_variable_products', 20); 
     } 
     // For simple product types 
     else if($product->is_type('simple')) 
     { 
      // Removing add to cart button and quantities 
      remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

      // Add back quantities without button 
      add_action('woocommerce_single_product_summary', 'add_back_quantities_simple_products', 30); 
     } 
    } 
} 

、オプションを

// Replacing the button add to cart by a link to the product in Shop and archives pages 
// For variable and simple products 
add_filter('woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2); 
function replace_loop_add_to_cart_button($button, $product ) { 
    // Set HERE your product categories in the array 
    $categories = array('t-shirts', 'gloves'); 

    // Only for simple products 
    if(! $product->is_type('variable')) return; 

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

コードはあなたのactivのfunction.phpファイルに入りますeの子供のテーマ(またはテーマ)または任意のプラグインファイルに保存されます。

このコードはWooCommerce 3+の下でテストされ、動作します。

+0

返信いただきありがとうございます。アーカイブページに表示されるカートボタンに追加する必要がないため、商品ボタンへのリンクは必要ありません。顧客は個々の製品ページ内のカートに追加することしか見ることができません。それを含まないようにコードを単純化できますか?ありがとう! –

+1

回答と情報をありがとう。それは私のために働いて、私は店/アーカイブページでこれらのボタンを既に見ることができないので、最後の文字列を削除しました。私はすでに私の質問にコードで試したことを投稿するようになるので、人々はただ単に無料のコードを求めているとは思っていません。ありがとうございました! –

+0

Hey LoicTheAztec。あなたのコードは完璧に機能しました。私は、同じ問題を抱えているだけでなく、他の人たちを助けてくれるだろうともう一つ質問します。私は "製品アドオン"を使用しようとしていますが、このコード列は製品のアドオンも消滅させます。私はコードを調べて調査しています。なぜこれがアドオンを削除するのかを理解できません。 「カートに追加」ボタンと製品アドオンの間には何らかの関係がなければなりません。そういう気持ちがあれば、それ以上の助けをいただければ幸いです。ありがとう –

関連する問題