Get Magento option entity value based on the value's id - magento

Here is the scenario specific to a base installation of magento.
I would like to be able to change some content on my category pages when a manufacturer filter has been chosen.
What I see is
http://www.myurl.com/path/to/cat.html?manufacturer=777
From this I can get the manufacturers id by doing something like:
$request->getParam('manufacturer');
What I would like to be able to do is jump into eav_attribute_option_value and get the manufacturers name based on the value_id (8404 in my case) and the manufacturer id to return the name of the manufacturer.
I am sure there are other use cases where this would become handy too.
For my case I will be prepending the brand to the category title tag based on some other logic. This will create new highly branded category pages that should rank well on google.
So your base category title tag might look like: 'Washing Machines'
And then the manufacturer brand filter page for Maytag would look like: 'Maytag Washing Machines'

I was able to find a dirty way to do this.
$query = "SELECT * FROM mg_eav_attribute_option_value WHERE option_id =" . $request_params['manufacturer'] . " LIMIT 0 , 30";
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$manufacturer = $read->fetchAll($query);

Related

Mass update of product attribute text

A previous developer at my work assigned all products an attribute called "delivery_text", which holds a string of html. For each product, the main body of this string is unique. However each one contains a link to a specific url (the link is the same for every product). Due to changes in our site layout, we now need to update this link for every product, without impacting the rest of the attribute text.
Obviously, I don't want to have to manually edit thousands of products just to change the url of the link in each product's delivery_text string.
Is there a way I can programmatically parse the contents of this attribute for every product, and replace instances of links to oldsite.com/old-url with links to newsite.com/new-url?
I presume I can do it with some kind of database trickery, and have access to phpmyadmin via cpan, but I have no idea how to actually go about doing so.
Note that I don't consider this a duplicate question, as while there are many "how do i mass update attribute values for all products" type questions, I have yet to find one that asks "how do i search and replace a specific character sequence within the attribute text for each product".
If you want to do this from the database, you could do something like this:
Find the attribute values
SELECT at.* FROM catalog_product_entity_varchar AS at
INNER JOIN eav_attribute AS ea
ON ea.attribute_id = at.attribute_id
WHERE ea.attribute_code = 'delivery_text';
This should show you a list of all of the attribute values in your database currently.
Update the attribute values
Once you've found all the values, you can use the same query to replace the URL within the values:
UPDATE catalog_product_entity_varchar AS at
INNER JOIN eav_attribute AS ea
ON ea.attribute_id = at.attribute_id
SET at.value = REPLACE(at.value, 'http://oldurl.com', 'http://newurl.com')
WHERE ea.attribute_code = 'delivery_text';
Doing it without SQL
You could also do this using the Magento model APIs. Start by gathering the data you need, the same way as above:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('delivery_text');
foreach ($collection as $product) { /** #var Mage_Catalog_Model_Product $product */
var_dump($product->getDeliveryText());
}
Then you can update the attribute within your loop. Here you need to specify a store ID, so I've assumed you'll do this at the admin (global) scope:
$old = 'http://oldurl.com';
$new = 'http://newurl.com';
foreach ($collection as $product) { /** #var Mage_Catalog_Model_Product $product */
Mage::getModel('catalog/product_action')
->updateAttributes(
array($product->getId()),
array('delivery_text' => $new),
Mage_Core_Model_App::ADMIN_STORE_ID
);
}
Please note: Using Mage_Catalog_Model_Product_Action::updateAttributes will work quickly and efficiently to update product attributes at a given store level, but if you are not using the admin store level then you may not be able to retrieve the value in a collection because this method doesn't create a default value - it just does what it says it will; create/update a value at a given store level.

Magento - Get Manufacturer attribute's value List, Even if any Manufacturer is selected as filter

I am using Magento 1.7
I want to Get Manufacturer attribute's value List, Even if any Manufacturer is selected as filter
If my category page is for example say :
http://www.magento.com/index.php/categoryPage.html?manufacturer=145
I am not able to get "manufacturer" attribute value's List on this particular page.
As Manufacturer is passed as query string, It would be selected as filter.
But I need that list any how.
when I use the code from filter.phtml, I got Error message in Log :
"You cannot define a correlation name 'manufacturer_idx' more than once"
How can I achieve this ?
you can check the current url contain manufacturer then on the list page you can filter the product with manufacturer attribute by using below code
$manufacturerId = 3;
$attributeCode = 'manufacturer';
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter($attributeCode, $manufacturerId);
So it will display the list of product that contain the manufacturer attribute.

Magento : get related products from both product ID and current category ID

I totally begin with Magento during my training period. I've been working on a project for two weeks now and sometimes I can't figure out how to proceed.
I'm gonna try to represent my categories tree :
- accessories
* visors
* communication systems
* other
- helmets
* a lot of subcategories and subcategories...
My actual problem is : I'm in one of the accessories subcategory (for example visors). I added a form with a select allowing to choose a helmet model. When the select is submitted, I'd like to display the list of visors related to the chosen helmet model (that is actually a virtual product).
I can get the current category ID (in this case, visors) and the virtual product ID (so the helmet model). But I can't figure out how to display related products by both product ID and category ID.
I tried stuff like this :
$relatedProducts = Mage::getModel('catalog/product_link')
->getCollection()
->addCategoryFilter($myCurrentCat)
->addFieldToFilter('product_id',$myVirtualProductId)
->addFieldToFilter('link_type_id','1');
But it seems not to work.
Any help is welcome. Thanks.
EDIT : 10 days after I asked this question, I still don't know how to solve my problem. If anyone could help, even a little, just a clue...
I cannot test this at the moment, but you can try to do something like this:
$collection = Mage::getSingleton('catalog/product_link')
->useRelatedLinks()
->getProductCollection()
->setIsStrongMode();
$product = Mage::getModel('catalog/product')->load($productId);
$collection->setProduct($product);
$collection->addCategoryFilter($category); //I'm not sure if this will work correctly
I'm gonna test it, when I will get some more time.
Actually I just solved my problem by myself.
Here is my solution if it could help anyone in the future :
$mainProduct = Mage::getModel('catalog/category')->load($myCurrentCat->getId());
$mainProduct = $mainProduct->getProductCollection();
$mainProduct = $mainProduct->addAttributeToFilter('product_id', $myVirtualProductId);
foreach ($mainProduct as $product) {
$related = $product->getRelatedProductIds();
if ($this->array_contains($related, $myVirtualProductId)) {
//TODO
}
}

Filter a Magento Category Collection by a Product Attribute?

I have a Magento site with an extensive catalog (10,000 products) and a fairly deep category tree.
All of the product's categories stem from a top-level "All Products" category.
All products have a drop-down attribute of "Brand."
What I am trying to do is allow a visitor to start at the top of the category tree and navigate down the "All Products" tree either with or without the brand selected. The hitch is that we need to show the next level of subcategories at each level and I don't want to show empty categories if a brand doesn't apply.
For example, if the tree looks like this and one is on the "Screwdrivers Page," we'd like both the type and length categories to be visible.
All Products
Screw Drivers
phillips
2"
3"
5"
flathead
1"
3"
5"
I was able do this without worrying about the brand filter by creating a category collection:
$_category = $this->getCurrentCategory();
$current_level = $_category->getLevel();
$collection = $_category->getCollection();
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('parent_id')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('level',array('in'=>array($current_level+1,$current_level+2)))
->setOrder('position','ASC')
->load();
Based on my research it appears that it is not possible to directly filter a category collection by a product attribute.
Assuming that this is true:
What is the best way to accomplish this? I've thought of and preliminarily tried few ideas:
Create a product collection filtered by brand and loop through it to get all of the relevant categories and build the tree (seems like this would be horribly inefficient)
Try to utilize layered navigation to output the categories (this would work if we were only viewing one level -- I'm having trouble getting a filtered "second" level.)
Abandon the filtering idea and duplicate the category Tree under a new "Browse By Brand" Category (I'd like to stay away from this so that updating products doesn't require updating two duplicate trees.)
Thanks in advance for any help. Please let me know if you need any clarification of the problem.
In case anyone has a similar issue down the road I accomplished this by using the second bullet point above.
/* Get Current Filters by loading catalog/layer_view Block */
$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('catalog/layer_view');
/* Get Current Category */
$_category = $this->getCurrentCategory();
$current_level = $_category->getLevel();
/* Get Layer Filter Category Model */
$category_filter = Mage::getModel('catalog/layer_filter_category');
/* Generate the collection based on the current category */
$categories = $_category->getCollection();
$categories->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('parent_id')
->addAttributeToSelect('thumbnail')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('path', array('like'=>'%/'.$_category->getId().'/%'))
->addAttributeToFilter('level',array('in'=>array($current_level+1,$current_level+2)))
->setOrder('position','ASC')
->load();
/* Using current filters, add product counts to collection so we can hide categories with 0 products */
$category_filter->getLayer()->getProductCollection()
->addCountToCategories($categories);
After this it is a matter of looping through the collection and displaying the name, URL of the category, etc..
This may not be the absolute best way -- I certainly welcome other solutions.

Magento - How to add multiple items to the cart programmatically?

I'm trying to add multiple simple products to the cart at the same time using a query string as below, however it only adds the last product to the cart instead of both:
Can someone let me know what I'm doing wrong?
http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&product=15749&qty=4
I have also tried this:
http://www.domain.co.uk/checkout/cart/add?product[]=9916&qty[]=4&product[]=15749&qty[]=4
Any help much appreciated!
Add Product To Cart With Querystring
Add simple product in shopping cart with no attribute.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY
Here PRODUCT_ID = 'Product Id',PRODUCT_QUANTITY = 'product quantity to purchase'.
Add product into shopping cart with single custome option.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID]=OPTION_VALUE
Here OPTION_ID = 'Custom attribute option id',OPTION_VALUE = 'Custom attribute option value'.
Add product into shopping cart with multipal custome option.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID_1]=OPTION_VALUE_1&super_attribute[OPTION_ID_2]=OPTION_VALUE_2
Here OPTION_ID_1 & OPTION_ID_1 = 'Custom attribute option ids',OPTION_VALUE_1 & OPTION_VALUE_2 = 'Custom attribute option values'.Here add more options in `super_attribute` array
Add Extra products with mail product with only 1 quantity.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2
Here PRODUCT_ID_1 and PRODUCT_ID_2 is other products id. add more product by id using `,` separator. Example:- &related_product=1,2,3,4.
Default magento there is not setting for add related product quantity into cart.so if you want to add this code than open app/code/core/Mage/Checkout/controllers/CartController.php find public function addAction().
if (!empty($related)) {
$cart->addProductsByIds(explode(',', $related));
}
Replace with
$rel_qty = $this->getRequest()->getParam('related_qty');
if (!empty($related)) {
$relatedproducts = explode(',', $related);
$relatedqtys = explode(',',$rel_qty);
$i = 0;
foreach($relatedproducts as $relatedproduct)
{
$cart->addProduct($relatedproduct, array('qty'=>$relatedqtys[$i]));
$i++;
}
}
Now use query string for add related products with quantity.
http://yourserver.com/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2&related_qty=PRODUCT_ID_1_QUANTITY,PRODUCT_ID_2_QUANTITY
If you don't want to change any code, you can try to utilize related products functionality by adding related_product parameter to your request. So your url will look like this:
http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&related_product=15749
If you want to add more products, just list them with comma separator: related_product=1,2,3
The only drawback from that is that you actually can't specify the qty for related products.
To see how it works - Mage_Checkout_Model_Cart::addProductsByIds(array_of_ids)
If qty for subsequent products is a mandatory for you, you'll need to create your own controller, or override the Mage_Checkout_CartController::addAction method.
I found a cheeky way I found of getting around the quantity limitation of the related_products query string field noted above in other answers. If you just put the SAME ID MULTIPLE TIMES in the value of related_products, as many times as the quantity you need, then that will achieve the same effect as if there was an explicit qty field for each related product. So taking himansu's answer above and adapting it we get:
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_1,PRODUCT_ID_1,PRODUCT_ID_2,PRODUCT_ID_2
This will add to the cart PRODUCT_QUANTITY of PRODUCT_ID, 3 of PRODUCT_ID_1, and 2 of PRODUCT_ID_2.
So as long as you're happy doing a little work to generate the same ID multiple times this works a treat. And no custom code changes required on your magento server.

Resources