Php 在产品名称中添加变体
我试图使用functions.php在产品标题的Php 在产品名称中添加变体,php,wordpress,woocommerce,elementor,Php,Wordpress,Woocommerce,Elementor,我试图使用functions.php在产品标题的h1标记中插入可用的产品变体 我试图实现的目标如下所示: 我的产品名称-0.7千克/1.4千克 我使用带有“Hello Elementor”主题的Elementor。 标题是使用页面标题元素插入的,因此不是默认的WC标题。 您可以查看以下产品: 我对PHP不是很在行,所以我毫无乐趣地混合了一堆代码片段: function test_func(){ global $woocommerce, $product, $post; // t
h1
标记中插入可用的产品变体
我试图实现的目标如下所示:
我的产品名称-0.7千克/1.4千克
我使用带有“Hello Elementor”主题的Elementor。
标题是使用页面标题元素插入的,因此不是默认的WC标题。
您可以查看以下产品:
我对PHP不是很在行,所以我毫无乐趣地混合了一堆代码片段:
function test_func(){
global $woocommerce, $product, $post;
// test if product is variable
if( $product->is_type( 'variable' ) ){
// Loop through available product variation data
foreach ( $product->get_available_variations() as $key => $variation ) {
// Loop through the product attributes for this variation
foreach ($variation['attributes'] as $attribute => $term_slug ) {
// Get the taxonomy slug
$taxonmomy = str_replace( 'attribute_', '', $attribute );
// Get the attribute label name
$attr_label_name = wc_attribute_label( $taxonmomy );
// Display attribute labe name
$term_name = get_term_by( 'slug', $term_slug, $taxonmomy )->name;
// Testing output
remove_action(‘woocommerce_single_product_summary’,’woocommerce_template_single_title’,5);
add_action(‘woocommerce_single_product_summary’, ‘woocommerce_my_single_title’,5);
if ( ! function_exists( ‘woocommerce_my_single_title’ ) ) {
function woocommerce_my_single_title() {
?>
<h1 itemprop=”name” class=”product_title entry-title”><?php the_title(); ?> - <?php echo '<span>' . $attr_label_name . ': ' . $term_name . '</span>'; ?></h1>
<?php
}
}
}
}
}
}
功能测试_func(){
全球$woocmerce、$product、$post;
//测试产品是否可变
如果($product->is_类型('variable')){
//循环查看可用的产品变化数据
foreach($product->get\u available\u variations()作为$key=>$variation){
//循环浏览此变体的产品属性
foreach($variation['attributes']作为$attribute=>$term\u slug){
//获取分类法slug
$taxonomy=str_replace('attribute_','''$attribute);
//获取属性标签名称
$attr\u label\u name=wc\u attribute\u label($taxonomy);
//显示属性标签名称
$term\u name=get\u term\u by('slug',$term\u slug,$taxonomy)->name;
//测试输出
删除行动(“woocommerce\u single\u product\u summary”和“woocommerce\u template\u single\u title”,5);
添加行动(“woocommerce单品摘要”,“woocommerce单品标题”,5);
如果(!function_存在('woocommerce_my_single_title')){
功能名称(我的单标题){
?>
-