我已經嘗試了大約三天來獲取我的 woocommerce 產品屬性 slug 中的 4 個,而不是在產品下方顯示的名稱。
到目前為止,我一直在使用這段代碼,它似乎完全符合我的要求,除了采用屬性名稱而不是值。
/**
* Display available attributes.
*
* @return array|void
*/
function iconic_available_attributes() {
global $product;
if ( ! $product->is_type( 'variable' ) ) {
return;
}
$attributes = iconic_get_available_attributes( $product );
if ( empty( $attributes ) ) {
return;
}
foreach ( $attributes as $attribute ) {
?>
<div class="iconic-available-attributes">
<p class="iconic-available-attributes__title"><?php _e( 'Available', 'iconic' ); ?> <strong><?php echo $attribute['name']; ?></strong></p>
<ul class="iconic-available-attributes__values">
<?php foreach ( $attribute['values'] as $value ) { ?>
<li class="iconic-available-attributes__value <?php echo $value['available'] ? '' : 'iconic-available-attributes__value--unavailable'; ?>"><?php echo $value['name']; ?></li>
<?php } ?>
</ul>
</div>
<?php
}
}
/**
* Get available attributes.
*
* @param WC_Product_Variable $product
*
* @return array
*/
/**
* @snippet Display Custom Products Attributes on the Products Page
*/
function cw_woo_attribute(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$display_result = '';
foreach ( $attributes as $attribute ) {
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
$cwtax = $terms[0]->taxonomy;
$cw_object_taxonomy = get_taxonomy($cwtax);
if ( isset ($cw_object_taxonomy->labels->singular_name) ) {
$tax_label = $cw_object_taxonomy->labels->singular_name;
} elseif ( isset( $cw_object_taxonomy->label ) ) {
$tax_label = $cw_object_taxonomy->label;
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$display_result .="<span class='attribute'>" . $tax_label . "</span>";
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
array_push( $tax_terms);
}
$display_result .= implode(', ', $tax_terms);
} else {
$display_result .= $name;
$display_result .= esc_html( implode( ', ', $attribute->get_options() ) );
}
}
echo "<span class='attributeline'>" . "| " . "</span>" . $display_result;
}
add_action('woocommerce_shop_loop_item_title', 'cw_woo_attribute', 25);
我在任何方面都不是 PHP 編碼員,所以我一直在努力讓它作業。
這是當前情況的示例,顯示名稱:“植物型別”而不是值:“年度”。https://i.stack.imgur.com/VVYmF.png
期待您的回復,以便我繼續處理商店的其余部分!
uj5u.com熱心網友回復:
你能檢查一下這個現有的答案嗎?
Wordpress Woocommerce 在商店頁面上顯示屬性
我剛剛在最近的 Woocommerce 安裝中對其進行了測驗,接受的答案似乎仍然可以正常作業。
看看你是否可以通過預先定義你想要顯示的屬性來讓它作業,就像他們在那個問題中所做的一樣:pa_country、pa_class 等。你可以在下面的部分中看到它。
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class','pa_faction', 'pa_gender' );
如果您不想預定義屬性,您仍然可以像下面一樣獲取它們(也經過測驗)。但是對于測驗,僅使用預定義的字串可能會有所幫助,因此您可以確定它正在作業。
$attributes = $product->get_attributes();
$attrs = [];
foreach($attributes as $key => $attribute) :
$attrs[] = $key;
endforeach;
// just replace the array with attribute names with $attrs
$product_attribute_taxonomies = $attrs;
uj5u.com熱心網友回復:
將以下代碼片段添加到functions.php 以在商店存檔的產品下顯示逗號分隔的術語名稱字串。
add_filter( 'woocommerce_after_shop_loop_item_title', 'loop_display_attr', 15 );
function loop_display_attr() {
global $product;
// The attribute slug
$attribute = 'attr';
// Get attribute term names in a coma separated string
$term_names = $product->get_attribute( $attribute );
// Display a coma separted string of term names
echo '<p>' . $term_names . '</p>';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/366686.html
標籤:php WordPress的 求购 属性 产品
下一篇:在帖子標題前顯示特色圖片
