SKU groups all the attributes that applies to a product and assigns a fixed number/value to it so that that one number could define a set of values within an attribute group.
First of all, SKU (stock keeping unit) does not group anything. A SKU is a reference code for an item for which you keep inventory. Therefore, it must be unique; otherwise, you will not be able to keep accurate inventory for all of the items that require keeping inventory.
So what you see now is that the stock of two different products got affected but the attributes were same and hence we kept SKUs also same.
Additionally, SKUs are not attributes as you have described. Your SKUs should be MN-VN-TS-SZ-L-COL-GRY
, etc. not just SZ-L-COL-GRY
, etc. So, your products indeed have unique SKUs.
So my questions is why dose the SKU needs to be unique for each product? What purpose dose it serves by being unique?
Lastly, product IDs are for the system (Magento). If you were to use product IDs as your unique identifier for a product, nobody could use codes they wanted. A lot of merchants use meaningful SKU codes, as you have demonstrated. Therefore, a SKU is a one-to-one representation of a product ID that you can customize. Besides, do you really want everybody to use SKUs 1 ~ 10 to describe their first ten products?
To answer your last two questions, which are actually the same question, a SKU needs to be unique in order for you to distinguish between items and keep accurate inventory.
Eventually, I've been able to come up with a method that does exactly what I need.
private function getSkuForOptions($product, $options) {
$usedProducts = $product->getTypeInstance(true)->getUsedProducts(null, $product);
$sku = -1;
foreach ($usedProducts as $childProduct) {
if (!$childProduct->isSaleable()) {
continue;
}
$data = $childProduct->getData();
$match = true;
foreach ($options as $id => $value) {
if (!isset($data[$id]) || $data[$id] !== $value) {
$match = false;
}
}
if ($match) {
$sku = $childProduct->getSku();
break;
}
}
return $sku;
}
The $product
parameter is the object returned by the method getItemByUrl
, described in my original question. The parameter $options
is an array in the following format:
attribute_code => value_id
For example, the red XL shirt is associated with the following options array:
array(2) {
'color' => 28
'size' => 78
}
My method looks for the saleable instances of the base product and tries to match their options to the ones supplied.
This idea came to me after many hours of reverse-engineering the magento core and reading this article: http://www.divisionlab.com/solvingmagento/magento-configurable-product-type-tutorial/
Best Answer
If neither of the following are working.
Mage::getModel('catalog/product')->load($sku, 'sku');
Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
Then you could always use the resource model,
Mage_Catalog_Model_Resource_Product
, to get the product id via the functiongetIdBySku
and then simply do a normal load.