Php 在电子商务归档页面中显示产品属性标签和值

Php 在电子商务归档页面中显示产品属性标签和值,php,html,woocommerce,attributes,variations,Php,Html,Woocommerce,Attributes,Variations,我有简单的和可变的产品。我也有一个插件,用于在商店中单独显示每个变体,而不仅仅是它的父项。我设置了4个属性,每个属性都有几个可能的值,所有产品都可能共享这些属性的全部或部分。将来可能会有更多属性,它们可能适用于所有产品或仅适用于零件 我需要在每个变体(以及简单产品)下和变体父项下仅显示相应产品具有的标签和值属性,而不是可能应用于该属性的所有可能值 最后,我们必须考虑到站点有一个以上的WPML语言。 为此,我使用了下面的代码,这很有效,但这意味着对每个属性重复相同的代码(您可以看到我重复了相同的4

我有简单的和可变的产品。我也有一个插件,用于在商店中单独显示每个变体,而不仅仅是它的父项。我设置了4个属性,每个属性都有几个可能的值,所有产品都可能共享这些属性的全部或部分。将来可能会有更多属性,它们可能适用于所有产品或仅适用于零件

我需要在每个变体(以及简单产品)下和变体父项下仅显示相应产品具有的标签和值属性,而不是可能应用于该属性的所有可能值

最后,我们必须考虑到站点有一个以上的WPML语言。 为此,我使用了下面的代码,这很有效,但这意味着对每个属性重复相同的代码(您可以看到我重复了相同的4x)。当然有一种方法可以合并foreach循环来完成这项工作。我用StackExchange中看到的相同示例多次尝试过这一点,但都没有用

add_action( 'woocommerce_after_shop_loop_item_title', 'add_attributes_terms', 1);

    function add_attributes_terms() {

        $tax_bar = 'pa_bar-colour';
      $tax_but = 'pa_button-colour';
        $tax_caps = 'pa_caps-colour';
        $tax_cas = 'pa_lock-cases-colour';
        $att_bar = get_the_terms( $product->id, $tax_bar); 
        $att_but = get_the_terms( $product->id, $tax_but); 
        $att_caps = get_the_terms( $product->id, $tax_caps);
        $att_cas = get_the_terms( $product->id, $tax_cas); 


        ?><div style="font-size: 11px; overflow: overlay; padding: 0px 5px 15px 5px; line-height: 13px;"><?php
            if (! empty($att_bar)) {
                ?><div style="overflow: overlay;"><?php
                    echo '<span style=" float: left; color: #444;">' . str_replace(" colour", "", get_taxonomy($tax_bar)->labels->singular_name) . ':&nbsp;&nbsp;</span>';          
                    ?><div style="overflow: overlay; color: #959595;"><?php
                        if ( $att_bar && ! is_wp_error( $$att_bar )): foreach ( $att_bar as $attributes_name ){
                            echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
                        }
                        endif;
                    ?></div><?php   
                ?></div><?php
            }
            if (! empty($att_but)) {
                ?><div class="b2" style="overflow: overlay;"><?php
                    echo '<span style=" float: left; color: #444;">' . str_replace(" colour","", get_taxonomy($tax_but)->labels->singular_name) . ':&nbsp;&nbsp;</span>';           
                    ?><div style="overflow: overlay; color: #959595;"><?php
                        if ( $att_but && ! is_wp_error( $$att_but )): foreach ( $att_but as $attributes_name ){
                            echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
                        }
                        endif;
                    ?></div><?php   
                ?></div><?php
            }
            if (! empty($att_caps)) {
                ?><div style="overflow: overlay;"><?php
                    echo '<span style=" float: left; color: #444;">' . str_replace(" colour","", get_taxonomy($tax_caps)->labels->singular_name) . ':&nbsp;&nbsp;</span>';          
                    ?><div style="overflow: overlay; color: #959595;"><?php
                        if ( $att_caps && ! is_wp_error( $$att_caps )): foreach ( $att_caps as $attributes_name ){
                            echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
                        }
                        endif;
                    ?></div><?php   
                ?></div><?php
            }
            if (! empty($att_cas)) {
                ?><div style="overflow: overlay;"><?php
                    echo '<span style=" float: left; color: #444;">' . str_replace(" colour","", get_taxonomy($tax_cas)->labels->singular_name) . ':&nbsp;&nbsp;</span>';           
                    ?><div style="overflow: overlay; color: #959595;"><?php
                        if ( $att_cas && ! is_wp_error( $$att_cas )): foreach ( $att_cas as $attributes_name ){
                            echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
                        }
                        endif;
                    ?></div><?php   
                ?></div><?php
            }
        ?></div><?php
    }
add_action('woocommerce_之后的_shop_loop_item_title','add_attributes_terms',1);
函数add_attributes_terms(){
$tax_bar='pa_bar-color';
$tax_but='pa_按钮颜色';
$tax_caps='pa_caps-color';
$tax_cas='pa_lock-cases-color';
$att\u bar=获取条款($product->id,$tax\u bar);
$att_but=获取条款($product->id,$tax_but);
$att_caps=获取条款($product->id,$tax_caps);
$att_cas=获取条款($product->id,$tax_cas);

?>更新

以下代码将:

  • 压缩代码
  • 自动显示属性名称+值对(不定义它们)
  • 特别是对于产品变体,它将仅显示选定的属性值
守则:

add_action( 'woocommerce_after_shop_loop_item_title', 'shop_loop_display_product_attributes', 4);
function shop_loop_display_product_attributes() {
    global $product;
    $attributes = array();

    echo '<div style="font-size: 11px; overflow: overlay; padding: 0px 5px 15px 5px; line-height: 13px;">';

    // Loop through each defined attribute
    foreach ( $product->get_attributes() as $taxonomy => $values ) {
        $term_names = array();

        // Get the class for only 'pa_button-colour' case
        $class = $taxonomy == 'pa_button-colour' ? ' class="b2"' : '';

        $taxonomy_label = get_taxonomy($taxonomy)->labels->singular_name;
        $label_name = str_replace(" colour", "", $taxonomy_label );

        echo '<div'. $class .' style="overflow: overlay;">
            <span style=" float: left; color: #444;">'.$label_name.':&nbsp;&nbsp;</span>';

        if( $product->is_type('variation') ) // Product variation type (For your plugin)
        {
            $term_names[] = get_term_by( 'slug', $values, $taxonomy )->name;
        } 
        else // Other product types
        { 
            $term_ids = $values->get_options();
            foreach ( $term_ids as $term ) {
                $term_names[] = get_term( $term, $taxonomy )->name;
            }
        }
        if ( count($term_names) != 0 ) {
            echo '<div style="overflow: overlay; color: #959595;">
                <span> ' . implode( ' </span><span> ', $term_names ) . ' </span>
            </div>';
        }
        echo '</div>';
    }
    echo '</div>';
}
add_action('woocommerce_在_shop_loop_项目名称之后,'shop_loop_显示_产品属性',4);
功能车间\循环\显示\产品\属性(){
全球$产品;
$attributes=array();
回声';
//循环遍历每个定义的属性
foreach($product->get_attributes()作为$taxonomy=>$values){
$term_names=array();
//仅为“PAU按钮颜色”案例获取课程
$class=$taxonomy=='pa_按钮颜色'?'class=“b2”:'';
$taxonomy\u label=get\u taxonomy($taxonomy)->labels->singular\u name;
$label\u name=str\u replace(“颜色”、“”、$taxonomy\u标签);
回声'
“.$label_name”。:”;
if($product->is_type('variation')//产品变体类型(用于插件)
{
$term_names[]=get_term_by('slug',$values,$taxonomy)->name;
} 
else//其他产品类型
{ 
$term_id=$values->get_options();
foreach($term\u id作为$term){
$term\u names[]=get\u term($term$taxonomy)->name;
}
}
如果(计数($term_名称)!=0){
回声'
“.内爆(“”,$term_名称)。”
';
}
回声';
}
回声';
}

此代码经过测试,不会输出任何错误…它应该适用于您。

商店页面下拉列表代码显示可变产品商店页面上的变化下拉列表

 add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page' );

 function woo_display_variation_dropdown_on_shop_page() {

    global $product;
    if( $product->is_type( 'variable' )) {

    $attribute_keys = array_keys( $product->get_attributes() );

?>

    <form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
        <?php do_action( 'woocommerce_before_variations_form' ); ?>

        <?php if ( empty( $product->get_available_variations() ) && false !== $product->get_available_variations() ) : ?>
            <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
        <?php else : ?>
            <table class="variations" cellspacing="0">
                <tbody>
                    <?php foreach ( $product->get_variation_attributes() as $attribute_name => $options ) : ?>
                        <tr>
                            <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
                            <td class="value">
                                <?php
                                    $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
                                    wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                                    echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                                ?>
                            </td>
                        </tr>
                    <?php endforeach;?>
                </tbody>
            </table>

                <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

            <div class="single_variation_wrap">
                <?php
                    /**
                     * woocommerce_before_single_variation Hook.
                     */
                    do_action( 'woocommerce_before_single_variation' );

                    /**
                     * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
                     * @since 2.4.0
                     * @hooked woocommerce_single_variation - 10 Empty div for variation data.
                     * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
                     */
                    do_action( 'woocommerce_single_variation' );

                    /**
                     * woocommerce_after_single_variation Hook.
                     */
                    do_action( 'woocommerce_after_single_variation' );
                ?>
            </div>

            <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
        <?php endif; ?>

        <?php do_action( 'woocommerce_after_variations_form' ); ?>
    </form>

    <?php } else {

    echo sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        );

    }

}
add_filter('woocommerce_loop_add_to_cart_link'、'woo_display_variation_dropdown_on_shop_page');
功能woo_显示_变化_下拉列表_在_店铺_页面上(){
全球$产品;
如果($product->is_类型('variable')){
$attribute_keys=数组_keys($product->get_attributes());
?>

谢谢你,伙计。有了你的提示,我甚至成功地使它更紧凑了。然而,它仍然要求在开始时定义属性,这不应该是必要的,因为它应该显示所有产品属性。就像这样,每次后端管理器创建一个新属性,都需要程序员将其添加到代码中…Whi你不喜欢哪一部分?好吧,现在让我们看看你的答案,谢谢!!我现在有了我想要的。我甚至做了更紧凑的设计,可以在不同的浏览器中使用。我使用“WooCommerce Show Single Variations(Premium)”。