Php 选择属性时,将产品名称更改为包含属性

Php 选择属性时,将产品名称更改为包含属性,php,jquery,woocommerce,shortcode,variations,Php,Jquery,Woocommerce,Shortcode,Variations,我已经引用了一个完美的答案 但我在主页上显示一个产品时使用了一个,我不知道如何调整,所以它也适用于主页 我尝试调整只选择产品页面以包含主页的行,但不起作用: // Only single product pages if( ! is_product() && ! is_home() ) return $title; 任何建议都将不胜感激 这仅适用于具有颜色产品属性(用于变体)的可变产品,它将所选颜色属性标签值附加到产品标题 对于您的主页您将使用WordPress条件标签

我已经引用了一个完美的答案

但我在主页上显示一个产品时使用了一个,我不知道如何调整,所以它也适用于主页

我尝试调整只选择产品页面以包含主页的行,但不起作用:

    // Only single product pages
if( ! is_product() && ! is_home() ) return $title;

任何建议都将不胜感激

这仅适用于具有颜色产品属性(用于变体)的可变产品,它将所选颜色属性标签值附加到产品标题

对于您的主页您将使用WordPress条件标签
是首页()…
您还必须设置在快捷码中使用的产品ID

1) 在主页中,我有一个短代码示例(带有可变产品ID):

2) 要使其同时适用于单个产品页面和主页(产品快捷码),请执行以下操作:

add_filter('wp_footer','custom_product_title_script');
函数自定义\产品\标题\脚本(){
//只有单一的产品页面和主页
如果(!(is_product()| | is_front_page())返回;
//在这里设置您的家庭产品ID
$shortcode_product_id=40;//是_类型('variable')返回;
//在这里设置此阵列中的特定产品属性(分开):
$attributes=数组('pa_color');
//变量ID的第一个循环
foreach($product->get\u visible\u children()作为$variation\u id){
//属性/值的第二个循环
foreach($product->get\u available\u variation($variation\u id)['attributes']作为$key=>$value\u id){
$taxonomy=str_replace('attribute_','$key);//获取产品属性的分类
//仅针对定义的属性
if(在数组中($taxonomy,$attributes)){
//在数组中设置和构造数据(变体ID=>product属性=>term名称)
$data[$variation\u id][$taxonomy]=get\u term\u by('slug',$value\u id,$taxonomy)->name;
}
}
} 
?>
(函数($){
//变量初始化
变量数据=,
productTitle=$('.product_title').text(),
颜色='pa_颜色';
//获取所选变体和更改标题的函数
函数更新标题(产品标题、变量数据、颜色){
$.each(变量数据、函数(索引、值){
if(index=$('input.variation_id').val()){
$('.product_title')。文本(productTitle+'-'+值[颜色]);
返回false;
}否则{
$('product_title')。文本(productTitle);
}
});
}
//一旦全部加载
setTimeout(函数(){
更新标题(产品标题、变量数据、颜色);
}, 300);
//现场事件:选择字段
$('select').blur(函数(){
更新标题(产品标题、变量数据、颜色);
});
})(jQuery);

是的!!非常感谢:)
[product_page id="40"]
add_filter( 'wp_footer','custom_product_title_script' );
function custom_product_title_script(){
    // Only single product pages and home page
    if( ! ( is_product() || is_front_page() ) ) return;

    // HERE Set your home product ID
    $shortcode_product_id = 40; // <=====  =====  =====  HERE your Shortcode Product ID

    $product_id = is_product() ? get_the_id() : $shortcode_product_id;

    // get an instance of the WC_Product Object
    $product = wc_get_product( $product_id );

    // Only for variable products
    if( ! $product->is_type( 'variable' ) ) return;

    // Here set your specific product attributes in this array (coma separated):
    $attributes = array('pa_color');
    // The 1st loop for variations IDs
    foreach($product->get_visible_children( ) as $variation_id ) {
        // The 2nd loop for attribute(s)/value
        foreach($product->get_available_variation( $variation_id )['attributes'] as $key => $value_id ){
            $taxonomy = str_replace( 'attribute_', '', $key ); // Get the taxonomy of the product attribute
            // Just for defined attributes
            if( in_array( $taxonomy, $attributes) ){
                // Set and structure data in an array( variation ID => product attribute => term name )
                $data[ $variation_id ][$taxonomy] = get_term_by( 'slug', $value_id, $taxonomy )->name;
            }
        }
    } 
    ?>
        <script type="text/javascript">
            (function($){
                // variables initialization
                var variationsData = <?php echo json_encode($data); ?>,
                    productTitle = $('.product_title').text(),
                    color = 'pa_color';
                // function that get the selected variation and change title
                function update_the_title( productTitle, variationsData, color ){
                    $.each( variationsData, function( index, value ){
                        if( index == $('input.variation_id').val() ){
                            $('.product_title').text(productTitle+' - '+value[color]);
                            return false;
                        } else {
                            $('.product_title').text(productTitle);
                        }
                    });
                }
                // Once all loaded
                setTimeout(function(){
                    update_the_title( productTitle, variationsData, color );
                }, 300);
                // On live event: select fields
                $('select').blur( function(){
                    update_the_title( productTitle, variationsData, color );
                });
            })(jQuery);
        </script>
    <?php
}