首页 > 解决方案 > 为空描述显示 Woocommerce 产品页面默认描述

问题描述

是否可以在后端产品描述区域中为所有新添加的产品页面添加 Woocommerce 产品默认描述作为默认文本(例如免费)或默认功能,该功能将生成特定操作但仅适用于没有的产品t 有产品描述值

所需要的只是让描述在下面的描述中为新添加的产品具有默认的附加值

在此处输入图像描述

有人可以帮忙吗?

我发现这对产品的简短描述起到了神奇的作用,但我希望它是产品描述本身

add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_short_desc_if_empty', 21 );

function bbloomer_echo_short_desc_if_empty() {
   global $post;
   if ( empty ( $post->post_excerpt  ) ) {
      $post_excerpt = '<p class="default-short-desc">';
        $post_excerpt .= 'This is the default, global, short description.<br>It will show if <b>no short description has been entered!</b>';
        $post_excerpt .= '</p>';
      echo $post_excerpt;
   }
}

标签: phpwordpresswoocommercehook-woocommerce

解决方案


function woocommerce_default_description($content) {
    $empty = empty($content) || trim($content) === '';
    if(is_product() && $empty) {
        $content = 'default text content';
    }
    return $content;
}
add_filter( 'the_content', 'woocommerce_default_description' ); 

function rmg_woocommerce_default_product_tabs($tabs) {
    if (empty($tabs['description'])) {
        $tabs['description'] = array(
            'title'    => __( 'Description', 'woocommerce' ),
            'priority' => 10,
            'callback' => 'woocommerce_product_description_tab',
        );
    }
    return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'rmg_woocommerce_default_product_tabs' );

像上面这样的东西应该可以工作。


推荐阅读