本文整理汇总了PHP中Mage_SalesRule_Model_Rule类的典型用法代码示例。如果您正苦于以下问题:PHP Mage_SalesRule_Model_Rule类的具体用法?PHP Mage_SalesRule_Model_Rule怎么用?PHP Mage_SalesRule_Model_Rule使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Mage_SalesRule_Model_Rule类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _beforeSave
/**
* Processing object before save data
*
* @return Mage_Core_Model_Abstract
*/
protected function _beforeSave()
{
if (!$this->getRuleId() && $this->_rule instanceof Mage_SalesRule_Model_Rule) {
$this->setRuleId($this->_rule->getId());
}
return parent::_beforeSave();
}
示例2: addRuleToFilter
/**
* Add rule to filter
*
* @param Mage_SalesRule_Model_Rule|int $rule
*
* @return Mage_SalesRule_Model_Resource_Coupon_Collection
*/
public function addRuleToFilter($rule)
{
if ($rule instanceof Mage_SalesRule_Model_Rule) {
$ruleId = $rule->getId();
} else {
$ruleId = (int) $rule;
}
$this->addFieldToFilter('rule_id', $ruleId);
return $this;
}
示例3: _getItemQty
/**
* Return discount item qty
*
* @param Mage_Sales_Model_Quote_Item_Abstract $item
* @param Mage_SalesRule_Model_Rule $rule
* @return int
*/
protected function _getItemQty($item, $rule)
{
//@nelkaake: Magento 1.3 and lower doesnt have the item->getTotalQty attribute.
if (Mage::helper('rewards/version')->isBaseMageVersionAtLeast('1.4')) {
$qty = $item->getTotalQty();
} else {
$qty = $item->getQty();
if ($item->getParentItem()) {
$qty *= $item->getParentItem()->getQty();
}
}
return $rule->getDiscountQty() ? min($qty, $rule->getDiscountQty()) : $qty;
}
示例4: handle
/**
* @param Mage_Sales_Model_Quote_Address $address
* @param Mage_SalesRule_Model_Rule $rule
* @param Mage_Sales_Model_Quote_Item_Abstract[] $allItems
* @param Mage_Sales_Model_Quote_Item_Abstract[] $validItems
*
* @return bool
*/
public function handle(Mage_Sales_Model_Quote_Address $address, Mage_SalesRule_Model_Rule $rule, array $allItems, array $validItems)
{
if ($rule->getSimpleAction() !== Mage_SalesRule_Model_Rule::BUY_X_GET_Y_ACTION) {
return false;
}
$helper = $this->getHelper();
// Get the X and Y values
$x = max(floatval($rule->getDiscountStep()), 0.0);
$y = max(floatval($rule->getDiscountAmount()), 0.0);
if ($x <= 0.0 || $y <= 0.0) {
return false;
}
// Get the discount step size
$step = $x + $y;
$applied = false;
foreach ($validItems as $item) {
// Get max quantity
$qty = $helper->getItemRuleQty($item, $rule);
// Apply discount step size limitation
$qty = max($qty - ceil($qty / $step) * $x, 0.0);
if ($qty <= 0.0) {
continue;
}
$applied = true;
// Get unit prices
$itemPrice = $helper->getItemPrice($item);
$itemBasePrice = $helper->getItemBasePrice($item);
$itemOriginalPrice = $helper->getItemOriginalPrice($item);
$itemBaseOriginalPrice = $helper->getItemBaseOriginalPrice($item);
// Calculate discount amounts
$discountAmount = $itemPrice * $qty;
$originalDiscountAmount = $itemOriginalPrice * $qty;
$baseDiscountAmount = $itemBasePrice * $qty;
$baseOriginalDiscountAmount = $itemBaseOriginalPrice * $qty;
// Round the discount amounts
$discountAmount = $helper->round($discountAmount, $address->getQuote()->getQuoteCurrencyCode());
$baseDiscountAmount = $helper->round($baseDiscountAmount, $address->getQuote()->getBaseCurrencyCode());
$originalDiscountAmount = $helper->round($originalDiscountAmount, $address->getQuote()->getQuoteCurrencyCode());
$baseOriginalDiscountAmount = $helper->round($baseOriginalDiscountAmount, $address->getQuote()->getBaseCurrencyCode());
// Update the item discounts
$item->setDiscountAmount($item->getDiscountAmount() + $discountAmount);
$item->setBaseDiscountAmount($item->getBaseDiscountAmount() + $baseDiscountAmount);
$item->setOriginalDiscountAmount($item->getOriginalDiscountAmount() + $originalDiscountAmount);
$item->setBaseOriginalDiscountAmount($item->getBaseOriginalDiscountAmount() + $baseOriginalDiscountAmount);
}
return $applied;
}
示例5: getDiscountAmount
public function getDiscountAmount()
{
if (substr($this->getCouponCode(), 0, 6) == 'points') {
$step = Mage::getStoreConfig('rewardpoints/default/points_money', Mage::app()->getStore()->getId());
return $this->getPointsAmt() / $step;
}
$test = new Mage_SalesRule_Model_Rule();
if (method_exists($test, 'getDiscountAmount')) {
return parent::getDiscountAmount();
}
if ($this->discount_amount) {
return $this->discount_amount;
}
}
示例6: isValidRuleUsageForAddress
/**
* Validate the rule usage for given address
* @param Mage_SalesRule_Model_Rule
* @param Mage_Sales_Model_Quote_Address
* @return bool
*/
protected function isValidRuleUsageForAddress(Mage_SalesRule_Model_Rule $rule, Mage_Sales_Model_Quote_Address $address)
{
$ruleId = $rule->getId();
$customerId = $address->getQuote()->getCustomerId();
$ruleCustomer = $this->getRuleCustomerModel();
$ruleCustomer->loadByCustomerRule($customerId, $ruleId);
if ($ruleCustomer->getId() && $ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
return false;
}
return true;
}
示例7: handle
/**
* @param Mage_Sales_Model_Quote_Address $address
* @param Mage_SalesRule_Model_Rule $rule
* @param Mage_Sales_Model_Quote_Item_Abstract[] $allItems
* @param Mage_Sales_Model_Quote_Item_Abstract[] $validItems
*
* @return bool
*/
public function handle(Mage_Sales_Model_Quote_Address $address, Mage_SalesRule_Model_Rule $rule, array $allItems, array $validItems)
{
// Skip invalid rule actions
if ($rule->getSimpleAction() !== Mage_SalesRule_Model_Rule::CART_FIXED_ACTION) {
return false;
}
// Skip if there are no valid items and it's not applied to shipping
if (!count($validItems) && !$rule->getApplyToShipping()) {
return false;
}
// Define a few helpful variables
$helper = $this->getHelper();
$quote = $address->getQuote();
$store = $quote->getStore();
// Total available discount amounts
$baseDiscountAmount = $helper->round($rule->getDiscountAmount(), $quote->getBaseCurrencyCode());
$discountAmount = $helper->round($store->convertPrice($baseDiscountAmount), $quote->getQuoteCurrencyCode());
// Skip zero discounts
if ($discountAmount <= 0.0) {
return false;
}
$applied = false;
// Pre-calculate the totals for all valid items
$ruleTotalItemsPrice = 0;
$ruleTotalBaseItemsPrice = 0;
$itemPrices = [];
foreach ($validItems as $item) {
// Get max quantity (min or rule max qty or item qty)
$qty = $helper->getItemRuleQty($item, $rule);
// Skip zero quantity
if ($qty <= 0.0) {
continue;
}
// Get unit price
$itemPrice = $helper->getItemPrice($item);
$itemBasePrice = $helper->getItemBasePrice($item);
// Get row price
$itemRowPrice = $itemPrice * $item->getTotalQty();
$itemBaseRowPrice = $itemBasePrice * $item->getTotalQty();
// Get discountable price
$itemDiscountablePrice = $itemPrice * $qty;
$itemBaseDiscountablePrice = $itemBasePrice * $qty;
// Save price data for later
$itemPrices[$item->getId()] = [$itemPrice, $itemBasePrice, $itemRowPrice, $itemBaseRowPrice, $itemDiscountablePrice, $itemBaseDiscountablePrice];
// Add row prices to running totals
$ruleTotalItemsPrice += $itemDiscountablePrice;
$ruleTotalBaseItemsPrice += $itemBaseDiscountablePrice;
}
$startDiscountAmount = $discountAmount;
$startBaseDiscountAmount = $baseDiscountAmount;
foreach ($validItems as $item) {
// Skip the skipped items
if (!isset($itemPrices[$item->getId()])) {
continue;
}
// Flag indicating the rule was applied
$applied = true;
// Extract the pre-calculate price data
list($itemPrice, $itemBasePrice, $itemRowPrice, $itemBaseRowPrice, $itemDiscountablePrice, $itemBaseDiscountablePrice) = $itemPrices[$item->getId()];
// Calculate remaining row amount
$itemRemainingRowPrice = max($itemRowPrice - $item->getDiscountAmount(), 0);
$itemRemainingBaseRowPrice = max($itemBaseRowPrice - $item->getBaseDiscountAmount(), 0);
// Calculate price factor
$priceFactor = $itemDiscountablePrice / $ruleTotalItemsPrice;
$basePriceFactor = $itemBaseDiscountablePrice / $ruleTotalBaseItemsPrice;
// Calculate (and round) the item discount amount
$itemDiscountAmount = $helper->round($startDiscountAmount * $priceFactor, $quote->getQuoteCurrencyCode());
$itemBaseDiscountAmount = $helper->round($startBaseDiscountAmount * $basePriceFactor, $quote->getBaseCurrencyCode());
// Ensure discount does not exceed the remaining discount, max item discount, or remaining row price
$itemDiscountAmount = max(min($itemDiscountAmount, $discountAmount, $itemDiscountablePrice, $itemRemainingRowPrice), 0.0);
$itemBaseDiscountAmount = max(min($itemBaseDiscountAmount, $baseDiscountAmount, $itemBaseDiscountablePrice, $itemRemainingBaseRowPrice), 0.0);
// Update the item discount
$item->setDiscountAmount($item->getDiscountAmount() + $itemDiscountAmount);
$item->setBaseDiscountAmount($item->getBaseDiscountAmount() + $itemBaseDiscountAmount);
// This is a bit wonky, but needed for taxes
$item->setOriginalDiscountAmount($item->getOriginalDiscountAmount() + $itemDiscountAmount);
$item->setBaseOriginalDiscountAmount($item->getBaseOriginalDiscountAmount() + $itemBaseDiscountAmount);
// Subtract from the total remaining discount amount
$discountAmount -= $itemDiscountAmount;
$baseDiscountAmount -= $itemBaseDiscountAmount;
}
if ($rule->getApplyToShipping()) {
$shippingAmount = $address->getShippingAmountForDiscount();
$baseShippingAmount = $address->getBaseShippingAmountForDiscount();
if ($shippingAmount === null || $baseShippingAmount === null) {
$shippingAmount = $address->getShippingAmount();
$baseShippingAmount = $address->getBaseShippingAmount();
}
$shippingAmount -= $address->getShippingDiscountAmount();
$baseShippingAmount -= $address->getBaseShippingDiscountAmount();
$shippingDiscountAmount = max(min($discountAmount, $shippingAmount), 0.0);
$shippingBaseDiscountAmount = max(min($baseDiscountAmount, $baseShippingAmount), 0.0);
//.........这里部分代码省略.........
示例8: updateSpecificCoupons
/**
* Update auto generated Specific Coupon if it's rule changed
*
* @param Mage_SalesRule_Model_Rule $rule
* @return Mage_SalesRule_Model_Resource_Coupon
*/
public function updateSpecificCoupons(Mage_SalesRule_Model_Rule $rule)
{
if (!$rule || !$rule->getId() || !$rule->hasDataChanges()) {
return $this;
}
$updateArray = array();
if ($rule->dataHasChangedFor('uses_per_coupon')) {
$updateArray['usage_limit'] = $rule->getUsesPerCoupon();
}
if ($rule->dataHasChangedFor('uses_per_customer')) {
$updateArray['usage_per_customer'] = $rule->getUsesPerCustomer();
}
$ruleNewDate = new Zend_Date($rule->getToDate());
$ruleOldDate = new Zend_Date($rule->getOrigData('to_date'));
if ($ruleNewDate->compare($ruleOldDate)) {
$updateArray['expiration_date'] = $rule->getToDate();
}
if (!empty($updateArray)) {
$this->_getWriteAdapter()->update($this->getTable('salesrule_coupon'), $updateArray, array('rule_id = ?' => $rule->getId()));
}
return $this;
}
示例9: _addDiscountDescription
/**
* Add rule discount description label to address object
*
* @param Mage_Sales_Model_Quote_Address $address
* @param Mage_SalesRule_Model_Rule $rule
* @return Mage_SalesRule_Model_Validator
*/
protected function _addDiscountDescription($address, $rule)
{
$description = $address->getDiscountDescriptionArray();
$ruleLabel = $rule->getStoreLabel($address->getQuote()->getStore());
$label = '';
if ($ruleLabel) {
$label = $ruleLabel;
} else {
if ($address->getCouponCode()) {
$label = $address->getCouponCode();
}
}
if (!empty($label)) {
$description[$rule->getId()] = $label;
}
$address->setDiscountDescriptionArray($description);
return $this;
}
示例10: saveMatchedCustomers
/**
* Try to associate reminder rule with matched customers.
* If customer was added earlier, update is_active column.
*
* @param Bronto_Reminder_Model_Rule $rule
* @param null|Mage_SalesRule_Model_Rule $salesRule
* @param int $websiteId
* @param null $threshold
*
* @return $this
* @throws Exception
*/
public function saveMatchedCustomers(Bronto_Reminder_Model_Rule $rule, $salesRule, $websiteId, $threshold = null)
{
$select = $rule->getConditions()->getConditionsSql($rule, $websiteId);
$interval = Mage::helper('bronto_reminder')->getCronInterval();
if (!$rule->getConditionSql()) {
return $this;
}
if ($threshold) {
$select->where('c.emails_failed IS NULL OR c.emails_failed < ? ', $threshold);
}
// Only pull for reminders not already attached to an active record
$select->where('c.is_active IS NULL OR c.is_active <> 1');
// Handle Send Limit
$sendLimit = $rule->getSendLimit();
if ($sendLimit > 0) {
$subSelect = $this->createSelect()->from(array($this->getTable('bronto_reminder/log')), array('num_send' => 'count(log_id)', 'unique_id'))->group(array('unique_id'));
$select->joinLeft(array('l' => $subSelect), 'c.unique_id=l.unique_id', array())->where('l.num_send IS NULL OR l.num_send < ?', $sendLimit);
}
// Handle Send To Value
switch ($rule->getSendTo()) {
case 'user':
$select->where('`root`.`customer_id` IS NOT NULL AND `root`.`customer_id` != 0');
break;
case 'guest':
$select->where('`root`.`customer_id` IS NULL OR `root`.`customer_id` = 0');
break;
case 'both':
default:
// No need to filter
break;
}
$i = 0;
$ruleId = $rule->getId();
$adapter = $this->_getWriteAdapter();
$currentDate = $this->formatDate(time());
$dataToInsert = array();
Mage::helper('bronto_reminder')->writeDebug('ruleId: ' . $rule->getId() . ' website: ' . $websiteId, 'bronto_reminder_sql.log');
// Log the query with binds replaced
$this->logFullQuery($select, array('rule_id' => $ruleId, 'interval' => $interval));
/* @var $stmt Varien_Db_Statement_Pdo_Mysql */
$stmt = $adapter->query($select, array('rule_id' => $ruleId, 'interval' => $interval));
Mage::helper('bronto_reminder')->writeDebug('saveMatchedCustomers():', 'bronto_reminder_sql.log');
try {
$adapter->beginTransaction();
while ($row = $stmt->fetch()) {
if (empty($row['coupon_id']) && $salesRule) {
if ($salesRule->getCouponType() == Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC && $salesRule->getUseAutoGeneration()) {
$coupons = $salesRule->getCoupons();
if (!$coupons) {
$coupons = array();
}
foreach ($coupons as $couponTemp) {
if ($couponTemp->getUsageLimit() > $couponTemp->getTimesUsed() && (is_null($couponTemp->getExpirationDate()) || $couponTemp->getExpirationDate() > date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d'), date('Y'))))) {
$coupon = $couponTemp;
}
}
} else {
$coupon = $salesRule->acquireCoupon();
}
$couponId = $coupon !== null ? $coupon->getId() : null;
} else {
$couponId = $row['coupon_id'];
}
$dataToInsert[] = array('rule_id' => $ruleId, 'product_recommendation_id' => $rule->getProductRecommendationId(), 'coupon_id' => $couponId, 'unique_id' => $row['unique_id'], 'store_id' => $row['store_id'], 'customer_id' => $row['customer_id'], 'quote_id' => $row['quote_id'], 'wishlist_id' => $row['wishlist_id'], 'customer_email' => $row['customer_email'], 'associated_at' => $currentDate, 'is_active' => '1');
$i++;
if ($i % 1000 == 0) {
$this->_saveMatchedCustomerData($dataToInsert);
$adapter->commit();
$adapter->beginTransaction();
$dataToInsert = array();
}
}
$this->_saveMatchedCustomerData($dataToInsert);
$adapter->commit();
Mage::helper('bronto_reminder')->writeDebug(" Query Matched {$i} customers", 'bronto_reminder_sql.log');
} catch (Exception $e) {
$adapter->rollBack();
throw $e;
}
return $this;
}
示例11: _getItemQty
/**
* Return discount item qty
*
* @param Mage_Sales_Model_Quote_Item_Abstract $item
* @param Mage_SalesRule_Model_Rule $rule
* @return int
*/
protected function _getItemQty($item, $rule)
{
$qty = $item->getTotalQty();
return $rule->getDiscountQty() ? min($qty, $rule->getDiscountQty()) : $qty;
}
示例12: _handleGift
/**
* Make sure that a gift is only added once, create a free item and add it to the cart.
*
* @param Mage_Sales_Model_Quote $quote
* @param Mage_Sales_Model_Quote_Item $item
* @param Mage_SalesRule_Model_Rule $rule
*/
protected static function _handleGift(Mage_Sales_Model_Quote $quote, Mage_Sales_Model_Quote_Item $item, Mage_SalesRule_Model_Rule $rule)
{
if ($rule->getIsApplied()) {
return;
}
$qty = (int) $rule->getDiscountAmount();
if ($qty) {
$freeItem = self::_getFreeQuoteItem($quote, $rule->getGiftSku(), $item->getStoreId(), $qty);
self::_addAndApply($quote, $freeItem, $rule);
}
}
示例13: saveMatchedCustomers
/**
* Try to associate reminder rule with matched customers.
* If customer was added earlier, update is_active column.
*
* @param Enterprise_Reminder_Model_Rule $rule
* @param Mage_SalesRule_Model_Rule $salesRule
* @param int $websiteId
* @param int $threshold
*
* @return Enterprise_Reminder_Model_Resource_Rule
*/
public function saveMatchedCustomers($rule, $salesRule, $websiteId, $threshold = null)
{
$rule->setConditions(null);
$rule->afterLoad();
/** @var $select Zend_Db_Select */
$select = $rule->getConditions()->getConditionsSql(null, $websiteId);
if (!$rule->getConditionSql()) {
return $this;
}
if ($threshold) {
$select->where('c.emails_failed IS NULL OR c.emails_failed < ? ', $threshold);
}
$i = 0;
$ruleId = $rule->getId();
$adapter = $this->_getWriteAdapter();
$couponsTable = $this->getTable('enterprise_reminder/coupon');
$currentDate = $this->formatDate(time());
$dataToInsert = array();
$stmt = $adapter->query($select, array('rule_id' => $ruleId));
$adapter->beginTransaction();
try {
while ($row = $stmt->fetch()) {
if (empty($row['coupon_id']) && $salesRule) {
$coupon = $salesRule->acquireCoupon();
$couponId = $coupon !== null ? $coupon->getId() : null;
} else {
$couponId = $row['coupon_id'];
}
$dataToInsert[] = array('rule_id' => $ruleId, 'coupon_id' => $couponId, 'customer_id' => $row['entity_id'], 'associated_at' => $currentDate, 'is_active' => '1');
$i++;
if ($i % 1000 == 0) {
$adapter->insertOnDuplicate($couponsTable, $dataToInsert, array('is_active'));
$dataToInsert = array();
}
}
if (!empty($dataToInsert)) {
$adapter->insertOnDuplicate($couponsTable, $dataToInsert, array('is_active'));
}
} catch (Exception $e) {
$adapter->rollBack();
throw $e;
}
$adapter->commit();
return $this;
}
示例14: updateRuleProductData
public function updateRuleProductData(Mage_SalesRule_Model_Rule $rule)
{
// foreach ($rule->getActions()->getActions() as $action) {
// break;
// }
$ruleId = $rule->getId();
$read = $this->_getReadAdapter();
$write = $this->_getWriteAdapter();
$write->delete($this->getTable('salesrule/rule_product'), $write->quoteInto('rule_id=?', $ruleId));
if (!$rule->getIsActive()) {
return $this;
}
if ($rule->getUsesPerCoupon() > 0) {
$usedPerCoupon = $read->fetchOne('select count(*) from ' . $this->getTable('salesrule/rule_customer') . ' where rule_id=?', $ruleId);
if ($usedPerCoupon >= $rule->getUsesPerCoupon()) {
return $this;
}
}
$productIds = explode(',', $rule->getProductIds());
$websiteIds = explode(',', $rule->getWebsiteIds());
$customerGroupIds = explode(',', $rule->getCustomerGroupIds());
$fromTime = strtotime($rule->getFromDate());
$toTime = strtotime($rule->getToDate());
if ($toTime) {
$toTime += 86400;
}
$couponCode = $rule->getCouponCode() ? "'" . $rule->getCouponCode() . "'" : 'NULL';
$sortOrder = (int) $rule->getSortOrder();
$rows = array();
$header = 'replace into ' . $this->getTable('salesrule/rule_product') . ' (rule_id, from_time, to_time, website_id, customer_group_id, product_id, coupon_code, sort_order) values ';
try {
$write->beginTransaction();
foreach ($productIds as $productId) {
foreach ($websiteIds as $websiteId) {
foreach ($customerGroupIds as $customerGroupId) {
$rows[] = "('{$ruleId}', '{$fromTime}', '{$toTime}', '{$websiteId}', '{$customerGroupId}', '{$productId}', {$couponCode}, '{$sortOrder}')";
if (sizeof($rows) == 100) {
$sql = $header . join(',', $rows);
$write->query($sql);
$rows = array();
}
}
}
}
if (!empty($rows)) {
$sql = $header . join(',', $rows);
$write->query($sql);
}
$write->commit();
} catch (Exception $e) {
$write->rollback();
throw $e;
}
return $this;
}
示例15: addRuleDescription
/**
* Add the rule label to the address for later usage
*
* @param Mage_Sales_Model_Quote_Address $address
* @param Mage_SalesRule_Model_Rule $rule
*
* @return $this
*/
protected function addRuleDescription(Mage_Sales_Model_Quote_Address $address, Mage_SalesRule_Model_Rule $rule)
{
$descriptions = $address->getDiscountDescriptionArray();
$label = trim($rule->getStoreLabel($address->getQuote()->getStore()));
if (empty($label) && strlen($address->getCouponCode())) {
$label = $address->getCouponCode();
}
if (!empty($label)) {
$descriptions[$rule->getId()] = $label;
}
$address->setDiscountDescriptionArray($descriptions);
return $this;
}