3

I had to create a product collection that contains some newly created attributes. It was not possible to add these attributes to the collection ('inner' doesn't make a difference). Even reindexing didn't help.

Example code:

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

umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$attributes = array(
    'name',
    'new_attribute'
);
$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect($attributes, 'inner');

Query for that collection:

SELECT `e`.*, `at_name`.`value` AS `name`, `at_new_attribute`.`value` AS `new_attribute`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_varchar` AS `at_name`
    ON (`at_name`.`entity_id` = `e`.`entity_id`) AND (`at_name`.`attribute_id` = '71') AND (`at_name`.`store_id` = 0)
INNER JOIN `catalog_product_entity_varchar` AS `at_new_attribute`
    ON (`at_new_attribute`.`entity_id` = `e`.`entity_id`) AND (`at_new_attribute`.`attribute_id` = '514') AND (`at_new_attribute`.`store_id` = 0)

If i check for that attribute_id 514 in catalog_product_entity_varchar I get also an empty result. So neither null nor the "default value" is set.

SELECT *
FROM `catalog_product_entity_varchar`
WHERE attribute_id = 514

MySQL returned an empty result set

After using workaround from below I get 750 correct results (with value null).

Even when you set a "default value" for this newly created attribute, it is not populated to products when you add it to attribute set. The "default value" is only used if you create new products.

Steps to reproduce:

  1. add new attribute (use in product listing doesn't matter)
  2. assign attribute to attribute set
  3. reindex attributes/flat tables
  4. run code

Expected result:

Get a collection where $item->getNewAttribute() is null or empty string.

Current result:

Get an empty collection. ($products->getSize() is 0)


Workaround:

Fortunately i found an answer here: When setting a product attribute programmatically, can't retrieve it from a collection

  1. open product grid
  2. select all
  3. update attributes
  4. submit
  5. check "change" checkbox for "attributecode" attribute but leave input field empty
  6. save

For my understanding attribute values (null or "default value") should be added to DB when assing a attribute to a set, but it doesn't happen. Products only appear in product collection, if:

  • product is saved manually, or
  • workaround is used (for multible products)

Any ideas where to fix this in code?

  • do reindexing and try again – Manashvi Birla Jun 9 '17 at 10:50
  • As said reindexing didnt help. – sv3n Jun 9 '17 at 10:53
1

You can't get theme in your collection because, Only the attributes that are marked be used in product listing to YES are copyed to the flat tables. Mark it like this in Catalog > Attributes > Manage Attributes > {select your attribute}, reindex everything and now you have your attributes.

EDIT:

It should work by doing this config! anyway if you don't set used in product listing to yes, You will never get the attributes in the collection, i explained you because for doing this config, Magento copyed them to the flat tables. and don't forget to reindex all !

Maybe you have some issue in your collection, try this one:

$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect(array('name', 'price', 'yourattribute'));
  • Thanks, but it doesn't make a difference. – sv3n Jul 2 '17 at 7:54
  • The attributes, you created them in the backoffice? – PЯINCƏ Jul 2 '17 at 9:39
  • Yes, created from backend. – sv3n Jul 2 '17 at 9:45
  • So it should work ! anyway if you don't set used in product listing to yes, You will never get the attributes in the collection, i explained you because for doing this config, Magento copyed them to the flat tables. – PЯINCƏ Jul 2 '17 at 10:03
  • Please, look my update – PЯINCƏ Jul 2 '17 at 10:13
0

can't use

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

because in 'app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php' function addAttributeToSelect called isEnabledFlat

    public function isEnabledFlat()
{
    // Flat Data can be used only on frontend
    if (Mage::app()->getStore()->isAdmin()) {
        return false;
    }
-2

you assign that manage attribute set if not assign so assign it.

  • Assign attribute to attribute set? Done before. This behavoir is confirmed in the linked question. – sv3n Jul 1 '17 at 21:49
  • Downvote. I just can run "Update attributes" on assigned attributes (step 3). – sv3n Jul 2 '17 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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