Jump to content

Recommended Posts

Доброго времени суток. Вывел опции в модули рекомендуемые, акции и хиты продаж и подключил скрипт для пересчета цены при выборе опций. Сам скрипт под спойлером. Но получилась непонятная ситуация: в рекомендуемых все отлично работает и цена считается, а в акциях и хитах продаж получаю NaN вместо цены, хотя параметры передаются вроде такие же, как и в рекомендуемых. Как выяснить проблему?

 

 

Скрипт

function price_format(n)
{ 
    c = <?php echo (empty($currency['decimals']) ? "0" : $currency['decimals'] ); ?>;
    d = '<?php echo $currency['decimal_point']; ?>'; // decimal separator
    t = '<?php echo $currency['thousand_point']; ?>'; // thousands separator
    s_left = '<?php echo $currency['symbol_left']; ?>';
    s_right = '<?php echo $currency['symbol_right']; ?>';
      
    n = n * <?php echo $currency['value']; ?>;

    //sign = (n < 0) ? '-' : '';

    //extracting the absolute value of the integer part of the number and converting to string
    i = parseInt(n = Math.abs(n).toFixed(c)) + ''; 

    j = ((j = i.length) > 3) ? j % 3 : 0; 
    return s_left + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '') + s_right; 
}

function calc_price(pid)
{
    var main_price = Number($('#formated_price_'+pid).attr('price'));
    var special = Number($('#formated_special_'+pid).attr('price'));
      
    var options_price = 0;
    
    $('#option_'+pid+' option:selected').each(function() {
      if ($(this).attr('price_prefix') == '=') {
        main_price = Number($(this).attr('price'));
        special = main_price;
        // tax = main_price;
      }
    });

    $('#option_'+pid+' option:selected').each(function() {
      if ($(this).attr('price_prefix') == '*') {
        main_price = main_price * Number($(this).attr('price'));
        special = special * Number($(this).attr('price'));
        // tax = tax * Number($(this).attr('price'));
      }
    });

    $('#option_'+pid+' option:selected').each(function() {
      if ($(this).attr('price_prefix') == '+')
        options_price = options_price + Number($(this).attr('price'));
      if ($(this).attr('price_prefix') == '-')
        options_price = options_price - Number($(this).attr('price'));
    });

    // Calculate new price (With options).
    var new_price = main_price + options_price;
    var new_special = special + options_price;
	      
        
    // Display Main Price
    $('#formated_price_'+pid).html( price_format(new_price) );

    $('#formated_special_'+pid).html( price_format(new_special) );
}

<?php foreach ($products as $product) { ?>
  calc_price(<?php echo $product['product_id']; ?>);
<?php } ?> 

 

Модуль Акции, после компиляции кода в браузере

<div class="box">
  <div class="box-heading">Акции</div>
 <div class="box-content">
    <div id="box-product">
            <div>
                <div class="image"><a href="http://opencart/index.php?route=product/product&product_id=80"><img src="http://opencart/image/cache/data/10339-250x300.png" alt="Test Product" /></a></div>
                <div class="name"><a href="http://opencart/index.php?route=product/product&product_id=80">Test Product</a></div>
                <div class="price">
                <span class="price-old">2 000 р.</span><span class="price-new"><span id="formated_special_80" price="1200.0000">1 200 р.</span></span>
              </div>

<div class="cart">
<div class="options">
<div id="option_80">
                                                                                            <div id="option-248" class="option">
<select name="option[250]" onchange="calc_price(82);" >
                                                                                            <option value="64"price_prefix="+" price="280 р.">0.7 кг</option>
                                                                                            <option value="65"price_prefix="=" price="1 444 р.">1.84 кг</option>
                                                                                            <option value="66"price_prefix="+" price="200 р.">2.72 кг</option>
</select>
</div>
                                                                                            </div>
                                                                                                            </div>     
        <input type="button" value="В корзину" class="button" />
        </div>
      </div>

 

 

Скрипт, после компиляции кода в браузере

<script type="text/javascript"><!--

function price_format(n)
{ 
    c = 0;
    d = '.'; // decimal separator
    t = ' '; // thousands separator
    s_left = '';
    s_right = ' р.';
      
    n = n * 1.00000000;

    //sign = (n < 0) ? '-' : '';

    //extracting the absolute value of the integer part of the number and converting to string
    i = parseInt(n = Math.abs(n).toFixed(c)) + ''; 

    j = ((j = i.length) > 3) ? j % 3 : 0; 
    return s_left + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '') + s_right; 
}

function calc_price(pid)
{
    var main_price = Number($('#formated_price_'+pid).attr('price'));
    var special = Number($('#formated_special_'+pid).attr('price'));
      
    var options_price = 0;
    
    $('#option_'+pid+' option:selected').each(function() {
      if ($(this).attr('price_prefix') == '=') {
        main_price = Number($(this).attr('price'));
        special = main_price;
        // tax = main_price;
      }
    });

    $('#option_'+pid+' option:selected').each(function() {
      if ($(this).attr('price_prefix') == '*') {
        main_price = main_price * Number($(this).attr('price'));
        special = special * Number($(this).attr('price'));
        // tax = tax * Number($(this).attr('price'));
      }
    });

    $('#option_'+pid+' option:selected').each(function() {
      if ($(this).attr('price_prefix') == '+')
        options_price = options_price + Number($(this).attr('price'));
      if ($(this).attr('price_prefix') == '-')
        options_price = options_price - Number($(this).attr('price'));
    });

    // Calculate new price (With options).
    var new_price = main_price + options_price;
    var new_special = special + options_price;
        
    // Display Main Price
    $('#formated_price_'+pid).html( price_format(new_price) );

    $('#formated_special_'+pid).html( price_format(new_special) );
}

  calc_price(80);
  calc_price(78);
  calc_price(77);
  calc_price(79);
  calc_price(82);

//--></script> 
Edited by mindthegap

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.