Цена не обновляется динамически при изменении раскрывающегося списка на странице сведений о продукте WooCommerce.Php

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Цена не обновляется динамически при изменении раскрывающегося списка на странице сведений о продукте WooCommerce.

Сообщение Anonymous »


I created a WooCommerce plugin for dynamic pricing I wanted to show the changing price based on dropdown selection. I created the Ajax but it is not working. As the prices are fetched from the database and calculations happen in code.

add_action('woocommerce_before_add_to_cart_button', 'add_to_cart_product_pricing'); function add_to_cart_product_pricing() { global $product; wp_enqueue_style('my-plugin-styles', plugins_url("jewellery_pricing/assets/css/diamond_style.css", __FILE__)); // Targeting "ring" or "rings" product category if (is_product() && has_term(array('ring', 'rings'), 'product_cat')) { extract(get_all_product_data($product, true)); $metal_purity_percent = get_metal_purity($metal_purity); // Metal Purity percent $gold_value = ($gold_rate * $metal_purity_percent) / 100; // Gold value $finalgold = $gold_value * $net_weight; // printf('%s
', get_displayed_price_html($finalgold, $product)); echo ''; echo '' . __("Ring Size") . ':
'; // Loop through ring sizes foreach (get_ring_sizes() as $ring_size) { printf('%s', $ring_size, selected($ring_size, $def_ring_size), $ring_size); } echo ''; echo ' ' . __("Metal Purity") . ':
'; // Loop through metal purity foreach (get_metal_purity_14k_18k() as $purity) { printf('%s', $purity, selected($purity, $metal_purity), $purity); } echo ''; echo '' . __("Diamond Quality") . ':
'; foreach (get_diamondqualityall() as $quality) { printf('%s', $quality, selected($quality, $diamond_quality), $quality); } echo ''; echo ' '; } }
Ajax Receiver from where all the action takeplace

add_action('wp_ajax_update_gold_price', 'update_gold_price'); add_action('wp_ajax_nopriv_update_gold_price', 'update_gold_price'); function update_gold_price() { check_ajax_referer('price_calculator_nonce', 'security'); if (isset($_REQUEST['product_data']) && !empty($_REQUEST['product_data'])) { $data = (array) $_REQUEST['product_data']; if ( isset($data['product_id']) && isset($data['ring_size']) && isset($data['metal_purity']) && !empty($data['product_id']) && !empty($data['ring_size']) && !empty($data['metal_purity']) ) { $product = wc_get_product(intval($data['product_id'])); // Get necessary product data, converting each element to usable variables (incl gold rate) extract(get_all_product_data($product, true)); $ring_size = esc_attr($data['ring_size']); // Get selected "metal purity" $metal_purity = esc_attr($data['metal_purity']); // Get selected "metal purity" // Load data from custom tables for further calculations $metal_purity_percent = get_metal_purity($metal_purity); // Metal Purity percentage $ring_grams_percent = get_ring_grams_percentage($ring_size); $gold_value = (($gold_rate * $metal_purity_percent) / 100); // Calculate gold value // Get calculated updated price if ($def_ring_size < $ring_size) { $net_weight_1 = $net_weight + $ring_grams_percent; $updated_gold_price = $gold_value * $net_weight_1; } elseif ($def_ring_size === $ring_size) { $updated_gold_price = $gold_value * $net_weight; } else { $net_weight_2 = $net_weight - $ring_grams_percent; $updated_gold_price = $gold_value * $net_weight_2;; } $data = json_encode(array( 'raw' => $updated_gold_price, 'html' => get_displayed_price_html($updated_gold_price, $product) )); echo $data; } } } price-calculator.js
jQuery(function ($) { if (typeof wc_price_calculator_params === 'undefined') return false; var ringSize = $('select#ring-size').val(), metalPurity = $('select#metal-purity').val(), diamondQty = $('select#diamond-quality').val(), productId = $('button[name=add-to-cart]').val(); function updateGoldPrice(ringSize, metalPurity, productId) { $.ajax({ type: 'POST', url: wc_price_calculator_params.ajax_url, data: { action: 'update_gold_price', // Add a new action for updating gold price product_data: { product_id: productId, ring_size: ringSize, metal_purity: metalPurity, }, security: wc_price_calculator_params.nonce, }, success: function (res) { var response = $.parseJSON(res); $('input#updated_gold_price').val(response['raw']); $('p.gold-price').html(response['html']); // Update gold p0rice $('form.cart').unblock(); }, error: function (err) { $('form.cart').unblock(); console.log(err); // Log errors }, }); } $('form.cart').on('change', 'select#ring-size', function () { ringSize = $(this).val(); updateGoldPrice(ringSize, metalPurity, productId); }); $('form.cart').on('change', 'select#metal-purity', function () { metalPurity = $(this).val(); updateGoldPrice(ringSize, metalPurity, productId); }); $('form.cart').on('change', 'select#diamond-quality', function () { diamondQty = $(this).val(); sendSelectedData(ringSize, metalPurity, diamondQty, productId); }); });

Источник: https://stackoverflow.com/questions/780 ... ges-in-woo
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»