Question

Comment obtenir un prix spécial inférieur ou non

  <?php 

$categoryIds = array(469);//category id

$todayDate = date('m/d/y');
        $tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
        $tomorrowDate = date('m/d/y', $tomorrow);

$collection = Mage::getModel('catalog/product')
                             ->getCollection()
                             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                             ->addMinimalPrice()->addFinalPrice()
                             ->addAttributeToSelect('*')
                             ->addAttributeToFilter('category_id', array('in' => $categoryIds))
                             ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))

                             ->addFinalPrice()

                   ->getSelect()
                   ->where('price_index.final_price < price_index.price')


                             ->addAttributeToFilter('special_to_date', array('or'=> array(
                                0 => array('date' => true, 'from' => $tomorrowDate))
                                ), 'left')




?>

Je souhaite afficher si le prix spécial est inférieur au prix d'origine, comment l'obtenir ?

Était-ce utile?

La solution

    $categoryIds = array(469);//category id



    $products = Mage::getResourceModel('catalog/product_collection')

->addCategoryFilter(Mage::getModel('catalog/category')->load($categoryIds));
$_productCollection = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array('in' => $products->getAllIds()));;
$_productCollection->addAttributeToSelect(array(
                                   'image',
                                   'name',
                                   'short_description'
                   ))
                   ->addFieldToFilter('visibility', array(
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                   )) 
                   ->addFinalPrice()

                   ->getSelect()
                   ->where('price_index.final_price < price_index.price')


                   print_r($_productCollection->getAllIds());

Autres conseils

Ajouter ->addMinimalPrice()->addFinalPrice() à la collecte pour obtenir le prix.

Une bonne chose à faire est d'utiliser le Mage_Catalog_Model_Layer cours pour préparer vos collections de produits.Augmente les performances (au lieu d'utiliser simplement un caractère générique dans addAttributeToSelect) et prépare entièrement votre collection, de la même manière que cela se produit dans les listes de produits de catégorie générique.

Ainsi, par exemple, vous feriez quelque chose comme ceci :

$products = Mage::getModel('catalog/layer')
    ->setCurrentCategory(469)
    ->getProductCollection();

Pour autant que je sache (je ne l'ai pas testé pour le moment), il devrait automatiquement appliquer le prix spécial et tout ce dont vous auriez besoin pour créer une liste de produits.Le faire vous-même, c'est créer des ennuis.

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top