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

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

Related

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.

How to get product name in magento?

I am trying to get only product name using the product id.
I have tried the below:
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->getName();
It load the whole resource object and than we can get the name using $product->getName() but I need only single name so we can reduce the extra overheads.
$product = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name')->addAttributeToFilter('entity_id', PRODUCT_ID)->load();
It also load the resource object.
is there any easiest way to get the product name( for exa. 'Test product') without loading the resource object?
Use getAttributeRawValue() With this you can get the attribute value of your product :
$attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue(PRODUCT_ID, 'attribute_code');
Try this,hope it's work for you.
Try this :
$product = Mage::registry('current_product'); then
$product->getName();
$product->getId();
You can try this for product :
$id = $this->getRequest()->getParam('id');
$current_product=Mage::getModel('catalog/product')->load($id);
We need to pass store value as well to get product attribute directly from resource model.
You can get store id by using this code:- Mage::app()->getStore().
So the final query will be :-
$attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue(PRODUCT_ID,'attribute_code',Mage::app()->getStore());

How to assign categories to a new product automatically?

I am new to Magento and not familiar with it. My requirement is to assign a category to new products automatically. How could I do that?
If you have fixed category to assign when new product is saved then it can be done by observer event (like catalog_product_save_after).
When product is saved, in observer you can write code to assign that newly saved product to particular category.
Following is snippet to assign product to category.
$categories = $product->getCategoryIds();
$categories[] = $categoryId;
$product->setCategoryIds($categories);
$product->save();

Magento: Get product price from another store?

I have multi-store Magento installation, and different product prices are set in different stores. I want to display on one page the actual product price from the current store, and the price from other store (I have its ID), but I'm not sure how to get that info?
The prices are set for each store view for each product, no tier-pricing or special-pricing was used.
If you know the storeId, set in setStoreId :
/**
* call the Magento catalog/product model
* set the current store ID
* load the product
*/
$product = Mage::getModel('catalog/product')
->setStoreId($storeId)
->load($key);
Display in a block :
echo $product->getName();
We can also use print_r to see the values :
print_r($product->getData());
The following code will show current store ID :
$storeId    = Mage::app()->getStore()->getId();
To get all product ID's with each store view :
$product    = Mage::getModel('catalog/product');
$products   = $product->getCollection()->addStoreFilter($storeId)->getData();
If you change the $storeId will show different product.

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