I need to check if a product has a catalog price rule and if it has, it needs to display a specific div.
So I need something like:
<?php if catalog price rule : ?>
<div>
Text
</div>
<?php endif; ?>
How can I achieve that?
catalogcatalog-price-rulescatalog-rules
I need to check if a product has a catalog price rule and if it has, it needs to display a specific div.
So I need something like:
<?php if catalog price rule : ?>
<div>
Text
</div>
<?php endif; ?>
How can I achieve that?
I have not tried this, but I believe it's possible. I don't have the exact code, but I can provide you with it later when I have more free time if you really need it. This appraoch is pretty straightforward.
There's a table called catalogrule_product
that stores Catalog Price Rules
discounts per product and customer group. Note that this table needs to get indexed every time you make changes to your Catalog Price Rules
. You can do this with the indexer or by clicking "Apply Rules" on this page (this could take long if you have a lot of products).
For a given product you have on a catalog (or any) page, get its entity_id
and look it up on catalogrule_product.product_id
. Make sure to check customer_group_id
as well. Once you find the row(s) you're looking for, this row(s) will have have timestamps (in seconds) from_time
and to_time
for that given applied rule (rule_id
).
Get its row_id
and look this up on catalogrule
if you need more information on the rule. The entry you find on catalogrule
has from_date
and to_date
, and those are all you need to calculate what you want.
Here is i have found solution after deep analysis.
you can override model
app\code\core\Mage\CatalogRule\Model\Action\Index\Refresh.php
<global>
<models>
<catalogrule>
<rewrite> <action_index_refresh>Yournamespace_Promorule_Model_CatalogRule_Action_Index_Refresh</action_index_refresh></rewrite>
</catalogrule>
</models>
</global>
class Yournamespace_Promorule_Model_CatalogRule_Action_Index_Refresh extends Mage_CatalogRule_Model_Action_Index_Refresh {
/**
* Prepare temporary data
*
* @param Mage_Core_Model_Website $website
* @return Varien_Db_Select
*/
protected function _prepareTemporarySelect(Mage_Core_Model_Website $website) {
/** @var $catalogFlatHelper Mage_Catalog_Helper_Product_Flat */
$catalogFlatHelper = $this->_factory->getHelper('catalog/product_flat');
/** @var $eavConfig Mage_Eav_Model_Config */
$eavConfig = $this->_factory->getSingleton('eav/config');
$priceAttribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'price');
$select = $this->_connection->select()
->from(
array('rp' => $this->_resource->getTable('catalogrule/rule_product')), array()
)
->joinInner(
array('r' => $this->_resource->getTable('catalogrule/rule')), 'r.rule_id = rp.rule_id', array()
)
->where('rp.website_id = ?', $website->getId())
->order(
array('rp.product_id', 'rp.customer_group_id', 'rp.sort_order', 'rp.rule_product_id')
)
->joinLeft(
array(
'pg' => $this->_resource->getTable('catalog/product_attribute_group_price')
), 'pg.entity_id = rp.product_id AND pg.customer_group_id = rp.customer_group_id'
. ' AND pg.website_id = rp.website_id', array()
)
->joinLeft(
array(
'pgd' => $this->_resource->getTable('catalog/product_attribute_group_price')
), 'pgd.entity_id = rp.product_id AND pgd.customer_group_id = rp.customer_group_id'
. ' AND pgd.website_id = 0', array()
);
$storeId = $website->getDefaultStore()->getId();
if ($catalogFlatHelper->isEnabled() && $storeId && $catalogFlatHelper->isBuilt($storeId)) {
$select->joinInner(
array('p' => $this->_resource->getTable('catalog/product_flat') . '_' . $storeId), 'p.entity_id = rp.product_id', array()
);
$priceColumn = $this->_connection->getIfNullSql(
$this->_connection->getIfNullSql(
'pg.value', 'pgd.value'
), 'p.price'
);
} else {
$select->joinInner(
array(
'pd' => $this->_resource->getTable(array('catalog/product', $priceAttribute->getBackendType()))
), 'pd.entity_id = rp.product_id AND pd.store_id = 0 AND pd.attribute_id = '
. $priceAttribute->getId(), array()
)
->joinLeft(
array(
'p' => $this->_resource->getTable(array('catalog/product', $priceAttribute->getBackendType()))
), 'p.entity_id = rp.product_id AND p.store_id = ' . $storeId
. ' AND p.attribute_id = pd.attribute_id', array()
);
$specialPriceAttribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'special_price');
$specialFormDateAttribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'special_from_date');
$specialToDateAttribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'special_to_date');
$todayStartOfDayDate = Mage::app()->getLocale()->date()
->setTime('00:00:00')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayEndOfDayDate = Mage::app()->getLocale()->date()
->setTime('23:59:59')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$select->joinLeft(
array(
'spd' => $this->_resource->getTable(array('catalog/product', $specialPriceAttribute->getBackendType()))
), 'spd.entity_id = rp.product_id AND spd.store_id = 0 AND spd.attribute_id = '
. $specialPriceAttribute->getId(), array()
)
->joinLeft(
array(
'sp' => $this->_resource->getTable(array('catalog/product', $specialPriceAttribute->getBackendType()))
), 'sp.entity_id = rp.product_id AND sp.store_id = ' . $storeId
. ' AND sp.attribute_id = spd.attribute_id', array()
);
$select->joinLeft(
array(
'sdfd' => $this->_resource->getTable(array('catalog/product', $specialFormDateAttribute->getBackendType()))
), 'sdfd.entity_id = rp.product_id AND sdfd.store_id = 0 AND sdfd.attribute_id = '
. $specialFormDateAttribute->getId(), array()
)
->joinLeft(
array(
'sdf' => $this->_resource->getTable(array('catalog/product', $specialFormDateAttribute->getBackendType()))
), 'sdf.entity_id = rp.product_id AND sdf.store_id = ' . $storeId
. ' AND sdf.attribute_id = sdfd.attribute_id', array()
);
$select->joinLeft(
array(
'sdtd' => $this->_resource->getTable(array('catalog/product', $specialToDateAttribute->getBackendType()))
), 'sdtd.entity_id = rp.product_id AND sdtd.store_id = 0 AND sdtd.attribute_id = '
. $specialToDateAttribute->getId(), array()
)
->joinLeft(
array(
'sdt' => $this->_resource->getTable(array('catalog/product', $specialToDateAttribute->getBackendType()))
), 'sdt.entity_id = rp.product_id AND sdt.store_id = ' . $storeId
. ' AND sdt.attribute_id = sdtd.attribute_id', array()
);
$priceColumn = $this->_connection->getCheckSql(
'(' . $this->_connection->getIfNullSql(
'sp.value', 'spd.value'
) . ' IS NOT NULL AND ((' .
$this->_connection->getIfNullSql(
'sdf.value', 'sdfd.value'
) . ' IS NULL) OR (' .
$this->_connection->getIfNullSql(
'sdf.value', 'sdfd.value'
) . ' <= "' . $todayEndOfDayDate . '")'
. ') AND ((' .
$this->_connection->getIfNullSql(
'sdt.value', 'sdtd.value'
) . ' IS NULL) OR (' .
$this->_connection->getIfNullSql(
'sdt.value', 'sdtd.value'
) . ' >= "' . $todayStartOfDayDate . '")'
. '))', $this->_connection->getIfNullSql(
'sp.value', 'spd.value'
), $this->_connection->getIfNullSql(
$this->_connection->getIfNullSql(
'pg.value', 'pgd.value'
), $this->_connection->getIfNullSql(
'p.value', 'pd.value'
)
)
);
}
$select->columns(
array(
'grouped_id' => $this->_connection->getConcatSql(
array('rp.product_id', 'rp.customer_group_id'), '-'
),
'product_id' => 'rp.product_id',
'customer_group_id' => 'rp.customer_group_id',
'from_date' => 'r.from_date',
'to_date' => 'r.to_date',
'action_amount' => 'rp.action_amount',
'action_operator' => 'rp.action_operator',
'action_stop' => 'rp.action_stop',
'sort_order' => 'rp.sort_order',
'price' => $priceColumn,
'rule_product_id' => 'rp.rule_product_id',
'from_time' => 'rp.from_time',
'to_time' => 'rp.to_time'
)
);
return $select;
}
}
hope this will sure help to our community.
Let me know if any inputs from your end.
Best Answer
If you don't have many rules or many products you can try this:
The quick and dirty idea would be to search for a record in the table
catalogrule_product
.All products that are affected by catalog rules are referenced in that table.
But this is not the best practice.