Magento products from attribute - magento

I have the following code, which will get all products from all orders for one logged in customer which works fine. I want to add to this code so that it only returns products from a specified attribute set. I believe I have both bits of code that I need they just won't work together.
Code I want to add to is:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
$customer_id = $customer->getId();
}
$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
echo various variables here;
}
}
}
?>
Code I have used before to get products from a specified attribute set
$attrSetName = 'Beer';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
->load($attrSetName, 'attribute_set_name')
->getAttributeSetId();
//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('attribute_set_id', $attributeSetId);

You can add this piece of code to make your script work in the foreach loop.
$product = Mage::getModel('catalog/product')->load($sku, 'sku');
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if(0 == strcmp($attributeSetName, 'Beer') {
//add your logic
}else{
continue;
}
Update:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
$customer_id = $customer->getId();
}
$collection =
Mage::getModel('sales/order')->getCollection();
$collection->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
$item->getProduct()->getSku();
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($item->getProduct()->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if(0 == strcmp($attributeSetName, 'Beer')) {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
// echo various variables here;
}else{
continue;
}
}
}
}
print_r($uniuqProductSkus);
?>

Related

Requesting solution for laravel 8 project

I'm an elementary web developer, I'm facing a problem. Please help me solve this. I'm creating both row and column dynamic. for save() is fine. But for update, only name is updated and other item
quantities cannot. Here is my codes.
//for save()
public function stockAdd(Request $request)
{
$validator = validator(request()->all(), ['stock_name' => 'required']);
if($validator->fails()) {
return back()->withErrors($validator);
}
$user_id = Auth::user()->user_id;
$stock_id = random_int(100000, 999999);
$stocks = new Stocks();
$stocks->stock_id = $stock_id;
$stocks->owner_id = $user_id;
$stocks->stock_name = request()->stock_name;
$stocks->my_stock = 0;
$stocks->disable = 0;
$stocks->save();
$brands = Brands::all();
$products = Products::all();
foreach ($brands as $brand){
foreach ($products as $product){
if($product['brand_id'] == $brand['id']){
$product_id = $product->product_id;
$stockitems = new StockItems();
$stockitems->stock_id = $stock_id;
$stockitems->product_id = $product->product_id;
$stockitems->owner_id = $user_id;
if(request()->$product_id > 0){
$stockitems->count = request()->$product_id;
}else{
$stockitems->count = 0;
}
$stockitems->save();
}
}
}
return redirect()->back()->with('successAddMsg','Successfully Added');
}
//for update()
public function stockUpdate(Request $request, $id)
{
$validator = validator(request()->all(), ['stock_name' => 'required']);
if($validator->fails()) {
return back()->withErrors($validator);
}
$stock = Stocks::findOrFail($id);
$stock->stock_name = request()->stock_name;
$stock->my_stock = 0;
$stock->disable = 0;
$stock->update();
$brands = Brands::all();
$products = Products::all();
foreach ($brands as $brand){
foreach ($products as $product){
if($product['brand_id'] == $brand['id']){
$product_id = $product->product_id;
//dd($product_id);
$stockitemitems = DB::table("stock_items")->select("*")->where("owner_id",Auth::user()->user_id)->where("stock_id",$stock->stock_id)->get();
$stockitems->count = $request->input($product_id);
$stockitems->update();
}
}
}
return redirect(route('user.stock-list'))->with('successAddMsg','Successfully Added');
}
The problem shows like this "Creating default object from empty value"

Magento: how to get the quantity of each bundle option that was ordered

//Observer function
$order = $observer->getEvent()->getOrder();
foreach($order->getAllItems() as $item){
if($item->getProductType() == 'bundle')
{
//Loading bundle product object
$bundle_product = Mage::getModel('catalog/product')->load($item->getProductId());
//Getting bundle items collection
$selectionCollection = $bundle_product->getTypeInstance(true)->getSelectionsCollection($bundle_product->getTypeInstance(true)->getOptionsIds($bundle_product), $bundle_product);
foreach($selectionCollection as $option)
{
//Loading each bundle item
$bundle_item = Mage::getModel('catalog/product')->load($option->getId());
//How to get the quantity that was ordered? example:
$bundle_item->getQtyOrdered(); //Note: I know this is wrong, this is not the correct object.
}
}
}
Please use below code:
//Observer function $order = $observer->getEvent()->getOrder();
foreach($order->getAllItems() as $item){
if($item->getProductType() == 'bundle')
{
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
$bundleOption=$options['info_buyRequest']['bundle_option_qty'];
foreach($bundleOption as $bundleItemQty){
echo 'Bundle Item Qty='.$bundleItemQty;
}
}
}

Return products if Category is not Active

How can I make $cat->getProductCollection(); to return products if the category is NOT ACTIVE
It's been a very long time. I could not make this to work.
This is what I am doing. But no Luck
private function _getCollection() {
$category = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>False))
->addAttributeToFilter('entity_id',array('eq'=>'61'))
->load();
foreach($category as $cat){
$Prods = $cat->getProductCollection();
$Prods->addWebsiteFilter();
$Prods->addAttributeToSelect('*');
}
return $Prods;
}
Try this code
$cat = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('is_active', 1);
foreach ($cat as $category){
$cat_id = $category->getId();
$category = new Mage_Catalog_Model_Category();
$category->load($cat_id);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) {
echo $_product->getName();
}
}

How can i save attributes values against product ids in magetno

How can I add Products attributes value against product ids's. I want to add attributes values for specific product id using code.My code for products attribute is
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
//Load the particular attribute by id
//Here 73 is the id of 'manufacturer' attribute
$attr_model->load(73);
//Create an array to store the attribute data
$data = array();
//Create options array
$values = array(
//15 is the option_id of the option in 'eav_attribute_option_value' table
15 => array(
0 => 'Apple' //0 is current store id, Apple is the new label for the option
),
16 => array(
0 => 'HTC'
),
17 => array(
0 => 'Microsoft'
),
);
//Add the option values to the data
$data['option']['value'] = $values;
//Add data to our attribute model
$attr_model->addData($data);
//Save the updated model
try {
$attr_model->save();
$session = Mage::getSingleton('adminhtml/session');
$session->addSuccess(
Mage::helper('catalog')->__('The product attribute has been saved.'));
/**
* Clear translation cache because attribute labels are stored in translation
*/
Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
$session->setAttributeData(false);
return;
} catch (Exception $e) {
$session->addError($e->getMessage());
$session->setAttributeData($data);
return;
}
Hi after search i find some code that save my attribute values and than i store them against products.This is my code :
public static function getAttributeOptionId($arg_attribute, $arg_value,$id)
{
$id = $id;
//load the attribute by attribute code
$entityTypeID = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
$attribute_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
$type = $attribute->getBackendType();
foreach($options as $option) {
if ($option['label'] == $arg_value) {
$arrk[] = $option['label'];
}
}
$optiont = self::getAttributeOptionValue($arg_attribute, $arg_value);
if(!$optiont){
$value['option'] = array($arg_value,$arg_value);
$result = array('value' => $value);
$attribute->setData('option',$result);
$attribute->save();
$product = Mage::getModel('catalog/product')->load($id);
$product->setMyAttribute($arg_attribute)->save();
}
return 0;
}
public function getAttributeOptionValue($arg_attribute, $arg_value) {
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
$attribute_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);
foreach($options as $option) {
if ($option['label'] == $arg_value) {
return $option['value'];
}
}
return false;
}.
Where $arg_attribute is the attriubte code and $arg_value is his value.Id is product id for specific sku.I am saving records in my custom module.If anyone know issue with this code please inform me.Thanks

Multiselection in Layered navigation In Magento

In layered navigation, when i select particular attribute, it gets disappeared from left side selection panel, so I want I should be able to select more than once.
If I come to know from what collection it is coming, that will be helpful.
-Thanks in Advance.
public function apply(Zend_Controller_Request_Abstract $request, $filterBlock) {
$filter = $request->getParam($this->_requestVar);
if (is_array($filter)) {
$text = array();
foreach ($filter as $f)
array_push ($text, $this->_getOptionText($f));
if ($filter && $text) {
$this->_getResource()->applyFilterToCollection($this, $filter);
$this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
$this->_items = array();
}
return $this;
}
$text = $this->_getOptionText($filter);
if ($filter && $text) {
$this->_getResource()->applyFilterToCollection($this, $filter);
$this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
$this->_items = array();
}
return $this;
}
public function applyFilterToCollection($filter, $value)
{
$collection = $filter->getLayer()->getProductCollection();
$attribute = $filter->getAttributeModel();
$connection = $this->_getReadAdapter();
$tableAlias = $attribute->getAttributeCode() . '_idx';
if (!is_array($value)) {
$conditions = array(
"{$tableAlias}.entity_id = e.entity_id",
$connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
$connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
$connection->quoteInto("{$tableAlias}.value = ?", $value)
);
}else{
$conditions = array(
"{$tableAlias}.entity_id = e.entity_id",
$connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
$connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
$connection->quoteInto("{$tableAlias}.value in (?)",$value)
);
}
$collection->getSelect()->join(
array($tableAlias => $this->getMainTable()),
implode(' AND ', $conditions),
array()
);
//echo $collection->getSelect();
return $this;
}
I've done something similar, and you have to rewrite the Mage_Catalog_Model_Layer_Filter_Attribute class, which uses the method apply() to filter results.

Resources