โซลูชันที่นำเสนอโดย @SanderMangel นั้นยอดเยี่ยมที่สุด ฉันสามารถช่วยขยายโค้ดนี้ได้ด้วยซึ่งตอนนี้ฉันใช้ในโมดูลหมวดหมู่อัตโนมัติ / ไดนามิกหมวดหมู่ - ซึ่งมีความสามารถในการทำหมวดหมู่กฎของผลิตภัณฑ์พิเศษ
รหัสจะปรับชุดผลิตภัณฑ์มาตรฐานเพื่อให้ได้ผลิตภัณฑ์ทั้งหมดพร้อมชุดราคาพิเศษในวันที่มีการเรียกใช้รหัส คุณสามารถใช้สิ่งนี้ใน cron เพื่อเติมข้อมูลหมวดหมู่อีกครั้งในเวลา 00:00 น. และตรวจสอบให้แน่ใจว่าพวกเขาได้รับการปรับปรุงอยู่เสมอ
โปรดทราบว่ารหัสถูกดึงออกมาจากโมดูลขนาดใหญ่ขึ้นดังนั้นฉันจึงทำการกระชับส่วนที่เกี่ยวข้องให้กับคุณ อาจมีตัวแปรหนึ่งหรือสองตัวที่ไม่ได้แสดงในสารสกัด thsi แต่พวกเขาจะง่ายต่อการอนุมานหรือเพียงแค่ถาม :)
ออบเจกต์ $ category เป็นหมวดหมู่จริงที่จะมีผลิตภัณฑ์ รหัสด้านล่างจะช่วยให้คุณสามารถระบุส่วนลดในค่า% เช่นกัน :)
$collection = $category->getProductCollection();
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATE_INTERNAL_FORMAT);
$collection->addAttributeToFilter(array(
    array(
        'attribute' => "special_to_date",
        'null' => true
    ),
    array(
        'attribute' => "special_to_date",
        'from' => $todayDate,
        //'to'      => $todayDate,
        'date' => true
    )
));
$collection->addAttributeToFilter(array(
    array(
        'attribute' => "special_from_date",
        'null' => true
    ),
    array(
        'attribute' => "special_from_date",
        //'from'    => $todayDate,
        'to' => $todayDate,
        'date' => true
    )
));
$collection->addAttributeToSelect('special_price','left');
$collection->addAttributeToSelect('price','left');
$select = $collection->getSelect();
if (strpos($value, '%') > 0) {
    $value = str_replace('%', '', $value);
    $select->where('( 100 - (( at_special_price.value * 100 ) / at_price.value ) )  ' . $operator . ' ' . $value);
} else {
    $select->where('((at_price.value - at_special_price.value)) ' . $operator . ' ' . $value);
}
ตอนนี้สิ่งที่ควรทราบก็คือคอลเลกชันจะไม่ส่งคืนผลิตภัณฑ์เนื่องจากมีลิงก์ไปยังตารางปกติของแคตตาล็อก <-> เนื่องจากคุณไม่ได้สนใจผลิตภัณฑ์ที่เชื่อมโยงอยู่ในปัจจุบันคุณจำเป็นต้องล้างความสัมพันธ์ของตารางนั้นออกจากการรวบรวม
ฉันใช้รหัสต่อไปนี้เพื่อให้เสร็จ:
/**
 * Remove Catalog Product Link elements from collection
 * 
 * @param type $collection
 * @return type
 */
public function removeCatProPart($collection)
{
    $select = $collection->getSelect();
    $fromPart = $select->getPart(Zend_Db_Select::FROM);
    $select->reset(Zend_Db_Select::FROM);
    if (array_key_exists('cat_pro', $fromPart)) {
        unset($fromPart['cat_pro']);
        // also remove any reference to the table in the rest of the query
        $columns = $select->getPart(Zend_Db_Select::COLUMNS);
        $columnRemoved = false;
        foreach ($columns as $columnKey => $column) {
            if ($column[0] == 'cat_pro') {
                unset($columns[$columnKey]);
                $columnRemoved = true;
            }
        }
        if ($columnRemoved) {
            $select->setPart(Zend_Db_Select::COLUMNS, $columns);
        }
        $orderPart = $select->getPart(Zend_Db_Select::ORDER);
        $orderRemoved = false;
        foreach ($orderPart as $orderKey => $order) {
            if ($order[0] == 'cat_pro') {
                unset($orderPart[$orderKey]);
                $orderRemoved = true;
            }
        }
        if ($orderRemoved) {
            $select->setPart(Zend_Db_Select::ORDER, $orderPart);
        }
    }
    $select->setPart(Zend_Db_Select::FROM, $fromPart);
    return $collection;
}
เป็นโบนัสที่เพิ่มเข้ามาคุณสามารถใช้ teqnique เดียวกันในการปรับการรวบรวมผลิตภัณฑ์แคตตาล็อกและค้นหาผลิตภัณฑ์ที่อยู่ในโหมดพิเศษเนื่องจากกฎแคตตาล็อก:
$storeDate = Mage::app()->getLocale()->storeTimeStamp($this->getStoreId());
$value = $this->getValue();
$conditions = 'price_rule.product_id = e.entity_id AND ';
$conditions .= "(from_time = 0
    OR from_time <= " . $storeDate . ")
    AND (to_time = 0
    OR to_time >= " . $storeDate . ") AND ";
$conditions .= "price_rule.rule_id IN (" . $value . ")";
$collection->getSelect()->joinInner(
        array('price_rule' => $collection->getTable('catalogrule/rule_product')), $conditions);
$collection->setFlag('applied_catalog_rule_id', true);
$collection->setFlag('applied_rule', true);
เมื่อคุณมีคอลเลกชันที่ใช้งานได้สิ่งที่คุณต้องทำคือรับรหัสทั้งหมดจากคอลเลกชันพลิกอาร์เรย์และใช้$category->setPostedProducts($products);และ $ category-> save () l; เพื่อให้การอัปเดตเสร็จสมบูรณ์
เพื่อความสมบูรณ์นี่คือ cron รายวันของฉันที่ทำให้หมวดหมู่ที่เป็นปัจจุบันอยู่เสมอ (อีกครั้งมันหมายถึงวิธีการที่ไม่รวมอยู่ที่นี่ แต่ฉันแน่ใจว่ามันจะพาคุณไปในทิศทางที่ถูกต้อง
มีความสุข :)
public static function rebuildAllDynamic($schedule)
{
    try {
        $tempDir = sys_get_temp_dir() . "/";
        $fp = fopen($tempDir . "dyncatprod_rebuild.lock", "w+");
        if (flock($fp, LOCK_EX | LOCK_NB)) {
            if (Mage::getStoreConfig('dyncatprod/debug/enabled')) {
                   mage::log("DynCatProd - rebuildAllDynamic");
            }
            if (!Mage::getStoreConfig('dyncatprod/rebuild/max_exec')) {
                ini_set('max_execution_time', 3600); // 1 hour
            }
            $categories = Mage::getModel('catalog/category')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addIsActiveFilter()
                ->addAttributeToFilter('dynamic_attributes', array('notnull' => true));
            foreach ($categories as $category) {
                $products = Mage::helper('dyncatprod')->getDynamicProductIds($category);
                if (is_array($products)) {
                    if (Mage::getStoreConfig('dyncatprod/debug/enabled')) {
                        mage::log("rebuilding :" . $category->getName() . ' ' . $category->getPath() );
                    }
                    $products = array_flip($products);
                    $category->setPostedProducts($products);
                    $category->setIsDynamic(true);
                    $category->save();
                }
            }
            flock($fp, LOCK_UN); 
            unlink($tempDir . "dyncatprod_rebuild.lock");
        } else {
            mage::log('Could not execute cron for rebuildAllDynamic -file lock is in place, job may be running');
        }
    } catch (Exception $e) {
        flock($fp, LOCK_UN); 
        unlink($tempDir . "dyncatprod_rebuild.lock");
        mage::logException($e);
        return $e->getMessage();
    }
}
อ้างอิง: http://www.proxiblue.com.au/magento-dynamic-category-products.html