A place to log my programming solutions.

Category :: Programming

8
Mar 10

Magento – Set Product Dropdown and Multiselect Values Programmatically

Here’s how to programmatically, meaning through code, set a product’s attribute value when the attribute is of type Dropdown or Multiselect.  For dropdown, we’ll be interested in setting only a single value.  And obviously for multiselect, we’ll be interested in setting multiple values.

With Text attributes such as Name and Description, you can do something like:

$product->setName( 'My Sweet Shirt' );
$product->setDescription( 'This shirt will make you look good, thus impressing girls.' );

Unfortunately, this won’t work for attributes whose values are predefined.  Instead of cursing Magento, think of it as a data-integrity/validation measure.

Anyway, assuming you already have a $product object, our overall process will be to:

  1. Load an attribute object for the attribute you want to work on
  2. Load the collection of that attribute’s values
  3. Make your choices (different for Dropdown and Multiselect)
  4. Save the product

1. Load an attribute object for the attribute you want to work on

$attribute = Mage::getModel('eav/entity_attribute');
$attribute->loadByCode( 4, 'color' );

2. Load the collection of that attribute’s values

$values = array();
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
	->setAttributeFilter( $attribute->getId() )
	->setStoreFilter( Mage_Core_Model_App::ADMIN_STORE_ID, false)
	->load();
 
foreach ($valuesCollection as $item) {
	$values[$item->getValue()] = $item->getId();
}

3. Make your choice – DROPDOWN

$product->setColor( $values['Blue'] ); // just do whatever you need to code 'Blue' instead of hard-coding it

3. Make your choices – MULTISELECT

$product->addData( array(
	'color' => $values['Blue'] .','. $values['Red'] .','. $values['Black']  // just putting together a comma-separated list of values
) );

4. Save the product

$product->save();

This builds off the solution posted by member “icvu” here: http://www.magentocommerce.com/boards/main.php/viewthread/69925/ — specifically the addData() part.

Thanks, and have fun setting attribute values.

7
Mar 10

ConstantContact Send Welcome Email When Creating a New Contact via API

Using the ConstantContact mailing list, I was having trouble getting the welcome email to send after creating a new contact through the API. I had tried checking the “Autoresponder” box in the admin panel, but still no welcome email. The solution is to have:

var $actionBy = 'ACTION_BY_CONTACT';

in cc_class.php (provided via the “PHP Sample Forms” zip file). By default, this value is:

var $actionBy = 'ACTION_BY_CUSTOMER'

instead, which does not trigger the welcome email.

6
Mar 10

Refresh Magento Cache Programmatically

When writing processes to automate some of Magento’s normally laborious tasks, perhaps for a product-import script, a mass attribute update script, category import, or inventory adjustment, keep in mind that you may have to refresh some kind of cache — especially when working with attributes. If you are encountering quirks or inconsistencies with your data, try these:

  • Rebuild Catalog Index
    Mage::getSingleton('catalog/index')->rebuild();
  • Rebuild Flat Catalog Product
    Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
  • Inventory Stock
    Mage::getSingleton('cataloginventory/stock_status')->rebuild();

For more cache refreshes, such as:

  • Catalog Rewrites
  • Layered Navigation Indices
  • Product Image
  • Search Index

… please refer to the source of this knowledge http://www.magentocommerce.com/boards/viewthread/43238/#t156277

19
Feb 10

Center Align a UL Menu

The task of center-aligning an unordered list (which can’t have its width explicitly set) via CSS comes up every now and then. Logging it here for later.

“For a start you could text-align: center the ul inside the #ul_container_div and display: inline the li instead of floating them left.”

#ul_container_div{ width:100%; text-align:center; }
#ul_container_div li { display: inline;  }

Source of knowledge: http://archivist.incutio.com/viewlist/css-discuss/59041

17
Feb 10

IE6 Phantom Text / Duplicate Text Bug

Just for purposes of documenting this useful knowledge:

When viewing in Internet Explorer 6 and experiencing the “strange-repeated-text” problem, try applying this CSS to the element containing the text that gets repeated:

float: none;
clear: both;

Knowledge from: http://uxdev.blogspot.com/2008/10/phantom-text-in-ie6.html

3
Dec 09

Force IE8 to Render Like IE7

Found this little snippet yesterday. I’m not saying that this is best practice in any way, but if you’re looking for a quick fix, here it is:

<meta content='IE=EmulateIE7' http-equiv='X-UA-Compatible'/>

I think this has the effect of enabling “Compatibility Mode” that the user would normally have to turn on.

As a side note, the specific reason I used this was to make a Google Friend Connect widget render properly — there are plenty of posts on the web about those widgets not working in IE8.

26
Oct 09

Magento $this->getSkinUrl() Wrong Template

When using $this->getSkinUrl(‘path/to/yourfile.html’) inside view (.phtml) code, and you’re getting /default/default/ instead of /default/yourtheme, this isn’t Magento working incorrectly.  Check the $argument you send and verify that the target file actually exists.

getSkinUrl() does more than just generate the proper URL, it also verifies that the file exists.  If it doesn’t find the file within the store’s /yourtheme folder, it will revert to /default.

12
Oct 09

Script Double Run / Double Execute Phenomenon

I had a problem with your PHP code running or executing seemingly twice.  To debug this phenomenon, I put some write-to-file code at the beginning of the script.  Surprisingly, whenever I loaded my script in the browser, the file would be written to twice despite my one page load.

The problem was that The page I was developing had an <img> in it with the “src” attribute set blank.  When the document was fully loaded, javascript was in place to dynamically assign the “src”.  This worked perfectly fine in the pre-server-side-logic stage of development so it was an easy decision to begin debugging server-side logic.

The double execute happens when Firefox sees <img src=”" />.  Not knowing what source to assign, it sends a request (the second request) to the current path (again) for the image.  Therein lies the problem.  It’s interesting to note that Firefox is not to blame, as it’s following HTML specification.

12
Oct 09

Introducing Magento Widgets

Magento Widgets allow business users with no technical knowledge to easily add dynamic content (including product data, for example) to pages in Magento Stores.  This allows for greater control and flexibility in creating informational and marketing content through administrator tools, enabling intuitive and efficient control of content Read more »

About NVNCBL and Myself

Contact Me