在WooCommerce中针对特定产品属性的产品简短说明显示文本



我正在尝试将其用于特定产品属性,该属性包含"龙舌兰"文本在简短描述中显示。

我尝试了一些代码段,但似乎没有用。我没有问题让他们与类别一起工作,但我只想要产品的某些属性-Agave

function filter_woocommerce_short_description( $post_excerpt ) {
    global $post;
    if ( has_term( "agave", "categories", $post->ID ) ) {
        $post_excerpt .= "<br/>" . "Text Here";
    }
    return $post_excerpt; 
};
add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1  );

我希望文本在某些属性(龙舌兰(下显示,但它们不

我尝试使用此

    add_filter('woocommerce_short_description', 
    'filter_woocommerce_short_description',10, 1  );
    function filter_woocommerce_short_description( $short_description ) {
     global $product;
   $string_values = $product->get_attribute('agave');
   if ( strpos($string_values, 'agave') !== false ) {
  $short_description .= '<br>' . __("Testing This Out - AGAVE");
    }
   return $short_description;
   }

对于特定产品属性" Agave"您将使用一些不同的东西:

add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1  );
function filter_woocommerce_short_description( $short_description ) {
    global $product;
    $string_values = $product->get_attribute('agave');
    if ( ! empty($string_values) ) {
        $short_description .= '<br>' . __("Text Here");
    }
    return $short_description;
}

代码在您的活动子主题(或活动主题(的function.php文件中。测试并起作用。

现在,如果"龙舌兰"是产品属性的术语,则需要在$product->get_attribute('attribute-name');中设置产品属性名称并用以下内容替换条件:

if ( strpos($string_values, 'agave') !== false ) {

注意:产品类别的分类法是product_cat,但不是categories

 add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1  );
 function filter_woocommerce_short_description( $short_description ) {
     global $product;
   $string_values = $product->get_attribute('agave');
   $agave = $attributes["agave"];
   if ( $agave ) {
        $short_description .= '<br>' . __("Testing This Out - AGAVE");
    }
   return $short_description;
   }

最新更新