简体   繁体   English

批量更改WooCommerce变量产品上属性下拉列表的顺序

[英]Bulk change the order of attributes dropdowns on WooCommerce variable products

I have 2 product attributes on my products: color and size . 我的产品有2个产品属性: 颜色尺寸 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. WooCommerce文件single-product/add-to-cart/variable.php似乎负责输出,但我不够熟练,无法建议对此代码进行更改,这会使它按attribute_idattribute_name或默认顺序反转。 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. 现在,它们似乎是按分类术语或分类来排序,而不是按实际属性数据排序,因此更改woocommerce_attribute_taxonomies表中的ID或属性名称是无济于事的。

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. 无法使用代码来更改此设置,因为它由read_variation_attributes()方法上的WC_Product_Variable_Data_Store_CPT类处理,并且没有可用的钩子。

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 . 现在您可以尝试在数据库上进行更改,但是似乎不可能,因为产品属性排序是基于meta_key = _product_attributes变量wp_postmeta中变量product post_id 的序列化数组的索引进行


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 在数据库中 ,产品属性排序基于meta_key = _product_attributes的变量产品post_id位于wp_postmeta中的数组的索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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