We're a very small store and have to sell some of our products online. Magento is our system of choice. Right now we're still using Magento 1. For our shop we have the possibility to list all products (about 650) and change the price, the stock qty and one custom attribute (the year of manufacturing) from an external list. The list looks like this

[Manufacturer] [Product Name] [Year of manufacturing] [Price] [Stock QTY] [-> Save]

and is looped through the whole list of products. I think the (or one of the) performance problem(s) is that there is a loop inside the loop - [Year of manufacturing] is a dropdown-attribute and every available option is looped through for every single product.

Is somebody using the same or a similar approach and has some advice regarding the performance?

EDIT: Added... "Code" by request

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');

$productCollection = $obj->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productCollection->create()
            ->joinField('qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left')
            ->setOrder('manufacturer', 'asc')

foreach($collection as $product) {
                                    echo $product->getAttributeText('manufacturer') . ' ' . $product->getName() . ' ' . $product->getAttributeText('jahrgang');
                                    $productRepository = $obj->get('\Magento\Catalog\Model\Product\Attribute\Repository');
                                    $jahrgaenge = $productRepository->get('jahrgang')->getOptions();
                                    $jahrgang_optionen = '';
  foreach ($jahrgaenge as $jahrgaengeOption) {
                                      $jahrgang_id = $jahrgaengeOption->getValue(); //id of the option
                                      $jahrgang_value = $jahrgaengeOption->getLabel(); //Label of the option 
                                      $jahrgang_optionen .= '<option value="' . $jahrgang_id . '">' . $jahrgang_value . '</option>';
                                    echo '<select>' . $jahrgang_optionen . '</select><br>';


1 Answer 1


If the Year of manufacturing is an attribute why don't you try to get the value like this:


instead of looping all the options.

  • Thats because each value and id is needed to change the [Year of manufacturing] - it‘s a dropdown-list for each item/ product...
    – Daniel
    Aug 21, 2019 at 11:19
  • can you post an example?
    – Tsita
    Aug 21, 2019 at 12:04
  • Sure, but be warned... I've updated the question
    – Daniel
    Aug 22, 2019 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.