[转]magento 产品详细页面添加购物车数量实时更新总价格

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php $_product = $this->getProduct(); ?>

 <?php
        $sku = nl2br($_product->getSku());
        $_productCollection = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToFilter('sku', $sku)
            ->setOrder('ordered_qty', 'desc')
            ->getFirstItem();
            $product = $_productCollection;
             $ordered_qty = (int)$product->ordered_qty;
             $inventory_qty = $this->htmlEscape((int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()) ;

 ?>
<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
             <label for="qty"><?php echo $this->__('Qty:') ?></label>
             <a id="qty-dec">-</a>
                 <input type="text" name="qty" οninput="OnInput(event)"  onpropertychange="OnPropChanged(event)" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
              <a id="qty-inc">+</a>
        <?php endif; ?>

        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
     <?php
         $price=$_product->getPrice();
         $finprice=$_product->getFinalPrice();
         if($price==$finprice){
            $productprice=round($price,2);
         }
         else{
            $productprice=round($finprice,2);
         }
     ?>

    <div class="totalPrice_box">
        <p class="totalPrice_txt">
            Total Price:<?php echo  Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();?>
            <span class="pro_total" id="pro_total">
                    <?php echo round($productprice,2);?>
            </span>
        </p>
    </div>
    <button type="button" title="<?php echo $buttonTitle ?>" class="add-cart-btn" οnclick="productAddToCartForm.submit(this)">
                    <span><span><?php //echo $this->__('Add to Cart') ?></span></span>
    </button>
    <button id="checknow" type="button" title="<?php echo $this->__('Buy Now') ?>" class="button buynow_cart btn-cart" >
        <span>
        <?php //echo $this->__('Buy Now') ?></span>
    </button>
    <input type="hidden" name="return_url" id="return_url" value="" />
<?php endif; ?>

<script type="text/javascript" src="http://www.mkyong.com/wp-content/uploads/jQuery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
    //<![CDATA[
      jQuery("#qty-inc").click( function () {

        var qty=Number(jQuery("#qty").val());
        if(qty<999)
        {
          jQuery("#qty").val(qty+1);
           var prices=<?php echo $productprice?>*(qty+1);
          var price=prices.toFixed(2);
          jQuery("#pro_total").html(price);
        }
        else
        {
         jQuery("#qty").val(999);
         //jQuery("#pro_total").html(<?php echo $productprice?>*qty);
        }

      });
      jQuery("#qty-dec").click( function () {
        var qty=Number(jQuery("#qty").val());
        if(qty>1)
        {
          jQuery("#qty").val(qty-1);
          var prices=<?php echo $productprice?>*(qty-1);
          var price=prices.toFixed(2);
          jQuery("#pro_total").html(price);
        }
        else
        {
         jQuery("#qty").val(1);
          var price=prices.toFixed(2);
          var prices=<?php echo $productprice?>*qty;
          jQuery("#pro_total").html(price);
        }
      });
        $("#qty").blur(function () {

        var qtys=$('#qty').val();
             var pr=<?php echo $productprice?>*qtys;
             var pri=pr.toFixed(2);
             jQuery("#pro_total").html(pri);
         });



          function OnPropChanged (event) {
             var qtys=$('#qty').val();
             var pr=<?php echo $productprice?>*qtys;
             var pri=pr.toFixed(2);
             jQuery("#pro_total").html(pri);
        }
         function OnInput(event) {
              var qtys=$('#qty').val();
              var pr=<?php echo $productprice?>*qtys;
              var pri=pr.toFixed(2);
              jQuery("#pro_total").html(pri);
        }
    //]]>
</script>

<script>
 jQuery("#checknow").click(function(){
        jQuery("input#return_url").val("<?php echo $this->getUrl('checkout/onepage/'); ?>");
        productAddToCartForm.submit();
  })
</script>

From: https://blog.csdn.net/petter7226/article/details/48677569

 

posted @ 2021-04-14 14:09  ec04  阅读(85)  评论(0编辑  收藏  举报