2017-12-03 124 views
2

我需要隐藏定义产品类别产品的“添加到购物车”按钮。移除添加到购物车在WooCommerce中选择产品类别的按钮3

我想数量fied仍然可见,因为我使用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

嘿LoicTheAztec。您的代码完美无瑕。我还有一个问题,我相信不仅能帮助我,还能帮助其他人解决同样的问题。我试图使用“产品附加件”,但是这段代码使得产品附加件也消失了。我一直在研究查看代码,并不知道为什么这会导致附件被删除。 “加入购物车”按钮和产品加载项之间必须存在某种关系。如果您感觉如此倾向,请随时与我们联系。谢谢 –

相关问题