【问题标题】:Display products attribute labels and values in WooCommerce archives pages在 WooCommerce 档案页面中显示产品属性标签和值
【发布时间】:2018-06-06 04:16:53
【问题描述】:

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

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

最后,必须考虑到该网站使用 WPML 的语言不止一种。

为此,我使用了以下代码,它可以工作,但这意味着对每个属性重复相同的代码(你可以看到我重复了相同的 4 次)。肯定有一种方法可以合并一个 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
    }

如何压缩此代码以避免 html 块重复?

如何获取所有产品类型的所有属性,而仅获取产品变体的选定属性值?

在我收到所有输入之后,我的最终代码已经可以正常工作了:

add_action( 'woocommerce_after_shop_loop_item_title', 'add_attributes_terms', 1);
    function add_attributes_terms() {
        global $product;

        ?><div style="font-size: 10px; overflow: overlay; padding: 6px 10px 6px 10px; line-height: 13px; box-shadow: inset 0 0 5px rgba(0,0,0,0.1), inset 0 3px 2px rgba(0,0,0,0.1);"><?php
            // Loop through each defined attribute
            foreach( $product->get_attributes() as $taxonomy => $values ){
                $terms = $product->get_attribute( $taxonomy ); 
                if(!empty($terms)){
                    ?><div style="overflow: overlay;"><?php
                        echo '<span id="shop_labels" style=" float: left; color: #444;">' . str_replace([" colour", "Cor da ", "Cor do ", "Cor das ", " de parede"], ["","","","",""],  wc_attribute_label($taxonomy)) . ':&nbsp;&nbsp;</span>';
                        echo '<div style="color: #959595;">' . $terms . '</div>';
                    ?></div><?php
                }
            }
	    ?></div><?php
    }

【问题讨论】:

    标签: php html woocommerce attributes variations


    【解决方案1】:

    更新

    以下代码将:

    • 压缩您的代码
    • 自动显示属性名称 + 值对(无需定义)。
    • 特别是对于产品变体,它将仅显示选定的属性值。

    代码:

    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>';
    }
    

    这段代码已经过测试,没有输出任何错误……它应该适合你。

    【讨论】:

    • 谢谢你,伙计。有了你的提示,我成功地让它变得更紧凑了
    • 尽管如此,它仍然要求在开始时定义属性,这应该不是必需的,因为它应该呈现所有产品属性。照原样,每次后端管理器创建一个新属性时,都需要程序员将其添加到代码中...
    • 你不喜欢哪一部分?
    • 好吧,现在让我们看看你的答案,伙计
    • 谢谢!!有了它,我现在有了我想要的。我什至做得更紧凑,它适用于不同的浏览器。我使用“WooCommerce Show Single Variations (Premium)”。
    【解决方案2】:

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

     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() )
            );
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2019-09-20
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多