A place to log my programming solutions.

Tagged :: magento

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.

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

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

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 »

8
Sep 09

Magento Clear Shopping Cart and Session

If you ever need to programmatically clear Magento’s shopping cart, use the snippet of code below. If you need to clear the user’s entire session, including the shopping cart, shipping information, billing information and chosen payment methods, please refer to the snippet further below.

Read more »

20
Aug 09

Clear Magento Customers, Orders, Searches, and Tags

From the Magento dashboard, there is no intuitive way of deleting/clearing/resetting customer, order, search, or tag data. As developers, we have to test the various functions of our specific Magento setup before presenting it to the client. Before the site can go live, all of the test data has to be removed so it doesn’t interfere with the client’s other business functions such as accounting and inventory.

Read more »

18
Aug 09

Magento Configurable Product Pricing

Let’s say for example that configurable product A is defined to have 2 attributes “color” and “size”.  Values for color may be: Black, White, Chrome while values for size may be: 1″, 2″, 3″, and 4″.

One would expect that there should be a price matrix that might look like:

1″ 2″ 3″ 4″
Black $1.00 $1.20 $1.40 $1.60
White $1.05 $1.25 $1.45 $1.65
Chrome $1.15 $1.35 $1.55 $1.75

Read more »

17
Jan 09

Magento Platform Revisited

It’s been almost three weeks now since I’ve started learning the Magento ecommerce platform.  With an impressive array of features, like product comparisons and a web services API, the platform sounds promising.  I had hoped that by the time I’d revisit this topic, I’d be able to say I’ve successfully implemented the software.  Instead I’m having trouble setting up my product catalog (it involves importing).  And how can I forget to mention the overall issue of speed?

Let’s start at the beginning, at installation.  Your web server likely has capable hardware, so you need to be sure that your software configuration meets requirements.  Luckily the host I’m on was set up just fine the way it was.  Obviously you need to download the software – I chose to download the Full Release (version 1.1.8 at the time).  Read more »

About NVNCBL and Myself

Contact Me