将“库存数量”添加到WooCommerce变量产品下拉列表中

时间:2017-07-31 00:55:36

标签: php wordpress woocommerce stock variations

在WooCommerce中,我希望在变体下拉菜单中添加其他信息。我很难找到如何做到这一点。

基本上,我现在所拥有的是:

<select id="pa_size" class="" name="attribute_pa_size" data-attribute_name="attribute_pa_size" data-show_option_none="yes">
    <option value="">Choose an option</option>
    <option value="17-x-22"  selected='selected'>17 x 22</option>
    <option value="32-x-40" >32 x 40</option>
</select>`

我希望有这个:

<select id="pa_size" class="" name="attribute_pa_size" data-attribute_name="attribute_pa_size" data-show_option_none="yes">
    <option value="">Choose an option</option>
    <option value="17-x-22"  selected='selected'>17 x 22 - 3 in stock</option>
    <option value="32-x-40" >32 x 40 - 5 in stock</option>
</select>

Image exemple

在文件wc-template-fonction.php中,我找到了所有下拉菜单都构成的功能,这里是代码

function wc_dropdown_variation_attribute_options( $args = array() ) {
    $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' ),
    ) );

    $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      = $args['show_option_none'] ? true : false;
    $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' ) );

            foreach ( $terms as $term ) {
                if ( in_array( $term->slug, $options ) ) {
                    $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 ) ) . '</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 ) ) . '</option>';
            }
        }
    }

    $html .= '</select>';

    echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
}

我是否可以使用过滤器或动作来实现我想要做的事情。我不认为编辑文件wc-template-function.php是最干净的方法,因为下次我做更新时它会被删除。

@LoicTheAztec确实为我提供了一种方法,但我不知道在哪里放置代码,这似乎不是正确的方法。我发现其他帖子解释了改变&#34;选择一个选项&#34;在下拉菜单中,他们主要使用add_filter()或add_action()函数与自定义函数here。但我不确定如何实现我想要的东西。

你能帮助我吗?

我确定我不是唯一一个在WooCommerce上需要这种功能的人。我会继续寻找答案,如果我找到它,我会在这里发布解决方案!

由于

最后我找到了答案!这是怎么做的: 在function.php文件中,我添加了整个wc_dropdown_variation_attribute_options()(上面的代码),然后在if(!empty($ options))中添加一个循环。这里是整个代码:

function wc_dropdown_variation_attribute_options( $args = array() ) {
global $product;
$variations = $product->get_available_variations();


    $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' ),
    ) );
    $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'];
    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 ) ) . '">';
    if ( $args['show_option_none'] ) {
        $html .= '<option value="">' . esc_html( $args['show_option_none'] ) . '</option>';
    }
    if ( ! empty( $options ) ) {

        //the loop is right here
            foreach ( $options as $option ) {

                foreach ($variations as $variation) {
                    if($variation['attributes'][$name] == $option) {
                        $stock = esc_html($variation['max_qty']);
                        //$edition = get_post_meta( get_the_ID(), 'edition', true)
                    }
                }
                $stock_text = ' - Edition of '.$stock;

                // 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 . ' class="'.$class.'" '.$disabled.'>' . $option  .  $stock_text .'</option>';
            //}
        }
    }
    $html .= '</select>';

    echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
}

感谢您的帮助!

0 个答案:

没有答案