Jquery Grouped Products Pirce Total根据数量Magento

时间:2014-09-26 10:09:42

标签: php jquery magento product

我正在使用jquery为分组产品定制定价。我想要这样的东西。请查看图片和链接以便更好地理解

http://i.stack.imgur.com/VqJ75.jpg

示例链接:http://www.graduationsource.com/faculty/bachelors/cap-gown-tassel/adult-shiny-black-cap-gown-tassel.html

我的价格表

<?php $this->setPreconfiguredValue(); ?>
<?php $_product = $this->getProduct(); ?>
<?php $_associatedProducts = $this->getAssociatedProducts(); ?>
<?php $_hasAssociatedProducts = count($_associatedProducts) > 0; ?>
<?php if ($this->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable() && $_hasAssociatedProducts): ?>
        <p class="availability in-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('In stock') ?></span></p>
    <?php else: ?>
    <p class="availability out-of-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
<?php endif; ?>
<?php echo $this->getChildHtml('product_type_data_extra') ?>
<table cellpadding="0" cellspacing="0" class="table table-bordered super-product-table1" id="super-product-table">
   <thead>

<tr>
<th valign="top" rowspan="3">Model Number</th>
<th valign="top" rowspan="3">Color</th>
<th valign="top" rowspan="3">Size</th>
<th valign="top" rowspan="3">Quantity per person</th>
<th valign="top" colspan="3">Price PER PERSON</th>
<th colspan="2">#OF CARTONS IN A CONTAINER LOAD</th>
<th colspan="2">CARTON PRICE PER CONTAINER LOAD</th>
<th rowspan="3">WEIGHT PER CARTON (IN LBS)</th>
<th rowspan="3">WANT IT FASTER<br />
  <input type="checkbox" name="input3" /></th>
<th rowspan="3">DO YOU WANT A CONTAINER <input type="checkbox" id="hidecheckb"> </th>
<th colspan="3">ENTER QUANTITY (CARTONS OR CONTAINERS)</th>
</tr>


<tr>
<th valign="top" rowspan="2">1-2</th>
<th rowspan="2">3-5</th>
<th rowspan="2">6+</th>
<th rowspan="2">20 FOOT CONTAINER</th>
<th rowspan="2">40 FOOT CONTAINER</th>
<th rowspan="2">20 FOOT CONTAINER</th>
<th rowspan="2">40 FOOT CONTAINER</th>
<th rowspan="2">NUMBER OF CARTONS <br/></th>
<th valign="top" colspan="2">Number of containers<br/></th>
</tr>
<tr>
<th>20 foot</th>
<th>40 foot</th>
</tr>
</thead>
    <tbody>
    <?php if ($_hasAssociatedProducts): ?>

    <?php $i = 0; foreach ($_associatedProducts as $_item): $i = $i+1; ?>
        <?php $_finalPriceInclTax = $this->helper('tax')->getPrice($_item, $_item->getFinalPrice(), true) ?>
<tr>
<td valign="top"><?php echo $_item->getModel();  ?></td>
<td valign="top"><?php echo $_item->getcolour();  ?></td>
<td valign="top"><select><option value="">Select size</option><?php  $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'select_size'); 
foreach ($attribute->getSource()->getAllOptions(true) as $option) {?>


    <option value="<?php echo $option['value']; ?>"><?php echo $option['label']; ?></option>
<?php } ?>   </select>

</td>
<td valign="top"><?php echo $_item->getQuantityPerCarton();  ?> <input type="hidden" class="QuantityPerCarton" id="QuantityPerCarton<?php echo $i; ?>" value="<?php echo $_item->getQuantityPerCarton();  ?>"></td>
<td valign="top"><?php echo $_item->getPricePerCorton();  ?><input type="hidden" class="PricePerCorton<?php echo $i; ?>" id="PricePerCorton<?php echo $i; ?>" value="<?php echo $_item->getPricePerCorton();  ?>"></td>
<td><?php echo $_item->getPricePerCortonSecond();  ?><input type="hidden" class="PricePerCortonSecond<?php echo $i; ?>" id="PricePerCortonSecond<?php echo $i; ?>" value="<?php echo $_item->getPricePerCortonSecond();  ?>"></td>
<td><?php echo $_item->getPricePerCortonThree();  ?><input type="hidden" class="PricePerCortonThree<?php echo $i; ?>" id="PricePerCortonThree<?php echo $i; ?>" value="<?php echo $_item->getPricePerCortonThree();  ?>"></td>
<td><?php echo $_item->getCartonsFortyContainerLoad();  ?><input type="hidden" class="CartonsFortyContainerLoad<?php echo $i; ?>" id="CartonsFortyContainerLoad<?php echo $i; ?>" value="<?php echo $_item->getCartonsFortyContainerLoad();  ?>"></td>
<td><?php echo $_item->getCartonsTwentyContainerLoad();  ?><input type="hidden" class="CartonsTwentyContainerLoad<?php echo $i; ?>" id="CartonsTwentyContainerLoad<?php echo $i; ?>" value="<?php echo $_item->getCartonsTwentyContainerLoad();  ?>"></td>
<td><?php echo $_item->getTwentyContainerLoadPrice();  ?><input type="hidden" class="TwentyContainerLoadPrice<?php echo $i; ?>" id="TwentyContainerLoadPrice<?php echo $i; ?>" value="<?php echo $_item->getTwentyContainerLoadPrice();  ?>"></td>
<td><?php echo $_item->getFortyContainerLoadPrice();  ?><input type="hidden" class="FortyContainerLoadPrice<?php echo $i; ?>" id="FortyContainerLoadPrice<?php echo $i; ?>" value="<?php echo $_item->getFortyContainerLoadPrice();  ?>"></td>
<td><?php echo $_item->getWeightPerCarton();  ?></td>
<td> $8</td>
<td><label></label></td>

<td> 
<div id="b" style="display:none"> <?php if ($_item->isSaleable()) : ?>
               <input type="text" name="super_group[<?php echo $_item->getId() ?>]" maxlength="12" id="qty<?php echo $i; ?>"  value="<?php echo $_item->getQty()*1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
            <?php else: ?>
                <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
            <?php endif; ?></div></td>

<td><div id="a" style="display:none"><input type="text" name="input"  class="input-text qty"></div></td>
<td><div id="a" style="display:none"><input type="text" name="input2"  class="input-text qty"></div></td>
</tr>
    <?php endforeach; ?>
    <tr>
<td valign="top" colspan="10" rowspan="4">
 <div class="addorderinfbox">
   <div class="addordertxtarea">ADDITIONAL ORDER INSTRUCTIONS<br />
     <textarea></textarea>
   </div>
   <div class="brandcuromdesign"><input type="submit" value="Brand IT" class="branditbtp">  <br/><input type="submit" value="Custom Colors" class="customitp">  <br/><input type="submit" value="Design It" class="designitp"></div>
 </div>
  </td>
<td colspan="2"><select name="select"> <option>Our Brand</option> </select></td>
<td colspan="5">$0</td>
</tr>
<tr>
<td colspan="2"><select name="select2"> <option>No Custom Colors</option> </select></td>
<td colspan="5">$0</td>
</tr>
<tr>
<td colspan="2"><select name="select3"> <option>No special design </option> </select></td>
<td colspan="5">$0</td>
</tr>
<tr>
<td colspan="2">Total</td>
<td colspan="5"><p>$<span id="price">00</span></p></td>
</tr>
<tr>
<td valign="top" colspan="9">
<p>IF YOU CHOSE BRAND IT, CUSTOM COLORS OR DESIGN IT OPTIONS, PLEASE UPLOAD YOUR ARTWORK, LOGO OR COLOR SWATCHES BY CLICK THE “UPLOAD ARTWORK” BUTTON</p>
</td>
<td><input type="submit" value="Upload ArtWork" class="uploadartworkpd"></td>
<td colspan="2"><input type="submit" class="recaluculatebt" value="Recalculate cost"></td>
<td colspan="5"><button onclick="productAddToCartForm.submit(this)" class="button btn-cart validation-passed" type="button" rel="tooltip" data-original-title=""><span><span>Add to Cart</span></span></button></td>
</tr>
    <?php else: ?>
       <tr>
           <td colspan="<?php if ($_product->isSaleable()): ?>4<?php else : ?>3<?php endif; ?>"><?php echo $this->__('No options of this product are available.') ?></td>
       </tr>
    <?php endif; ?>
    </tbody>
</table>

Jquery仅适用于一个数量框

<script>


    jQuery('#qty2').keyup(function(){
    if(jQuery(this).val() != '' && isNumber(jQuery(this).val()) && jQuery(this).val() > 0)
    {
    var QuantityPerCarton =jQuery('#QuantityPerCarton').val() * 1;
    var qty = jQuery(this).val() * 1;

   if (qty <= 3) 
        {
            price = jQuery('#PricePerCorton').val() * 1;
        }
        else if(3 <  qty && qty >= 5) 
        {
            price = jQuery('#PricePerCortonSecond').val() * 1;
        }
        else if(5 <  $qty )  {
            price = jQuery('#PricePerCortonThree').val() * 1;
        }

    var total = price * qty* QuantityPerCarton;
    jQuery('#price').html(total);
    }
    else
    {
        jQuery('#price').html('00');    
    }
});

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
</script>

请帮助!!!

先谢谢


更新

我已经设法让它在一些变化之后工作但是我需要每个产品的所有子总数的总计并且循环不适用于1和3它仅适用于i的值

jQuery('.qty').keyup(function(){
    //get $i from the ID which we know to be 'qty[a number]'

     var values = jQuery(this).val();


    //we can then use that to get the appropriate fields
    if(values != '' && jQuery.isNumeric(values) && values > 0)
    {
   for (var i = 1, limit = 4; i <limit; i++) {


 var QuantityPerCarton =jQuery('#QuantityPerCarton'+i).val() * 1;

    //get the matching QuantityPerCarton value
    var qty = jQuery('#qty'+i).val() * 1;

   if (qty <= 3) 
        {
            price = jQuery('#PricePerCorton'+i).val() * 1;
        }
        else if(3 <  qty && qty <= 5) 
        {
            price = jQuery('#PricePerCortonSecond'+i).val() * 1;
        }
        else if(5 <  $qty )  {
            price = jQuery('#PricePerCortonThree'+i).val() * 1;
        }

    var total = price * qty* QuantityPerCarton;
    jQuery('#price').html(total);
    }

    }
});

1 个答案:

答案 0 :(得分:0)

我看到你给每个数量框一个动态ID

id="qty<?php echo $i; ?>" ...

...但你的脚本只在其中一个上寻找一个键盘

jQuery('#qty2').keyup(function(){...

您需要更新脚本以侦听所有数量框上的密钥。看来他们分享了一个共同的“数量”类别。我们可以使用它:

jQuery('input.qty').keyup(function(){...

更新

除了您的评论之外,还需要一些方法将数量框与相关的隐藏字段联系起来。由于您已将$ i附加到这些字段的ID,我们可以使用:

jQuery('input.qty').keyup(function(){
    //get $i from the ID which we know to be 'qty[a number]'
    var $i = jQuery(this).attr("id").replace('qty','');
    //we can then use that to get the appropriate fields
    if(jQuery(this).val() != '' && isNumber(jQuery(this).val()) && jQuery(this).val() > 0)
    {
    var QuantityPerCarton =jQuery('#QuantityPerCarton'+$i).val() * 1;//get the matching QuantityPerCarton value
    var qty = jQuery(this).val() * 1;

   if (qty <= 3) 
        {
            price = jQuery('#PricePerCorton'+$i).val() * 1;
        }
        else if(3 <  qty && qty >= 5) 
        {
            price = jQuery('#PricePerCortonSecond'+$i).val() * 1;
        }
        else if(5 <  $qty )  {
            price = jQuery('#PricePerCortonThree'+$i).val() * 1;
        }

    var total = price * qty* QuantityPerCarton;
    jQuery('#price').html(total);
    }
    else
    {
        jQuery('#price').html('00');    
    }
});

更新2

在您更新的问题中,您有一个从1到4的循环。您可以随时添加总计价格:

jQuery('.qty').keyup(function(){
    var values = jQuery(this).val();
    var grandTotal = 0;
    if(values != '' && jQuery.isNumeric(values) && values > 0){        
        for (i=1; <=4; i++) {
            var QuantityPerCarton =jQuery('#QuantityPerCarton'+i).val() * 1;
            //get the matching QuantityPerCarton value
            var qty = jQuery('#qty'+i).val() * 1;
            if (qty <= 3){
                price = jQuery('#PricePerCorton'+i).val() * 1;
            } else if(3 < qty && qty <= 5) {
                price = jQuery('#PricePerCortonSecond'+i).val() * 1;
            }
            else if(5 < $qty ) {
                price = jQuery('#PricePerCortonThree'+i).val() * 1;
            }                
            var subtotal = price * qty* QuantityPerCarton;
            grandTotal += subtotal;       
        }//end of loop
    jQuery('#price').html(grandTotal);    
    }
});