简体   繁体   中英

Load product information by comparing custom attribute in Magento

I am working on some scripts to automate some things inside our webshop.

I have looked through many forums and questions.
Now I almost have finished my script but there is a small thing that doesn't work but I can not think of what I am doing wrong.

What the goal of this script is, is to get products that has the same attribute value as the values in an array (pulled from DB).

So here is my code:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once('../app/Mage.php');
require_once('db.php');


Mage::app();

$db = db_connection();

$collection = Mage::getModel('catalog/product')->getCollection();

$collection->addAttributeToSelect('ean');  


$getean = $db->prepare('SELECT ean_l FROM mytable');
$getean->execute();
$allean = $getean->fetchAll();


foreach($allean as $ean) {

    $collection->addFieldToFilter(array(
        array('attribute'=>'ean','eq'=>'' . $ean['ean_l'] . ''),     
    ));
    echo 'ean_l: ' . $ean['ean_l'] . '<br>';

    foreach ($collection as $product) {

        echo $product['entity_id'];

    }


}

So here's how it works:

We select an attribute (ean).
We get a list of all ean numbers from the database.
We loop through the list and compare any product with the ean number.
Then we loop through the collection and get the id of the corresponding product.
Yet, all $product['entity_id'] 's are 273. It is correct that the entity_id is 273, but there is also product 274 with a corresponding ean number.

Here is the result from the script (it's alot more):

结果

So why is this? Because in my reasoning, it changes the ean_l every loop and it equalizes it with the attribute values.
And then it should change the collection, right?
So shouldn't it at least show 274 at some point?

This question is not especially for Magento programmers, but other programmers can help too, so I figured to post it on SO.

Magento comes with powerfull filtering and queries into collections. If that doesn't satisfy, you can always extend with custom queries into getSelect function. Some info here .

Using addFieldToFilter into that foreach will filter the remaining values after another iterated filtering. So it's not good.

$allean = array("of", "ean", "values", "needed", "for", "filtering");

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('ean');
//addAttributeToFilter for EAV collections
$collection->addAttributeToFilter('ean', array('in' => $allean)); //not addFieldToFilter
$collection->getColumnValues('entity_id');

var_dump($collection); //will output an array of product_ids

Alternative, if you want to group by ean values you should remove getColumnValues and run group command. You can find additional info here .

Or you can just remove getColumnValues , start a foreach($collection as $product) and group manually or do what you want with those filtered products.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM