在 WooCommerce 單個產品頁面上,通過高級自定義欄位使用 true/false,我有一個標簽,它以文本形式顯示“精選”術語并且作業正常。
這個...
add_action( 'woocommerce_before_single_product_summary', 'property_main_details_field', 1 );
function property_main_details_field() {
if ( get_field('featured_property') == 1 ) {
?>
<span class="property-contract-badge">Featured</span>
<?php
}
}
但是,考慮到 WooCommerce 已經內置了一個特色產品選擇,我想最小化并用 WooCommerce 的真/假 ID 替換自定義欄位。
如果我沒看錯,特色產品現在由 product_visibility 自定義分類法處理,用于 WooCommerce 中的特色術語。
如果是這樣,我可以使用 Tax Query 然后echo $term->name;在 span 標簽中嗎?
<span class="property-badge">
<?php
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
?>
</span>
uj5u.com熱心網友回復:
有幾種選擇。其中之一是使用wc_get_featured_product_ids(),它回傳一個包含特色產品 ID 的陣列。
如果 productID 存在于陣列中,則它是特色產品。
function action_woocommerce_before_single_product_summary() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get productID
$product_id = $product->get_id();
// Returns an array containing the IDs of the featured products.
$featured_product_ids = wc_get_featured_product_ids();
// Checks if a value exists in an array
if ( in_array( $product_id, $featured_product_ids ) ) {
echo '<span >Featured</span>';
} else {
echo 'NOT featured';
}
}
}
add_action( 'woocommerce_before_single_product_summary', 'action_woocommerce_before_single_product_summary', 1 );
代碼位于活動子主題(或活動主題)的 functions.php 檔案中。在 Wordpress 5.8.1 和 WooCommerce 5.8.0 中測驗和作業
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/345942.html
標籤:php WordPress的 求购 产品 高级自定义字段
