我正在尝试使用functions.php在产品标题的h1
标签中插入可用的产品变体
我试图实现的目标如下: 我的产品标题-0.7千克/1.4千克
我将Elementor与“ Hello Elementor”主题一起使用。 使用“页面标题”元素插入标题,而不是使用默认的WC标题。 您可以看一下以下产品:
https://mancareromaneasca.ro/aperitive-la-comanda/fasole-batuta-la-comanda/
我对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
}
}
}
}
}
}
24nov-卑微的颠簸