在WooCommerce存档页面中显示产品属性标签和值

时间:2017-12-24 10:00:37

标签: php html woocommerce attributes variations

我有简单和可变的产品。我还有一个插件,用于在商店中显示每个变体本身,而不仅仅是其父。我设置了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
    }

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

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

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

&#13;
&#13;
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
    }
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分: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>';
}

此代码已经过测试,并且不会输出任何错误......它应该适合您。

答案 1 :(得分:0)

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

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

    }

}