Magento: Filter Configurable Product by their options - magento

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!

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

Get product order custom attributes in save_order_save_after observer in Magento 2

I want to capture some product custom attributes for the item in the order in magento 2.
I tried with getAttribute() and getAttributeText(‘attribute_code’)
Please help
Use this code to get product attribute for product items in save_order_save_after observer in Magento 2.
$orderObj = $objectManager->create('Magento\Sales\Model\Order')->load($orderID);
$orderAllItems = $orderObj->getAllItems();
if ($orderAllItems) {
foreach ($orderAllItems as $item) {
$product_manufacturer = $item->getManufacturer();
}
}
You can get all attribute from $item.

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 get color types for product

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

Magento - Get Product SKU's and quantities on order

I have created an event observer on the "sales_order_place_after" event, which fires when the user places an order in Magento's checkout. That event triggers my observer, which needs to get the following information, which is then sent to an external inventory management system.
Information needed: The SKU and quantity of every product on the order
I have included part of my observer model below. I am accessing the order object. I can get all the items in the order, but how do I get the SKU for every product that makes up the order. For example, with a simple product, this is very easy. However, with a bundled or configurable product I do not know how to access the children that make up that bundled product with their SKU's and quantities. That is the info I need for both bundled and configurable products. I need the SKU and quantities of the children that were selected. For the life of me, I can't figure out what method to call to access that information. I wish there was something like: $item->getBundleChildrenSkuQuantity();
$order = $observer->getEvent()->getOrder();
$joomecomPacket = array();
if ($order->getTotalItemCount() > 0) {
$items = $order->getAllItems();
foreach ($items as $item) {
$productType = $item->getProductType();
switch ($productType) {
case 'bundle':
break;
case 'configurable':
default: // simple products
if (isset($joomecomPacket[$item['sku']])) {
$joomecomPacket[$item['sku']] += $item['qty_ordered'];
} else {
$joomecomPacket[$item['sku']] = $item['qty_ordered'];
}
break;
}
}
}
You're missing a break after your configurable case. The default case is executing for configurable products.
Try this:
$items = $order->getAllVisibleItems(); // gives only parent items
foreach ($items as $item){
$childItem = $item->getChildren(); //do something with $cildItem like $childItem->getSku() etc...
}

Resources