Magento仿兰亭倒计时效果


View.php文件

<?php
class Light_Countdown_Block_Product_View extends Mage_Catalog_Block_Product_Abstract{

	private $_startTime = null;

	public function __construct(){
		$this->_template = 'countdown/product/view.phtml';
	}

	public function getStartTime(){
		return json_encode($this->_startTime*1000);
	}

	/**
	 *
	 * Enter description here ...
	 * @return Mage_Customer_Model_Session
	 */
	public function getCustomer(){
		return Mage::getSingleton('customer/session');
	}

	public function hasCatalogRule(){
		if(!$this->getCustomer()->isLoggedIn()){
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param Mage_Catalog_Model_Product $product
	 * @return string
	 */
	public function getToTime(Mage_Catalog_Model_Product $product){
		$specialTo = $product->getSpecialToDate();
		if($specialTo){
			$specialTo = date('Y-m-d H:i:s',strtotime('+1 day',strtotime($specialTo)));
		}
		return $specialTo;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param Mage_Catalog_Model_Product $product
	 * @return string
	 */
	public function getFromTime(Mage_Catalog_Model_Product $product){
		$specialFrom = $product->getSpecialFromDate();
		return $specialFrom;
	}
	
	public function isAvailableSpecial(){
		
		$_date = Mage::getModel('core/date')->date();
		$specialFrom = $this->getProduct()->getSpecialFromDate();
		$specialTo = $this->getToTime($this->getProduct());
		if(!$specialFrom){
			return false;
		}
		if($this->hasCatalogRule()){
			$storeId = $this->getProduct()->getStoreId();
			$finalPrice = Mage::getResourceModel('catalogrule/rule')
			->getRulePrice(Mage::app()->getLocale()->storeTimeStamp($this->getStore($storeId)), $this->getWebsiteId($storeId), $this->getCustomerGroupId(), $this->getProduct()->getId());
			$specialPrice = $this->getProduct()->getSpecialPrice();
			
			if($finalPrice!=$specialPrice&&$finalPrice<$specialPrice){
				return false;
			}
		}
		
		if(!$specialTo||!$specialFrom||($specialFrom&&$specialTo&&$specialFrom<$specialTo)){
			if($specialFrom){
				if(($specialFrom<$_date&&$specialTo>$_date)||($specialFrom<$_date&&!$specialTo)){
					return true;
				}
			}else{
				if($specialTo>$_date||!$specialTo)
				{
					return true;
				}
			}
		}
		return false;
	}

	public function getSavePrice(){
		$product = $this->getProduct();
		/* @var $product Mage_Catalog_Model_Product */
		$specialPrice = $product->getSpecialPrice();		
		if($this->hasCatalogRule()){
			$finalPrice = Mage::getResourceModel('catalogrule/rule')
			->getRulePrice(Mage::app()->getLocale()->storeTimeStamp($this->getStore($storeId)), $this->getWebsiteId($storeId), $this->getCustomerGroupId(), $this->getProduct()->getId());
			$finalPrice = $finalPrice>$specialPrice?$specialPrice:$finalPrice;
		}else{
			$finalPrice = $specialPrice;
		}
		
		$discount = $product->getPrice()-$finalPrice;
		return Mage::app()->getStore()->getCurrentCurrencyCode().' '.$this->helper('core')->currency(number_format($discount,2), true, false);
	}

	public function getStore($storeId){
		return Mage::app()->getStore($storeId);
	}

	public function getWebsiteId($storeId){
		return Mage::app()->getStore($storeId)->getWebsiteId();
	}
	public function getCustomerGroupId(){
		return Mage::getSingleton('customer/session')->getCustomerGroupId();
	}
	public function getSavePercent(){
		$product = $this->getProduct();
		$storeId = $product->getStoreId();
		/* @var $product Mage_Catalog_Model_Product */
		$specialPrice = $product->getSpecialPrice();		
		if($this->hasCatalogRule()){
			$finalPrice = Mage::getResourceModel('catalogrule/rule')
			->getRulePrice(Mage::app()->getLocale()->storeTimeStamp($this->getStore($storeId)), $this->getWebsiteId($storeId), $this->getCustomerGroupId(), $this->getProduct()->getId());
			$finalPrice = $finalPrice>$specialPrice?$specialPrice:$finalPrice;
		}else{
			$finalPrice = $specialPrice;
		}
		$discount = $product->getPrice()-$finalPrice;
		return number_format($discount*100/$product->getPrice(),2).'%';
	}

	public function getSpecialFromDate(){
		return $this->getProduct()->getSpecialFromDate();
	}

	public function getSpecialToDate(){
		return $this->getProduct()->getSpecialToDate();
	}

	/**
	 * estimate whether count down should be shown
	 * @return bool
	 */
	public function canShowCountdown($type=null){
		$product = $this->getProduct();
		
		/* @var $product Mage_Catalog_Model_Product */
		$fromDate = $product->getSpecialFromDate();
		$toDate = $this->getToTime($product);
		if($this->isAvailableSpecial()){
			if($this->hasCatalogRule()){
				$specialPrice = $product->getSpecialPrice();
				$finalPrice = Mage::getResourceModel('catalogrule/rule')
				->getRulePrice(Mage::app()->getLocale()->storeTimeStamp($this->getStore($storeId)), $this->getWebsiteId($storeId), $this->getCustomerGroupId(), $this->getProduct()->getId());
				if($finalPrice<$specialPrice){
					return false;
				}
			}
			if($toDate){
				$date = Mage::getModel('core/date')->date('Y-m-d H:i:s');
				$array = $this->dateSubtract($date, $toDate);
				if($type&&$type===1){
					if($array['year']>0||$array['month']>0||$array['day']>10){
						return true;
					}
				}
				if($type&&$type===2){
					if($array['year']===0&&$array['month']===0&&$array['day']<=10&&$array['day']>0){
						return true;
					}
				}
				if($type&&$type===3){
					if($array['year']===0&&$array['month']===0&&$array['day']==0&&($array['hour']>0||$array['minute']>0||$array['second']>0)){
						return true;
					}
				}
			}else{
				if($type&&$type===1&&$fromDate){
					return true;
				}
			}
		}
		return false;
	}

	public function getPosition(){
		$product = $this->getProduct();
		/* @var $product Mage_Catalog_Model_Product */
		$fromDate = $product->getSpecialFromDate();
		$toDate = $this->getToTime($product);
		
		$timeArray = array();
		$date = Mage::getModel('core/date')->date('Y-m-d H:i:s');
		$array = $this->dateSubtract($date, $toDate);
		$day = $array['day']%10;
		$timeArray['day'] = $this->getCoor($day,120);
		$hour = $array['hour'];
		if(strlen((string)$hour)==1){
			$hour = '0'.$hour;
		}
		$h1 = substr($hour, 0,1);
		$h2 = substr($hour, 1,1);
		$timeArray['hour'] = array(
		$this->getCoor($h1),
		$this->getCoor($h2)
		);
		$minute = $array['minute'];
		if(strlen((string)$minute)==1){
			$minute = '0'.$minute;
		}
		$m1 = substr($minute, 0,1);
		$m2 = substr($minute, 1,1);
		$timeArray['minute'] = array(
		$this->getCoor($m1),
		$this->getCoor($m2)
		);
		$second = $array['second'];
		if(strlen((string)$second)==1){
			$second = '0'.$second;
		}
		$s1 = substr($second, 0,1);
		$s2 = substr($second, 1,1);
		$timeArray['second'] = array(
		$this->getCoor($s1),
		$this->getCoor($s2)
		);

		return $timeArray;
	}
	public function getCoor($num,$x=0,$y=0){
		$x_t = $x;
		$y_t = 30*$num;
		$x_b = $x;
		$y_b = 300+30*$num;
		return array(
			't'=>array('x'=>$x_t,'y'=>$y_t),
			'b'=>array('x'=>$x_b,'y'=>$y_b)
		);
	}
	/**
	 *
	 * two date subtract
	 * @param datetime $date1
	 * @param datetime $date2
	 * @return Array
	 */
	public function dateSubtract($date1,$date2){
		$d1 = new Zend_Date($date1);
		$d2 = new Zend_Date($date2);
		
		$mod = Mage::getModel('core/date');
		/*@var $mod Mage_Core_Model_Date */
		$d3 = new Zend_Date($d2->getTimestamp()-$d1->getTimestamp());
		$this->_startTime = Mage::getModel('core/date')->timestamp(Mage::getModel('core/date')->date(null,$d3));
		$array = $d3->toArray();
		$array['year'] -=1970;
		$array['month'] -=1;
		$array['day'] -=1;
		return $array;
	}

	/**
	 * estimate whether day count down should be shown
	 * @return bool
	 */
	public function canShowDayCountdown(){
		$d = new Zend_Date($this->_startTime);
		$array = $d->toArray();
	}

	/**
	 * estimate whether hour count down should be shown
	 * @return bool
	 */
	public function canShowHourCountdown(){
		if(!$this->canShowDayCountdown()){
			$product = $this->getProduct();
			/* @var $product Mage_Catalog_Model_Product */
			$fromDate = $product->getSpecialFromDate();
			$toDate = $product->getSpecialToDate();
			$array = $this->dateSubtract($fromDate, $toDate);
		}
		return false;
	}
}

模板文件view.phtml

<?php /* @var $this Light_Countdown_Block_Product_View */?>
<?php $product = $this->getProduct();?>
<?php /* @var $product Mage_Catalog_Model_Product */ ?>
<?php if($this->isAvailableSpecial()):?>
	<div class="w-discountCountDown_N">
		<div class="save_price">You Save: <br>
			<span><?php echo $this->getSavePrice();?></span>
			<em></em><i></i>
		</div>
    	<div class="price_off"><span><?php echo $this->getSavePercent() ?></span>Off</div>
    	<?php if($this->canShowCountdown(1)):?>
	    	<div endtime="<?php echo $this->getSpecialToDate()?>" class="pro_count moreDays" id="pro_slime">
				<div class="remaining">remaining<i></i></div>
			</div>
		<?php endif;?>
		<?php if($this->canShowCountdown(2)||$this->canShowCountdown(3)):?>
		<?php $array = $this->getPosition()?>
    	<div endtime="<?php echo $this->getSpecialToDate();?>" class="pro_count days <?php echo $this->canShowCountdown(3)?'times':''?>" id="pro_slime">
    		<ul class="cd" id="pro_slime_d6">
    			<li id="pro_slime_t_d6" class="day_t" style="background-position: -<?php echo $array['day']['t']['x']?>px -<?php echo $array['day']['t']['y']?>px;"></li>
    			<li id="pro_slime_b_d6" class="day_b" style="background-position: -<?php echo $array['day']['b']['x']?>px -<?php echo $array['day']['b']['y']?>px;"></li>
    		</ul>
    		<ul class="cd">
    			<li class="days">DAYS</li>
    		</ul>
    		<ul class="cd" id="pro_slime_d5">
    			<li id="pro_slime_t_d5" class="t" style="background-position: -<?php echo $array['hour'][0]['t']['x']?>px -<?php echo $array['hour'][0]['t']['y']?>px;"></li>
    			<li id="pro_slime_b_d5" class="b" style="background-position: -<?php echo $array['hour'][0]['b']['x']?>px -<?php echo $array['hour'][0]['b']['y']?>px;"></li>
    		</ul>
    		<ul class="cd" id="pro_slime_d4">
    			<li id="pro_slime_t_d4" class="t" style="background-position: -<?php echo $array['hour'][1]['t']['x']?>px -<?php echo $array['hour'][1]['t']['y']?>px;"></li>
    			<li id="pro_slime_b_d4" class="b" style="background-position: -<?php echo $array['hour'][1]['b']['x']?>px -<?php echo $array['hour'][1]['b']['y']?>px;"></li>
    		</ul>
    		<ul class="cd">
    			<li class="s"></li>
    		</ul>
    		<ul class="cd" id="pro_slime_d3">
    			<li id="pro_slime_t_d3" class="t" style="background-position: -<?php echo $array['minute'][0]['t']['x']?>px -<?php echo $array['minute'][0]['t']['y']?>px;"></li>
    			<li id="pro_slime_b_d3" class="b" style="background-position: -<?php echo $array['minute'][0]['b']['x']?>px -<?php echo $array['minute'][0]['b']['y']?>px;"></li>
    		</ul>
    		<ul class="cd" id="pro_slime_d2">
    			<li id="pro_slime_t_d2" class="t" style="background-position: -<?php echo $array['minute'][1]['t']['x']?>px -<?php echo $array['minute'][1]['t']['y']?>px;"></li>
    			<li id="pro_slime_b_d2" class="b" style="background-position: -<?php echo $array['minute'][1]['b']['x']?>px -<?php echo $array['minute'][1]['b']['y']?>px;"></li>
    		</ul>
    		<ul class="cd">
    			<li class="s"></li>
    		</ul>
    		<ul class="cd" id="pro_slime_d1">
    			<li id="pro_slime_t_d1" class="t" style="background-position: -<?php echo $array['second'][0]['t']['x']?>px -<?php echo $array['second'][0]['t']['y']?>px;"></li>
    			<li id="pro_slime_b_d1" class="b" style="background-position: -<?php echo $array['second'][0]['b']['x']?>px -<?php echo $array['second'][0]['b']['y']?>px;"></li>
    		</ul>
    		<ul class="cd" id="pro_slime_d0">
    			<li id="pro_slime_t_d0" class="t" style="background-position: -<?php echo $array['second'][1]['t']['x']?>px -<?php echo $array['second'][1]['t']['y']?>px;"></li>
    			<li id="pro_slime_b_d0" class="b" style="background-position: -<?php echo $array['second'][1]['b']['x']?>px -<?php echo $array['second'][1]['b']['y']?>px;"></li>
    		</ul>
			<div class="remaining">remaining<i></i></div>
		</div>
		<?php endif;?>
	</div>
<?php endif;?>
<script>
	var startTime = eval(<?php echo $this->getStartTime();?>);
	new Special({startTime : startTime});
</script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值