0

我正在尝试使用 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 工作吗?

4

1 回答 1

0

抱歉不知道怎么回事,但我总是使用这个分机来帮助调试和运行 cron 作业
http://www.magentocommerce.com/magento-connect/aoe-scheduler.html

于 2013-11-06T12:45:44.857 回答