【发布时间】:2013-11-06 10:16:50
【问题描述】:
我正在尝试使用 cron 作业将所有具有“特价”的产品添加到特殊类别中。我已经成功运行了 cron.php,但是我似乎无法让 cron 工作,任何人都可以看到我哪里出错了吗?
到目前为止我所拥有的: app/code/local/Namespace/Modulename/ect/config.xml:
<crontab>
<jobs>
<namespace_productassign>
<schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
<run><model>modulename/productassign::assignproduct</model></run>
</namespace_productassign>
</jobs>
</crontab>
app/code/local/Namespace/Modulename/Model/Productassign.php:
<?php
class Namespace_Modulename_Model_Productassign {
public function assignproduct () {
try {
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$productIds = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('visibility', array('neq'=>1))
->addAttributeToFilter('status', array('neq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED))
->addAttributeToFilter('price', array('neq' => ""))
->addAttributeToFilter('special_price', array('neq' => ""))
->addAttributeToFilter('special_price', array('lt' =>new Zend_Db_Expr('at_price.value')))
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(0 => array('date' => true, 'from' =>$tomorrowDate), 1 => array('is' => new Zend_Db_Expr('null')))), 'left')
->getAllIds();
$newCategories = 68; // Add here your SALE category id
$category = Mage::getModel('catalog/category')->load($newCategories);
$saleIds = Mage::getModel('catalog/product')->getCollection()
->addCategoryFilter($category)
->getAllIds();
$product = (array_values(array_diff($productIds,$saleIds)));
foreach ($product as $id) {
$sql = "INSERT INTO catalog_category_product (category_id ,product_id) VALUES ('".$newCategories."', '".$id."')";
$statement = $write->query($sql);
}
// Working copy of unassign product from Sale Category whose special to date is ended.
$current_date = date('Y-m-d H:i:s');
$category = Mage::getModel('catalog/category')->load($newCategories);
$saleIds = Mage::getModel('catalog/product')->getCollection()
->addCategoryFilter($category)
->addAttributeToFilter('special_to_date', array('date'=>true, 'lt'=> $current_date))
->getAllIds();
foreach ($saleIds as $id) {
$sql = "Delete from catalog_category_product where category_id='".$newCategories."' and product_id='".$id."'";
$statement = $write->query($sql);
}
/* reindexing
3. Catalog URL Rewrites
6. Category Products
*/
$process = Mage::getModel('index/process')->load(3);
$process->reindexAll();
$process = Mage::getModel('index/process')->load(6);
$process->reindexAll();
}
catch (Exception $e) {
Mage::log($e);
}
}
}
?>
任何帮助都会很棒,谢谢。我需要做任何其他事情才能使这个 cron 工作吗?
【问题讨论】:
-
你的问题解决了吗?
-
目前还没有找到 cron 没有运行的原因。 @monojit
-
我之前使用过 magento cron。确保您的模块工作正常。您可以使用 Mage::log("works");在assignproduct函数中检查你的函数是否正在调用。我在模块中使用了observer.php,但我认为这不是问题,我关注了magentocommerce.com/wiki/1_-_installation_and_configuration/…链接
-
还要确保你的 cron.php 工作正常。你可以从你的页面中删除所有的功能,只使用 Mage::log("works");用于检查和清除缓存。
-
如果你看到你的模块工作正常但 cron 不工作,然后在模型(类 Namespace_Modulename_Model_Observer)中创建observer.php 并检查它是否工作。
标签: magento cron magento-1.7