Pergunta

We get more product related information in the $_product->getData() in the product detail page, but not in category page.
Currently the only data available in the category page are,

Array
    (
        [entity_id] => 2442
        [attribute_set_id] => 19
        [type_id] => simple
        [sku] => wheat-tortilla-spinach
        [has_options] => 0
        [required_options] => 0
        [created_at] => 2019-09-19 04:55:54
        [updated_at] => 2019-09-19 04:55:54
        [price] => 140.0000
        [tax_class_id] => 2
        [final_price] => 140.0000
        [minimal_price] => 140.0000
        [min_price] => 140.0000
        [max_price] => 140.0000
        [tier_price] => Array
            (
            )

        [cat_index_position] => 10000
        [event_id] => 
        [logged_at] => 
        [event_type_id] => 
        [object_id] => 
        [subject_id] => 
        [subtype] => 
        [store_id] => 
        [num_view_counts] => 
        [num_reviews_count] => 
        [num_rating_summary] => 
        [is_salable] => 1
        [name] => Wheat Tortillas with Spinach
        [image] => /w/h/wheat_tortilla_spinach_2.jpg
        [small_image] => /w/h/wheat_tortilla_spinach_2.jpg
        [thumbnail] => /w/h/wheat_tortilla_spinach_2.jpg
        [msrp_display_actual_price_type] => 0
        [url_key] => wheat-tortillas-with-spinach
        [swatch_image] => /w/h/wheat_tortilla_spinach_2.jpg
        [status] => 1
        [request_path] => wheat-tortillas-with-spinach.html
        [rating_summary] => Magento\Framework\DataObject Object
            (
                [_data:protected] => Array
                    (
                        [store_id] => 24
                    )

            )

        [tier_price_changed] => 0
    )  

Here I would like to add few more product attribute values such as `weight`,`custom_attribute`, and so they can be viewed in the `catalog_category_view.xml` ? 

What I do now to fetch the additional data is like this,

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_product = $objectManager->create('Magento\Catalog\Model\Product')->load( $block->getProduct()->getId() );
    $weight = $_product->getData('weight');
    $custom_attribute = $_product->getData('custom_attribute');
But wouldn't this add more load to the products listing page?  
Someone please let me know if there is a better way. 



Foi útil?

Solução

To get additional attributes on category pages, Attribute must be set to "Yes" on the option "Used in Product Listing", Check below screenshot

enter image description here

And then you can use below code on the category page to get that attribute value like,

<?php echo $_product->getWeight(); ?>

Hope it will help you.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top