简体   繁体   English

Woocommerce 单变量产品中的重新排序属性下拉项

[英]Reorder attribute dropdown terms in Woocommerce single variable products

In the Woocommerce single variable product pages, I would like to change the order of the sizing options on the related drop-down menu.在 Woocommerce 单变量产品页面中,我想更改相关下拉菜单中尺寸选项的顺序。

Actually It is like this (In alphabetical order):其实是这样的(按字母顺序排列):
- L - 大号
- M - 米
- XL - 加大码

I would like to have it like this (in logical size order):我想像这样(按逻辑大小顺序):
- M - 米
- L - 大号
- XL - 加大码

How can I reorder the sizing options drop-down menu in single variable product pages?如何重新排序单一变量产品页面中的尺寸选项下拉菜单?

Any help is appreciated.任何帮助表示赞赏。

This is just about settings in the Attributes section for your "Size" product attribute. 这只是在属性部分设置为你的“大小”的产品属性。

1) You need to click on "configure terms" : 1)您需要点击“配置条款”

在此处输入图片说明

2) You are now on the list of the terms. 2)您现在位于条款列表中。 You can reorder terms by drag and drop on the icon located in the right: 您可以通过拖放右侧图标来对术语进行重新排序:

在此处输入图片说明

3) Then you will get the following 3)然后您将获得以下内容

在此处输入图片说明

The dropdown "Size" options on related variable products will be in the correct order now. 现在,相关变量产品上的“大小”下拉菜单将以正确的顺序排列。

Sorting attributes in Product -> Attributes can be extremely inconvenient, especially if you have a lot of attributes.Product -> Attributes中对属性进行排序可能会非常不方便,尤其是当您有很多属性时。 The problem can be solved through the woocommerce_dropdown_variation_attribute_options_html filter.这个问题可以通过woocommerce_dropdown_variation_attribute_options_html过滤器来解决。

To make drag-and-drop sorting work on the product page, try adding to your functions.php the following code:要在产品页面上进行拖放排序,请尝试将以下代码添加到您的 functions.php 中:

add_filter('woocommerce_dropdown_variation_attribute_options_html', 'wc_dropdown_variation_attribute_options_sorted', 20, 2);
function wc_dropdown_variation_attribute_options_sorted( $html, $args ) {
$args = wp_parse_args(
    apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ),
    array(
        'options'          => false,
        'attribute'        => false,
        'product'          => false,
        'selected'         => false,
        'name'             => '',
        'id'               => '',
        'class'            => '',
        'show_option_none' => __( 'Choose an option', 'woocommerce' ),
    )
);

// Get selected value.
if ( false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product ) {
    $selected_key = 'attribute_' . sanitize_title( $args['attribute'] );
    // phpcs:disable WordPress.Security.NonceVerification.Recommended
    $args['selected'] = isset( $_REQUEST[ $selected_key ] ) ? wc_clean( wp_unslash( $_REQUEST[ $selected_key ] ) ) : $args['product']->get_variation_default_attribute( $args['attribute'] );
    // phpcs:enable WordPress.Security.NonceVerification.Recommended
}

$options               = $args['options'];
$product               = $args['product'];
$attribute             = $args['attribute'];
$name                  = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id                    = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class                 = $args['class'];
$show_option_none      = (bool) $args['show_option_none'];
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options.

if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
    $attributes = $product->get_variation_attributes();
    $options    = $attributes[ $attribute ];
}

$html  = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

if ( ! empty( $options ) ) {
    if ( $product && taxonomy_exists( $attribute ) ) {
        // Get terms if this is a taxonomy - ordered. We need the names too.
        $terms = wc_get_product_terms(
            $product->get_id(),
            $attribute,
            array(
                'fields' => 'all',
            )
        );
        
        //sorting starts here
        foreach($terms as $key => $term) {
            $i = 0;
            foreach($product->get_available_variations() as $variation) {
                $i++;
                if ($term->slug == $variation['attributes'][$name]) {
                    $key = $i - 1;
                    unset($terms[$key]);
                    $terms[$key] = $term;
                }
            }
        }

        ksort($terms);

        foreach ( $terms as $term ) {
            if ( in_array( $term->slug, $options, true ) ) {
                $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</option>';
            }
        }
    } else {
        foreach ( $options as $option ) {
            // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
            $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
            $html    .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '</option>';
        }
    }
}

return $html .= '</select>';

} }

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

相关问题 Woocommerce可变产品下拉列表 - Woocommerce Variable products dropdown 隐藏 WooCommerce 可变产品上的特定产品属性下拉列表 - Hide specific product attribute dropdown on WooCommerce variable products 在 WooCommerce 可变产品属性下拉选项中添加最小差价 - Add the min price difference in WooCommerce variable products attribute dropdown options 在管理产品通用框中获取 WooCommerce 产品变体属性术语 - Get WooCommerce product variation attribute terms in admin products general box 显示 WooCommerce 自定义产品属性和单个产品的所有条款 - Display WooCommerce Custom Product Attributes and all terms on single products Ajax 加入购物车 WooCommerce 单品上的简单和可变产品 - Ajax Add to cart for simple and variable products on WooCommerce single products 在 Woocommerce 单个产品的标题后添加特定的产品属性 - Add specific product attribute after title on Woocommerce single products 在 Woocommerce 单个产品的标题前添加链接的特定产品属性 - Add linked specific product attribute before title on Woocommerce single products 从Woocommerce可变价格下拉菜单中排除产品 - Exclude products from Woocommerce variable product dropdown with variations prices 在 WooCommerce 可变产品中的每个属性值旁边显示库存状态 - Show stock status next to each attribute value in WooCommerce variable products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM