Magento以最低价获得产品

Magento以最低价获得产品,magento,Magento,我需要得到一个集合的产品下一个特定的产品在某些类别的价格。 例如:我有产品白色的鞋子,它位于鞋类。我需要得到接下来的五种产品,它们的价格比鞋类中的白色鞋子要高,还有五种产品的价格更低 谢谢你的帮助 从效率的角度来看,这可能是可以解决的,但希望你能理解其中的要点。您需要设置两个变量,$product(可能是作为Mage_Catalog_Model_产品对象的白色鞋子产品)和$category(作为Mage_Catalog_Model_类别对象的鞋子类别) 更新 这是一种更好的方法,无需加载$cat

我需要得到一个集合的产品下一个特定的产品在某些类别的价格。 例如:我有产品白色的鞋子,它位于鞋类。我需要得到接下来的五种产品,它们的价格比鞋类中的白色鞋子要高,还有五种产品的价格更低


谢谢你的帮助

从效率的角度来看,这可能是可以解决的,但希望你能理解其中的要点。您需要设置两个变量,$product(可能是作为Mage_Catalog_Model_产品对象的白色鞋子产品)和$category(作为Mage_Catalog_Model_类别对象的鞋子类别)

更新

这是一种更好的方法,无需加载$category的整个产品集合


从效率的角度来看,这可能是可以解决的,但希望您能理解其中的要点。您需要设置两个变量,$product(可能是作为Mage_Catalog_Model_产品对象的白色鞋子产品)和$category(作为Mage_Catalog_Model_类别对象的鞋子类别)

更新

这是一种更好的方法,无需加载$category的整个产品集合


我也有类似的任务,这里我做了:

        $customerGroupId = Mage::helper('customer')->getCustomer()->getGroupId();
        $websiteId = Mage::app()->getStore()->getWebsiteId();
        $finalPrice = $product->getFinalPrice();

        $coreResource = Mage::getSingleton('core/resource');
        $adapter = $coreResource->getConnection('catalog_read');
        $prevSelect = $adapter->select()
            ->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array())
            ->join(
                array('p' => $coreResource->getTableName('catalog/category_product')),
                'p.product_id = i.entity_id',
                array('product_id')
            )
            ->where('p.category_id = ?', $categoryId)
            ->where('i.customer_group_id = ?', $customerGroupId)
            ->where('i.website_id = ?', $websiteId)
            ->where('p.product_id != ?', $product->getId())
            ->where('i.final_price < ?', $finalPrice)
            ->order('i.final_price DESC')
            ->limit(self::PRODUCTS_BLOCK_SIZE);

        $lowerIds = array_reverse($adapter->fetchCol($prevSelect));

        $nextSelect = $adapter->select()
            ->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array())
            ->join(
                array('p' => $coreResource->getTableName('catalog/category_product')),
                'p.product_id = i.entity_id',
                array('product_id')
            )
            ->where('p.category_id = ?', $categoryId)
            ->where('i.customer_group_id = ?', $customerGroupId)
            ->where('i.website_id = ?', $websiteId)
            ->where('p.product_id != ?', $product->getId())
            ->where('i.final_price > ?', $finalPrice)
            ->order('i.final_price ASC')
            ->limit(self::PRODUCTS_BLOCK_SIZE);

        $higherIds = $adapter->fetchCol($nextSelect);

        $lowerSliced = array_slice($lowerIds, -self::PRODUCTS_BLOCK_SIZE);
        $requiredFromHigher = self::PRODUCTS_BLOCK_SIZE - count($lowerSliced);
        $similarIds = array_merge(
            $lowerSliced,
            array_slice($higherIds, 0, $requiredFromHigher)
        );

        $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('small_image')
            ->addAttributeToSelect('product_url')
            ->addAttributeToFilter('entity_id', array('in' => $similarIds))
            ->setPage(1, self::PRODUCTS_BLOCK_SIZE);

我也有类似的任务,这里我做了:

        $customerGroupId = Mage::helper('customer')->getCustomer()->getGroupId();
        $websiteId = Mage::app()->getStore()->getWebsiteId();
        $finalPrice = $product->getFinalPrice();

        $coreResource = Mage::getSingleton('core/resource');
        $adapter = $coreResource->getConnection('catalog_read');
        $prevSelect = $adapter->select()
            ->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array())
            ->join(
                array('p' => $coreResource->getTableName('catalog/category_product')),
                'p.product_id = i.entity_id',
                array('product_id')
            )
            ->where('p.category_id = ?', $categoryId)
            ->where('i.customer_group_id = ?', $customerGroupId)
            ->where('i.website_id = ?', $websiteId)
            ->where('p.product_id != ?', $product->getId())
            ->where('i.final_price < ?', $finalPrice)
            ->order('i.final_price DESC')
            ->limit(self::PRODUCTS_BLOCK_SIZE);

        $lowerIds = array_reverse($adapter->fetchCol($prevSelect));

        $nextSelect = $adapter->select()
            ->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array())
            ->join(
                array('p' => $coreResource->getTableName('catalog/category_product')),
                'p.product_id = i.entity_id',
                array('product_id')
            )
            ->where('p.category_id = ?', $categoryId)
            ->where('i.customer_group_id = ?', $customerGroupId)
            ->where('i.website_id = ?', $websiteId)
            ->where('p.product_id != ?', $product->getId())
            ->where('i.final_price > ?', $finalPrice)
            ->order('i.final_price ASC')
            ->limit(self::PRODUCTS_BLOCK_SIZE);

        $higherIds = $adapter->fetchCol($nextSelect);

        $lowerSliced = array_slice($lowerIds, -self::PRODUCTS_BLOCK_SIZE);
        $requiredFromHigher = self::PRODUCTS_BLOCK_SIZE - count($lowerSliced);
        $similarIds = array_merge(
            $lowerSliced,
            array_slice($higherIds, 0, $requiredFromHigher)
        );

        $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('small_image')
            ->addAttributeToSelect('product_url')
            ->addAttributeToFilter('entity_id', array('in' => $similarIds))
            ->setPage(1, self::PRODUCTS_BLOCK_SIZE);

感谢您的回答,但这里会有性能问题:您的代码将加载特定类别中的所有产品,但只需要10种产品。@Snowcore我已更新了我的答案,将选择的产品限制为5种以上,5种以下或相等。谢谢您的回答,但在这里我们会遇到性能问题:您的代码将加载特定类别中的所有产品,但只需要10种产品。@Snowcore我已更新了我的答案,将SELECT限制为5种产品更高,5种产品更低或相等。有什么比我的解决方案更好?不是小气,只是想学习!最后一种解决方案更好,因为存在以下情况:例如,如果没有5种产品,价格低于5,无论如何,我将得到一包self::products\u BLOCK\u SIZEproducts@Snowcore-我不太明白。若你们真正想要的是围绕产品X的5个价格最低的产品块。。。这还不清楚。是什么让这比我的解决方案更好?不是小气,只是想学习!最后一种解决方案更好,因为存在以下情况:例如,如果没有5种产品,价格低于5,无论如何,我将得到一包self::products\u BLOCK\u SIZEproducts@Snowcore-我不太明白。若你们真正想要的是围绕产品X的5个价格最低的产品块。。。这一点没有说清楚。