1
Woocommerceでは、各商品ボタンは、商品画像、商品名、価格、カートに追加ボタンのデフォルトになります。しかし、私は各製品ボタンの中にどのカテゴリが属するのか(カテゴリページにユーザーを誘導する)を追加したいと思います。WordPress WooCommerceはすべての商品ボタンに商品カテゴリを表示します
どうすればいいですか?
Woocommerceでは、各商品ボタンは、商品画像、商品名、価格、カートに追加ボタンのデフォルトになります。しかし、私は各製品ボタンの中にどのカテゴリが属するのか(カテゴリページにユーザーを誘導する)を追加したいと思います。WordPress WooCommerceはすべての商品ボタンに商品カテゴリを表示します
どうすればいいですか?
これは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+
ありがとうwoocommerceバージョンで動作され、これは私のような初心者のために多くのことができます:) – Squish
サイド注:これは、テーマのすべてのタイプでそれらのいくつかを動作しません。少し調整が必要です:) – Squish
@スノーバット。この回答に編集を追加することができれば(最後に)、私はそれを受け入れます...すべての設定、設定、および特殊性に合わせて、すべてのカスタマイズされたテーマで動作するようにするのは難しいでしょう。ありがとう – LoicTheAztec