簡體   English   中英

如果產品屬於 WooCommerce 中的某個類別,則在存檔頁面的價格下方添加自定義文本

[英]Add custom text below price on archive pages if product is in a certain category in WooCommerce

我正在嘗試在某個類別(或兩個類別)內的任何產品的價格下方(不是在單個產品頁面上)添加自定義消息。

這是我已經走了多遠:

add_action( 'woocommerce_after_shop_loop_item', 'custom_text', 5 );
function custom_text() {
global $product;

if ( is_product_category( 'swim-trunks' ) ) {  

echo '<span class="custom-text">Buy Any 3 Swim Trunks For $99</span>';
}
}

但這僅適用於類別 slug,這意味着它僅適用於該特定類別,並且不考慮該產品是否也屬於另一個類別。 最后,它不適用於主頁或相關產品的產品滑塊。

編輯:

所以,我在這里更新了代碼,這似乎有效:

/* Begin Custom Text below price on shop page */
add_action( 'woocommerce_after_shop_loop_item', 'custom_text', 5 );
function custom_text() {
global $product;

if ( has_term( 76, 'product_cat' ) || has_term( 71, 'product_cat' )) { 

    echo '<span class="custom-text">Buy Any 3 Swim Trunks For $99</span>';
}
}
/* End Custom Text below price on shop page */

如果有人有更好的解決方案,我很樂意聽到!

您可以使用 acf 字段或 metabox 或自定義字段 woocommerce。 您需要做的就是將其掛到位。 例如woocommerce_after_shop_loop_item

您可以在產品單頁上嘗試這個用於自定義測試的產品價格 -

add_filter( 'woocommerce_get_price_html', 'woo_add_text_after_price_single_product', 20, 1 );

function woo_add_text_after_price_single_product($price){

  // Your custom text
  $custom_text = '<span style="color: #ff0000;">My Custom Text Here...</span>';

   return $price.'<br>'.$custom_text;
}

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM