2017-08-09 3 views

答えて

1

これはwoocommerce_loop_add_to_cart_linkフィルターフックに引っかけ、このカスタム関数を使用することが可能であり、この方法:

// Shop pages: we replace the button add to cart by a link to the main category archive page 
add_filter('woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2); 
function custom_text_replace_button($button, $product) { 
    // Get the product categories IDs 
    $category_ids = $product->get_category_ids(); 

    // return normal button if no category is set or if is not a shop page 
    if(empty($category_ids) || ! is_shop()) return $button; 

    // Iterating through each product category 
    foreach($product->get_category_ids() as $category_id){ 
     // The product category WP_Term object 
     $term_obj = get_term_by('id', $category_id, 'product_cat'); 
     // Only for first main category 
     if($term_obj->parent == 0){ 
      break; // we stop the loop 
     } 
    } 

    // The custom button below 
    $button_text = __("Visit", "woocommerce") . ' ' . $term_obj->name; 
    $button_link = get_term_link($term_obj->slug, 'product_cat'); 
    return '<a class="button" href="' . $button_link . '">' . $button_text .'</a>'; 
} 

この

は私が達成したいものの一例ですコードはあなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイル、またはすべてのプラグインファイルにあります。

このコードはテストされ3+

+1

ありがとうwoocommerceバージョンで動作され、これは私のような初心者のために多くのことができます:) – Squish

+0

サイド注:これは、テーマのすべてのタイプでそれらのいくつかを動作しません。少し調整が必要です:) – Squish

+0

@スノーバット。この回答に編集を追加することができれば(最後に)、私はそれを受け入れます...すべての設定、設定、および特殊性に合わせて、すべてのカスタマイズされたテーマで動作するようにするのは難しいでしょう。ありがとう – LoicTheAztec

関連する問題