Woocommerce变体下拉选项标签添加了一些数据集 [英] Woocommerce variation dropdown option tag add some dataset

查看:60
本文介绍了Woocommerce变体下拉选项标签添加了一些数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<select class="license_type" name="license_type" id="license_type">
    <option value="license" data-set="500">Single Site License</option>
    <option value="license" data-set="700">5 Site License</option>
    <option value="license" data-set="1400">Developers License</option>
</select>

在woocommerce变体产品中-我想在选项标签中添加一些数据集标签.

In the woocommerce variation product - I want to add some data-set tags in option tag.

data-set="<? php  some code here to pull the price of the variation ?>"

通过钩子/过滤器是否可能.

Is that possible through hook/filter.

推荐答案

function get_price_of_current_variant($product, $name, $term_slug) {

    $price = 0;
    foreach ($product->get_available_variations() as $variation) {
        if ($variation['attributes'][$name] == $term_slug)
        $price = $variation['display_price'];
    }

    return $price;
}


add_filter('woocommerce_dropdown_variation_attribute_options_html', 'wt_modifiy_variation_options', 10, 2);

function wt_modifiy_variation_options($html, $args) {


    $options = $args['options'];
    $product = $args['product'];
    $attribute = $args['attribute']; // The product attribute taxonomy
    $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 = __('Add your custom text in here', 'woocommerce');

    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) . 'customdropdown" 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)) {
            $terms = wc_get_product_terms($product->get_id(), $attribute, array('fields' => 'all'));


            foreach ($terms as $term) {
                if (in_array($term->slug, $options)) {

                    $variat_price = get_price_of_current_variant($product, $name, $term->slug);


                    $data_attr = "data-custom=$variat_price";
                    $html .= '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($args['selected']), $term->slug, false) . $data_attr . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $term->name)) . '</option>';
                }
            }
        } else {
            foreach ($options as $option) {

                $selected = sanitize_title($args['selected']) === $args['selected'] ? selected($args['selected'], sanitize_title($option), false) : selected($args['selected'], $option, false);

                $variat_price = get_price_of_current_variant($product, $name, $option);

                $data_attr = "data-custom=$variat_price";
                $html .= '<option value="' . esc_attr($option) . '" ' . $selected . $data_attr . '>' .
                        esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>';
            }
        }
    }
    $html .= '</select>';


    return $html;
}

这是您所查找内容的代码.

This is the code for what you are looking..

这篇关于Woocommerce变体下拉选项标签添加了一些数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆