Data Accessing Methods from Within Various Scopes

Last modified by resonance on Thu, June 24, 2010 16:32
Source|Old Revisions  

This is an old revision of the document!


Introduction

This ongoing article is meant to be a reference for basic data-fetching tasks in the Mage environment as well as a learn-by-example tutorial. Please add to it as you can...

Accessing a Product in the Cart

Current Scope:

An extension of Mage_Payment_Model_Method_Abstract (payment module Model) or anywhere with an getOrder() method

Desired Entity:

Mage_Catalog_Model_Product of an item in the cart

Code:

Gets the Mage_Catalog_Model_Product of the first item in the cart...

  1. $item = current( $this->getOrder()->getAllItems() );       
  2. $prod = Mage::getModel( 'catalog/product' )
  3.     ->load( $item->getProductId() );

Notes:

An “item” is another class altogether and lacks methods for returning a product or accessing things like custom product attributes.

Accessing the Value of a Product's Custom Attribute

Current Scope:

Mage_Catalog_Model_Product

Desired Entity:

Value of a product’s custom attribute as set by an administrator in the backend

Code:

  1. // Take the attributes code (see Manage Attributes in the backend)
  2. // and remove underscores capitalising the next letter.  Also
  3. // capitalise the first letter and access as follows:
  4.  
  5. // Eg. Attribute code: "my_attrib_code"
  6.  
  7. $myprod->getMyAttribCode();

Notes:

Mage implements dynamic methods for its (custom) attributes

Accessing the Text of a Custom Attribute's Dropdown

Current Scope:

Anywhere

Desired Entity:

Text of a dropdown option selected by an administrator for a custom product or category attribute (i.e. “Text” in <option value=”1”>Text</text>)

Code:

  1. // Get the literal value selected
  2. // Load the attribute object
  3. $model_att = Mage::getModel('eav/entity_attribute');
  4.  
  5. // $att_code is the attribute code string (see Manage Attributes section in the backend)
  6. // substitute 'catalog_product' here for different entity attribs (eg. catalog_category)
  7. $att_id = $model_att->getIdByCode( 'catalog_product', $att_code );
  8.  
  9. $model_att->load( $att_id );
  10.        
  11. // Get the literal value
  12. $model_att_options = Mage::getModel('eav/entity_attribute_source_table'); // We get the model handling eav_attribute_option           
  13. $model_att_options->setAttribute($model_att); // We set our attribute for this model
  14.                
  15. // $att_value is the Magento value for the option in the drop down (usually an enumerated integer)
  16. $att_option_text = $model_att_options->getOptionText( $att_val );

Notes:

Magento assigns enumerated integers for an attribute’s dropdown/select box’s values depending on how many options there are in total and what order they are in on the page, i.e. its a bit arbitrary as the value assigned to each option depends on what order you created the custom attributes. Use this code to get the literal value displayed to the admin user.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs