magento get color types for product - magento

I have a product with different colors(white and blue) and different sizes(medium and large)
How do i get those values for a product.
I am trying to get all values for a color using following code:
require_once '../magento/app/Mage.php';
Mage::app();
$store_id = Mage::app()->getStore()->getStoreId();
$config = Mage::getModel('eav/config');
$attribute = $config->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');
$values = $attribute->setStoreId($storeId)->getSource()->getAllOptions();
print_r($values);
But i want to get only 2 colors and sizes for a product.

If you are having a product with different colors(white and blue) and different sizes(medium and large)
you must have been confused somewhere as this is not a single product. Rather this is a configurable products with 2 simple product
$attrs = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
foreach($attrs as $attr) {
if(0 == strcmp("size", $attr['attribute_code'])) {
$sizess = $attr['values'];
}
}
This would provide attributes options

Related

How to programmatically get option id's and values from configurable product in phtml?

I am modifying phtml code were I have access to
$this which happens to be Mage_Checkout_Block_Cart_Item_Renderer_Configurable class
and
$_item = $this->getItem();
$_product = $_item->getProduct();
where product is a configurable product in a cart.
Based on this I need to get a list of option id (swatches) and their values.
For example, to get specific size option id :
$product = $_item->getProduct();
$idStore = $_item->getOrder()->getStoreId();
$sizeSpecificAttributeCode = 'specific_size';
$sizeAttributeOptionId = $product->getResource()
->getAttributeRawValue($product->getId(), $sizeSpecificAttributeCode,$idStore);
from the same logic you can get the swatches or other options

Magento Configurable Import CSV

I've got the spreadsheet below...
https://docs.google.com/spreadsheet/pub?key=0AjZAunFJlooudFhaQjZFLWZHTGZRc0dwZUMtel9BNmc&output=html
All of the simple products are uploading fine (I've added one as an example). But the configurable is not 'associating' to the products correctly.
When I go into the back-end of Magento after the import and click on the configurable product it asks me which Attributes I would like to assign against the configurable product.
Does anyone know why I cannot associate the configurable product with the simple products? There's obviously an issue with configurable product on the import.
With this function you can Assign/Unassign (merge) list of simple productids to existing configurable product
private function _attachProductToConfigurable( $_childProduct, $_configurableProduct ) {
$loader = Mage::getResourceModel( 'catalog/product_type_configurable' )->load( $_configurableProduct );
$ids = $_configurableProduct->getTypeInstance()->getUsedProductIds();
$newids = array();
foreach ( $ids as $id ) {
$newids[$id] = 1;
}
$newids[$_childProduct->getId()] = 1;
$loader->saveProducts( $_configurableProduct->getId(), array_keys( $newids ) );
}
Thanks

list the values of a product attribute set in magento

how do I find the values of a product's attribute set?
For example, there's a product with an Attribute Set called shirts - T, with attributes of Gender, Shirt Size, and Color. Starting with a $_product object, how do I find the values of the attributes, e.g. Mens, Green, Large?
i am able to getting the attribute set value in the following way:
$product = Mage::getModel('catalog/product')->load($productId);
$prodAttributeSet = Mage::getModel('eav/entity_attribute_set')->load($product->getAttributeSetId())->getAttributeSetName();
I want to get all available attribute set values and codes for specific attribute set(i.e shirt - T)
$_product = Mage::getModel('catalog/product')->load($productId);
Now suppose you want to access value of manufacturer of this product, then consider following code.
$manufacturerValue = $_product->getAttributeText('manufacturer');
As you mention in comment for size and color here i can give you one sample code to use.
If it is a select or multiselect attribute, you still need to map the option ID's to option values. That is the case if you are getting a list of integers instead of human readable labels (e.g. for the color or manufacturer attribute).
// specify the select or multiselect attribute code
$attributeCode = 'color';
// build and filter the product collection
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter($attributeCode, array('notnull' => true))
->addAttributeToFilter($attributeCode, array('neq' => ''))
->addAttributeToSelect($attributeCode);
$usedAttributeValues = array_unique($products->getColumnValues($attributeCode));
// ---- this is the different part compared to the previous example ----
// fetch the attribute model
$attributeModel = Mage::getSingleton('eav/config')
->getAttribute('catalog_product', $attributeCode);
// map the option id's to option labels
$usedAttributeValues = $attributeModel->getSource()->getOptionText(
implode(',', $usedAttributeValues)
);
Direct DB query example
Depending on where you want to do this, here is an example of fetching the values without using a product collection. It is slightly more efficient.
Only use the following code in resource models, as thats where DB related code belongs.
This is meant as an educational example to show how to work with Magento's EAV tables.
// specify the attribute code
$attributeCode = 'color';
// get attribute model by attribute code, e.g. 'color'
$attributeModel = Mage::getSingleton('eav/config')
->getAttribute('catalog_product', $attributeCode);
// build select to fetch used attribute value id's
$select = Mage::getSingleton('core/resource')
->getConnection('default_read')->select()
->from($attributeModel->getBackend()->getTable(), 'value')
->where('attribute_id=?', $attributeModel->getId())
->distinct();
// read used values from the db
$usedAttributeValues = Mage::getSingleton('core/resource')
->getConnection('default_read')
->fetchCol($select);
// map used id's to the value labels using the source model
if ($attributeModel->usesSource())
{
$usedAttributeValues = $attributeModel->getSource()->getOptionText(
implode(',', $usedAttributeValues)
);
}
If you want all the attributes of an attribute set you can do the following.
$product = Mage::getModel('catalog/product')->load($productId);
$attributes = $eavConfig->getEntityAttributeCodes( Mage_Catalog_Model_Product::ENTITY, $product );
print_r(attributes);

Magento - Varien Grid - Totaling entire collection

I have created a custom product grid using the varien grid functionality and am using it as an order form. Basically, there is a Product Name, Product Price, and an input field for the user to enter the amount of a product they would like.
There are thousands of products, and I need to give a running subtotal of what the user has selected.
The closest I've gotten is as adding the following to Grid.php...
protected function _afterLoadCollection()
{
$collection = $this->getCollection()->load();
$total = 0;
if ($collection) {
foreach($collection as $item) {
$itemPrice = $item->getPrice();
$itemQty = $item->getQty();
$total = $total + ($itemPrice * $itemQty);
}
echo $total;
}
}
However, the results are affected by the current limit, and therefore only totals the current page.
Is there a way around this, or a better way to get the running total of the two columns?
Thanks in advance, I've been stuck on this for days!
You can try this to clear the collection complements, and set for the first page a big limit
$_productCollection = $this->getLoadedProductCollection();
$_productCollection->clear();
$_productCollection->setPage(1,99999);

Magento: Filter Configurable Product by their options

I want to filter some configurable products by attributes that are used to create more instances of that product (size, color etc.). This means those attributes are not directly assigned to the configurable product, but their childs.
I already have a code that filters configurable products by some attributes, but these are all assigned to the main product, and the children inherit that: designer.
$attributes_designers = $this->getRequest()->getParam('designers');
$attributes_colors = $this->getRequest()->getParam('color');
$attributes_sizes = $this->getRequest()->getParam('size');
$currentCategory = Mage::getModel('catalog/layer')->getCurrentCategory();
$_productCollection = $currentCategory->getProductCollection();
if(count($attributes_designers)>0 and !in_array("ALL",$attributes_designers)) {
$_productCollection->addAttributeToFilter('designer',$attributes_designers);
}
if(count($attributes_colors)>0 and !in_array("ALL",$attributes_colors)) {
$_productCollection->addAttributeToFilter('color',$attributes_colors);
}
if(count($attributes_sizes)>0 and !in_array("ALL",$attributes_sizes)) {
$_productCollection->addAttributeToFilter('size_apparel_eu',$attributes_sizes);
}
if(isset($_GET['order'])) $_productCollection->setOrder($this->getRequest()->getParam('order'), $this->getRequest()->getParam('dir'));
$_productCollection->load();
here, the color, and size_apparel_eu, are not working, because those are not directly assigned to the product, but their children.
If I'm understanding you correctly here...You first need to get the child products with something like this:
$_product = $this->getProduct();
$_configurable_model = Mage::getModel('catalog/product_type_configurable');
$_child_products = array();
if ($_product->getTypeId() == 'configurable')
$_child_products = $_configurable_model->getUsedProducts(null, $_product);
Then you can use some sort of loop (foreach is handy)
foreach ($_child_products as $_child_product){
$_child_product->getRequest()->getParam('designers');
}
Or however you need to call those methods to get the child product information.
I hope this was helpful and relevent!

Resources