Get product order custom attributes in save_order_save_after observer in Magento 2 - magento

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.

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

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 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...
}

Magento Shipping Module: Get all items currently in cart

I'm in the middle of developing a shipping module for Magento but get stuck on How to get the items that's currently in the cart for that session.
I follow some tutorial on the internet, they use:
if ($request->getAllItems()) {
foreach ($request->getAllItems() as $item) {
//do something here.....
}
}
My problem is that I don't know exactly what info/data that's on the $item variable??.
I want to get the weight and the price of the product that's currently on the cart to calculate the shipping fee. I tried to print the $item value by using Mage::log or printing it to the screen using print_r or var_dump but it's not successful. The log is empty and the variable won't be printed on screen.
Can somebody inform me of how to get the $item attributes/method or is there any other way to get the product information that's currently in cart?
you can achive this by using one of three methods which are available in Mage::getSingleton('checkout/session')->getQuote();
getItemsCollection() - retrive sales/quote_items collection
getAllItems() - retrive all items
getAllVisibleItems() - retrive items which aren't deleted and have parent_item_id != null
I was searching for this solution and found below. but not tested.
$CartSession = Mage::getSingleton('checkout/session');
foreach($CartSession->getQuote()->getAllItems() as $item)
{
$productWeight = $item->getWeight();
$productExPrice = $item->getPrice(); // price excluding tax
$productIncPrice = $item->getPriceInclTax(); // price excluding tax
}
If you want to know information about $item, you can do this:
print_r($item->getData());
If you want to get item weight, here is it:
$item->getWeight();

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