【问题标题】:Loading magento product sku and assigned drop down attribute value via product collection通过产品集合加载 magento 产品 sku 并分配下拉属性值
【发布时间】:2015-12-14 12:43:24
【问题描述】:

我有一个名为warehouse 的产品自定义属性,类型为下拉。

为了通过模型在日期之间加载Sku => Warehouse(为每个产品分配值)更新;我执行以下操作:

// Load warehouse attribute options
$warehouse_options = array();
$options = array();
$attribute = Mage::getSingleton('eav/config')
    ->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'warehouse');
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}
$attribute = null;
foreach ($options as $option) {
    $warehouse_options[$option['value']] = $option['label'];
}
$options = null;

// Load all products updated since last sync
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('updated_at', array(
    'from' => $datetime_from,
    'to' => $datetime_to,
    'date' => true
));

// Init results
$results = array(
    'ServerDateTime' => $datetime_now,
    'Mapping' => array()
);

// Build results
foreach ($collection as $p) {
    $product = Mage::getModel('catalog/product')->load($p->getId());
    $results['Mapping'][] = array(
        'Sku' => $product->getSku(),
        'Warehouse' => isset($warehouse_options[$product->getWarehouse()]) ? $warehouse_options[$product->getWarehouse()] : ''
    );
    $product = null;
}
$collection = null;

这可行,但加载每个产品时速度很慢,所以如果我有3000 产品,那就是3000+ 查询。

有没有办法对此进行优化,以便我能够以最少的查询和处理量加载所需的数据?


我尝试使用 addAttributeToSelect 来使用这样的集合:

$collection->addAttributeToSelect('sku', 'warehouse');

但是,返回的$collection->getData() 不包含字段warehouse。这是一个示例响应数组:

Array
(
    [0] => Array
        (
            [status] => 1
            [entity_id] => 4
            [type_id] => simple
            [attribute_set_id] => 4
            [updated_at] => 2015-07-07 15:35:35
            [sku] => C13S041061
        )

【问题讨论】:

标签: php magento


【解决方案1】:

当您调用$collection->addAttributeToSelect('sku', 'warehouse'); 时,您应该传递一个包含所有所需详细信息的数组,而不是作为单独的函数参数。

(看看Mage_Catalog_Model_Resource_Product_Collection::addAttributeToSelect($attribute, $joinType = false)

所以,你的电话应该是:

$collection->addAttributeToSelect(array('sku', 'warehouse'));

其实你应该可以调用:

$collection->addAttributeToSelect('warehouse');

SKU 应该是您获得的默认值,无论您是否指定它。

这将使您在循环中丢失额外的模型调用(这真的会降低您的速度)。关于从这里开始的最佳方式,我个人最喜欢的是分页集合。看这里:http://www.douglasradburn.co.uk/dealing-with-large-collections-in-magento/

基本上是这样的(示例订单集合):

$ordersCollection = Mage::getModel('sales/order')->getCollection();
$ordersCollection->setPageSize(100);

$pages = $ordersCollection->getLastPageNumber();
$currentPage = 1;

do {
    $ordersCollection->setCurPage($currentPage);
    $ordersCollection->load();

    foreach ($ordersCollection as $_order) {
        // process
    }

    $currentPage++;
    // clear collection (if not done, the same page will be loaded each loop) - will also free memory
    $ordersCollection->clear();
} while ($currentPage < = $pages);

【讨论】:

  • 我试过$collection-&gt;addAttributeToSelect(array('sku', 'warehouse'));它仍然没有返回warehouse
  • 您确定warehouse 是正确的属性名称吗?
  • 如果你使用:$collection-&gt;addAttributeToSelect("*") - 你有仓库吗?
  • 你在使用flat catalog吗?是否针对仓库属性将“在产品列表中使用”属性设置为“是”?
  • 实际上,stackoverflow.com/questions/6271284/… 可能是一个查看flat catalog 的好地方。
猜你喜欢
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 2011-04-22
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多