Woocommerce-Varianten-Dropdown-Options-Tag fügt einen Datensatz hinzu

Lesezeit: 3 Minuten

Woocommerce Varianten Dropdown Options Tag fugt einen Datensatz hinzu
Anfänger115

<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>

Im WooCommerce-Variantenprodukt möchte ich einige Datensatz-Tags im Options-Tag hinzufügen.

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

Ist das über Hook/Filter möglich?

Woocommerce Varianten Dropdown Options Tag fugt einen Datensatz hinzu
mujunur

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;
}

Dies ist der Code für das, was Sie suchen.

  • Ich habe dies auch ausgeführt (Lösung](stackoverflow.com/questions/58292681/…) werden sie nicht miteinander in Konflikt geraten?

    – Anfänger115

    21. Oktober 2019 um 8:13 Uhr

  • Funktioniert nicht → <option value="5-site-license" data-custom="0" class="attached enabled">5 Site License</option>

    – Anfänger115

    21. Oktober 2019 um 22:08 Uhr

  • Benutzerdefinierter Klassen-/ID-/Namenscode, der das Problem verursacht?screencast.com/t/LPhgHHQ0ZSTa

    – Anfänger115

    22. Oktober 2019 um 3:26 Uhr


  • @wocommerce115 Du brauchst nicht beide Codes gleichzeitig. Fügen Sie die Klasse über das obige Snippet hinzu.

    – mujuonly

    22. Oktober 2019 um 5:11 Uhr

  • @wocommerce115 – Aktualisiert

    – mujuonly

    22. Oktober 2019 um 6:01 Uhr

Ja es ist möglich. Sie können dies verwenden

apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args )

filtern, um Ihre oben genannten Aufgaben zu erfüllen.

  • Auch hier ein Beispiel, aber die richtige Lösung ist nicht angegeben.

    – Anfänger115

    20. Oktober 2019 um 19:46 Uhr

917250cookie-checkWoocommerce-Varianten-Dropdown-Options-Tag fügt einen Datensatz hinzu

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy