简体   繁体   中英

Bulk change the order of attributes dropdowns on WooCommerce variable products

I have 2 product attributes on my products: color and size . They are rendered in that order. I need to switch them, so that size always comes first.

The WooCommerce file single-product/add-to-cart/variable.php seems responsible for the output, but I am not skilled enough to suggest a change to this code, that would make it order by attribute_id , attribute_name , or reverse default order. Right now they seem to order by a taxonomy term or slug rather than actual attribute data, changing ID or name of attributes in the woocommerce_attribute_taxonomies table does not help.

Please help me crack this.

I have searched for this without luck, the answer might be hidden among the many questions about attribute term order which is NOT what I am asking here.

EDIT: I have thousands of products, so manual per-product reordering won't work.

$attribute_keys = array_keys( $attributes );

do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<form class="variations_form cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ); // WPCS: XSS ok. ?>">
    <?php do_action( 'woocommerce_before_variations_form' ); ?>

    <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
        <p class="stock out-of-stock"><?php esc_html_e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
    <?php else : ?>
        <table class="variations" cellspacing="0">
            <tbody>
                <?php foreach ( $attributes as $attribute_name => $options ) : ?>
                    <tr>
                        <td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></td>
                        <td class="value">
                            <?php
                                wc_dropdown_variation_attribute_options( array(
                                    'options'   => $options,
                                    'attribute' => $attribute_name,
                                    'product'   => $product,
                                ) );
                                echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : '';
                            ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

Update: (related to your edit)

Is not possible to alter this using code, as it's handle by WC_Product_Variable_Data_Store_CPT Class on read_variation_attributes() method and there is no available hook(s) for that.

Also as that data is cached, a filter hook should not be reasonably welcome..

In general your actual problem doesn't occurs for most people, as they set a sorting from start on each product, when adding attributes to a variable product.

Now you could try to change that, on the database, but it seems not possible as the product attributes sorting is based on the index of a serialized array located in wp_postmeta for a variable product post_id using the meta_key = _product_attributes .


Original answer:

In a Variable product settings, under "Product data" (metabox) > Attributes (tab) , you can add product attributes. You can also reorder them simply dragging and drop them on the desired positions.

在此处输入图片说明

This will affect the order of the product attributes dropdown, in front end, for a variable product. So you don't need any code for that.

In database , the product attributes sorting is based on the index of an array located in wp_postmeta for a variable product post_id using the meta_key = _product_attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM