0

我试图让我的购物车仅在该特定网站/商店(其中我有两个)有可用的有效折扣代码时才显示折扣代码框。

这就是我到目前为止所得到的(包含在我自己的副本中coupon.phtml

<?php
/** @var $coupon Mage_SalesRule_Model_Coupon */
$coupon = Mage::getModel('salesrule/coupon');
$validCoupons = $coupon->getCollection()
    ->addFieldToFilter('expiration_date', array('gt' => NOW()))
    ->count();
?>

<?php if($validCoupons > 0): ?>
    <!-- discount code form here -->
<?php endif; ?>

如果您对检查所有网站/商店的折扣代码感到满意,这很好用,但我想更新它,以便只考虑当前的网站/商店。

4

1 回答 1

2

我认为更好的方法是检查是否有使用优惠券的折扣规则。因为折扣规则可以有一个指定的优惠券而不是优惠券列表。为此,请使用以下代码:

$coupon = Mage::getModel('salesrule/rule');
$todayEndOfDayDate =  Mage::app()->getLocale()->date()
    ->setTime('23:59:59')
    ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayStartOfDayDate  = Mage::app()->getLocale()->date()
    ->setTime('00:00:00')
    ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$validCouponRules = $coupon->getCollection()
    ->addFieldToFilter('coupon_type', 2)//rules that have coupons fixed or generated
    ->addFieldToFilter('website_ids', array('finset'=>Mage::app()->getWebsite()->getId())) //filter rules available for current website 
    ->addFieldToFilter('to_date', array('or'=> array(
        0 => array('date' => true, 'from' => $todayEndOfDayDate), // filter rules that end later than today
        1 => array('is' => new Zend_Db_Expr('null'))) //or rules that don't have an end date
    ), 'left')
    ->addFieldToFilter('from_date', array('or'=> array(
        0 => array('date' => true, 'to' => $todayStartOfDayDate), //filter rules that started today or earlier
        1 => array('is' => new Zend_Db_Expr('null'))) // or rules that don't have a start date
    ), 'left');
$validCoupons = $validCouponRules->count();
于 2013-09-05T14:29:36.303 回答