2017-09-21 69 views
0

在WooCommerce我想替換添加到購物車按鈕,並將其更改爲「更多」但僅限於某些產品類別Woocommerce - 刪除添加到購物車的按鈕功能,並更改按鈕文字

我已經嘗試過使用相當多的不同代碼片段,但沒有任何東西似乎給我我想要的結果。

原因是我想使用我店的某些部分作爲顯示商品和價格的產品目錄,但不允許用戶購買它們。

有沒有人知道如何/如果這是可能的?

任何幫助都將不勝感激。

+0

請做一些更多的研究,有很多目錄模式插件,有可以爲你做這個。 – Kodaloid

回答

0

這裏有一些功能,它將取代一些定義的產品類別只

  1. 在店鋪和檔案網頁:按鈕被鏈接添加到購物車的產品
  2. 在單品頁:通過您的自定義按鈕添加到購物車按鈕,批量

你將不得不限定在功效:

  1. 陣列中的產品類別(2次)
  2. 的「更多」按鈕的產品鏈接(簡單的產品頁)

下面是代碼:

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

    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; 
} 


// Button replacement function for Single product pages (SET the link below) 
function custom_button_read_more($product_link){ 
    global $product; 

    // Set HERE your product link 
    $product_link = home_url('/some-link-to-define/'); 

    $button_text = __('Read more', 'woocommerce'); 
    echo '<a href="'.$product_link.'" class="button">'.$button_text.'</a>'; 
} 


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

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

    if(has_term($categories, 'product_cat', $product->get_id())){ 

     // 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); 

      // The button replacement 
      add_action('woocommerce_single_variation', 'custom_button_read_more', 20); 
     } 
     else // For all other product types 
     { 
      // Removing add to cart button and quantities 
      remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

      // The button replacement 
      add_action('woocommerce_single_product_summary', 'custom_button_read_more', 30); 
     } 
    } 
} 

代碼會出現在您的活動子主題(或主題)的function.php文件中,或者也存在於任何插件文件中。

所有代碼都在Woocommerce 3+上測試並正常工作。


類似的答案:Customize "Add to cart" button for a specific product category in WooCommerce

+0

嘿@LoicTheAztec,對不起,因此延遲嘗試此碼。但是,我剛剛測試了它(在woocommerce 3.1.2),它不工作。有任何想法嗎?目前我的fuctions.php中粘貼https://hastebin.com/dimurugoyo.php –

+0

@LBlakemore我真的不知道爲什麼不能爲你工作...可能是你的主題已經有一些定製...我已經重新測試過代碼,它完全適合我在2個不同的測試服務器(WC 3.1.2)... – LoicTheAztec

相關問題