在单个产品页面上显示类别和品牌名称
Display category and brand name on single product page
在 Woocommerce 中,我使用 YITH WooCommerce Brands plugin 来处理产品品牌。
我目前正在为我在 WooCommerce 中的简短描述下想要的固定文本而苦苦挣扎。我想在该文本 (有效) 中动态显示 产品名称 ,还有产品类别名称 [CATEGORY_NAME]
和品牌名称[BRAND_NAME]
.
但我似乎无法让它们发挥作用。
基于此回答主题:
这是我的代码版本:
// Add text after short description
add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
global $product;
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}
function custom_single_excerpt(){
global $post, $product;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description )
return;
// The custom text
$custom_text = 'Zoekt u naast de '.$product->get_name().' andere [PRODUCT_CATEGORY] van dit merk? Bekijk dan eens de gehele collectie van [BRAND_NAME]. Powerlight is officieel dealer van [BRAND_NAME]. Heeft u een specifieke vraag over dit product? Neem gerust eens contact op met onze <span style="text-decoration: underline;"><a href="https://power-light.nl/contact/">klantenservice</a></span>. Onze adviseurs staan graag voor u klaar.';
?>
<div class="woocommerce-product-details__short-description">
<?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
</div>
<?php
}
知道如何在我的自定义文本中显示产品类别名称和产品品牌名称吗?
下面的代码将在产品简短描述之后输出自定义文本,其中包含正确的产品类别和正确的 (Yith) 产品品牌 (因此YITH WooCommerce 品牌插件).
add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
global $product;
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}
function custom_single_excerpt(){
global $post, $product;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description )
return;
// Get product categories
$categories = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'names' ) );
// Get product brands (NOTE: for Woocommerce brands plugin, the taxonomy is 'product_brand')
$brands = wp_get_post_terms( $post->ID, 'yith_product_brand', array( 'fields' => 'names' ) );
// The custom link
$custom_link = '<span style="text-decoration: underline;"><a href="https://power-light.nl/contact/">'.__("klantenservice").'</a></span>';
// The custom text
$custom_text = sprintf(__("Zoekt u naast de %s andere %s van dit merk? Bekijk dan eens de gehele collectie van %s. Powerlight is officieel dealer van %s. Heeft u een specifieke vraag over dit product? Neem gerust eens contact op met onze %s. Onze adviseurs staan graag voor u klaar."), $product->get_name(), reset($categories), reset($brands), reset($brands), $custom_link );
?>
<div class="woocommerce-product-details__short-description">
<?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
</div>
<?php
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
如果您使用 Woocommerce Brands 插件,您必须将代码 'yith_product_brand'
替换为 'product_brand'
。就这些了。
在 Woocommerce 中,我使用 YITH WooCommerce Brands plugin 来处理产品品牌。
我目前正在为我在 WooCommerce 中的简短描述下想要的固定文本而苦苦挣扎。我想在该文本 (有效) 中动态显示 产品名称 ,还有产品类别名称 [CATEGORY_NAME]
和品牌名称[BRAND_NAME]
.
但我似乎无法让它们发挥作用。
基于此回答主题:
这是我的代码版本:
// Add text after short description
add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
global $product;
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}
function custom_single_excerpt(){
global $post, $product;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description )
return;
// The custom text
$custom_text = 'Zoekt u naast de '.$product->get_name().' andere [PRODUCT_CATEGORY] van dit merk? Bekijk dan eens de gehele collectie van [BRAND_NAME]. Powerlight is officieel dealer van [BRAND_NAME]. Heeft u een specifieke vraag over dit product? Neem gerust eens contact op met onze <span style="text-decoration: underline;"><a href="https://power-light.nl/contact/">klantenservice</a></span>. Onze adviseurs staan graag voor u klaar.';
?>
<div class="woocommerce-product-details__short-description">
<?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
</div>
<?php
}
知道如何在我的自定义文本中显示产品类别名称和产品品牌名称吗?
下面的代码将在产品简短描述之后输出自定义文本,其中包含正确的产品类别和正确的 (Yith) 产品品牌 (因此YITH WooCommerce 品牌插件).
add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary', 2 );
function custom_single_product_summary(){
global $product;
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_single_excerpt', 20 );
}
function custom_single_excerpt(){
global $post, $product;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description )
return;
// Get product categories
$categories = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'names' ) );
// Get product brands (NOTE: for Woocommerce brands plugin, the taxonomy is 'product_brand')
$brands = wp_get_post_terms( $post->ID, 'yith_product_brand', array( 'fields' => 'names' ) );
// The custom link
$custom_link = '<span style="text-decoration: underline;"><a href="https://power-light.nl/contact/">'.__("klantenservice").'</a></span>';
// The custom text
$custom_text = sprintf(__("Zoekt u naast de %s andere %s van dit merk? Bekijk dan eens de gehele collectie van %s. Powerlight is officieel dealer van %s. Heeft u een specifieke vraag over dit product? Neem gerust eens contact op met onze %s. Onze adviseurs staan graag voor u klaar."), $product->get_name(), reset($categories), reset($brands), reset($brands), $custom_link );
?>
<div class="woocommerce-product-details__short-description">
<?php echo $short_description . $custom_text; // WPCS: XSS ok. ?>
</div>
<?php
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
如果您使用 Woocommerce Brands 插件,您必须将代码 'yith_product_brand'
替换为 'product_brand'
。就这些了。