[英]Woocommerce: Display Product Variation Description on shop catalog page
我在這里提到了類似的問題,我希望在目錄頁面上顯示變量產品的簡短描述。
我正在使用WooCommerce Show Single Variations商業插件,但未顯示簡短說明。
在此之前,我曾使用一些代碼在可以正常工作的商店頁面上顯示簡單產品的描述,如下所示:
add_action( 'woocommerce_after_shop_loop_item_title',
'add_short_description', 9 );
function add_short_description() {
global $post;
$text = $post->post_excerpt;
$maxchar = 75; //максимальное кол-во символов
$text = preg_replace ('~\[[^\]]+\]~', '', $text ); //убираем шорткоды
//удаляем все html символы
//$text = strip_tags( $text);
// Обрезаем
if ( mb_strlen( $text ) > $maxchar ){
$text = mb_substr( $text, 0, $maxchar );
$text = preg_replace('@(.*)\s[^\s]*$@s', '\\1 ...', $text );
}
echo "<span class='catalog-short-desc'>$text</span>";
}
如果您告訴我如何更改此代碼,我將不勝感激。
已更新 :由於我沒有,所以我不使用您的商業插件,並且您的問題不太清楚……有2種可能性:
1)與其他產品一樣,您想為在woocommerce存檔頁面(如shop…)上顯示(帶有插件)的產品變體添加簡短描述(來自父變量產品)。
這應該可以解決問題(沒有任何錯誤的嘗試) :
add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
global $post;
// 1. Product variation
if ($post->post_type = 'product_variation'){
$post_parent_id = $post->post_parent; // Get the parent variable product ID
$post_parent = get_post( $post_parent_id ); // Get the parent variable WP_Post object
$text = $post_parent->post_excerpt; // Get the short description from the parent variable product
}
// 2. Other cases (simple, variable …)
else {
$text = $post->post_excerpt;
}
$maxchar = 75; // Maximum number of characters
$text = preg_replace ('~\[[^\]]+\]~', '', $text ); // Remove shortcodes
// Remove all html symbols
//$text = strip_tags( $text);
// Crop
if ( mb_strlen( $text ) > $maxchar ){
$text = mb_substr( $text, 0, $maxchar );
$text = preg_replace('@(.*)\s[^\s]*$@s', '\\1 ...', $text );
}
echo "<span class='catalog-short-desc'>$text</span>";
}
2)與其他產品一樣,您想添加在woocommerce存檔頁面(如shop…)上顯示的產品變型的描述(使用您的插件)… 對於產品變型,不存在簡短描述(為空) …
這應該可以解決問題(沒有任何錯誤的嘗試) :
// For Woocommerce versions 3+ only
add_action( 'woocommerce_after_shop_loop_item_title', 'add_short_description', 9 );
function add_short_description() {
global $post, $product;
// 1. Product variation
if ( $product->is_type('product_variation') ){
// Get the product variation description
// short description doesn't exist for product variations
$text = $product->get_description();
// If the product variation description is empty, we get the parent short description
if( empty( $text ) ){
$parent_id = $product->get_parent_id(); // The parent variable product ID
$parent_product = wc_get_product( $parent_id ); // The parent WC_Product object
$text = $parent_product->get_short_description();
}
}
// 2. Other cases (simple, variable …)
else {
$text = $product->get_short_description();
}
$maxchar = 75; // Maximum number of characters
$text = preg_replace ('~\[[^\]]+\]~', '', $text ); // Remove shortcodes
// Remove all html symbols
//$text = strip_tags( $text);
// Crop
if ( mb_strlen( $text ) > $maxchar ){
$text = mb_substr( $text, 0, $maxchar );
$text = preg_replace('@(.*)\s[^\s]*$@s', '\\1 ...', $text );
}
echo "<span class='catalog-short-desc'>$text</span>";
}
代碼在您的活動子主題(或主題)的function.php文件中,或者在任何插件文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.