I have an observer (checkout_cart_product_add_after) and would like to get the selected custom options array.
I have found the two elements I require in: app/design/frontend/MY-THEME/default/template/checkout/cart/item/default.phtml
Which are:
1. $_item = $this->getItem();
2. $_options = $this->getOptionList();
I just don't know how to get them in an observer i.e. what else I have to call.
Thanks in advance!!
please go to the following url
http://inchoo.net/magento/updating-options-of-configurable-product-that-is-already-in-the-cart/
thanks
Observers always start returning data from their respective "events".
So in your case you have to first get checkout-quote object in your observer's function code using below line:
$quote = $observer->getEvent()->getQuote();
Then you can get custom options for each item in items collection from the quote like below:
$quoteItems = $quote->getAllItems();
$helper = Mage::helper('catalog/product_configuration');
foreach ($quoteItems as $item) {
$product = $item->getProduct();
$options = $helper->getCustomOptions($item);
//do anything with $options.
}
Try this. hope it helps.
Also have you already referred below links ?
https://magento.stackexchange.com/questions/16804/get-the-object-of-the-whole-quotation-in-observer
https://magento.stackexchange.com/questions/63752/get-products-final-price-with-its-selected-custom-option-on-add-to-cart
https://magento.stackexchange.com/questions/6368/how-to-get-selected-custom-options-on-onepage
Related
I am looking for a solution to add all the nodes of content type article to a group with group id=25, Is there any way to add them once through PHP code, I heard about the function addcontent in groups , but I don't know how to use it, please help me out
Group::addContent(ContentEntityInterface $entity, $plugin_id, $values = [])
How to use the above function?
Is there any way to do one step process?
Group::addContent(ContentEntityInterface $entity, $plugin_id, $values = [])
I need to all the content type of articles to group with gid=25
let me try to explain more
use Drupal\node\Entity\Node;
use Drupal\group\Entity\Group;
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function yourmodulename_node_insert(Node $node) {
if ($node->bundle() == 'article') {
$pluginId = 'group_node:article';
// Here create some logic to load/chose the group id's you want to
// add the content to
// ...
foreach ($group_ids as $gid) {
$group = Group::load($gid);
$group->addContent($node, $pluginId);
}
}
}
I hope it will be more clear, foreach means you can add to multiple groups so here you can try your own logic that to which group do you want to add, hope it will make sense.
May be it will help
$group->addContent($entity, 'group_node:article');
plugin_id is actually node type
please try it.
We are using Magento 1.
We have used this code:
Mage::getResourceSingleton('catalog/category_tree')->load();
$tree = Mage::getResourceSingleton('catalog/category_tree')->load();
$root = $tree->getNodeById($rootCategoryId);
if($root && $root->getId() == 1) {
$root->setName(Mage::helper('catalog')->__('Root'));
}
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToFilter('is_active','1');
//->addAttributeToFilter('display_mode',array('nlike'=>'PAGE'))
//->setLoadProductCount(true)
//->setProductStoreId($store)
//->addAttributeToFilter('include_in_menu','1');
$tree->addCollectionData($collection, true);
We have created a api, and calling this in widget categories as well as category listing, but its only loading for last one only, not resulting for all calls.
kindly suggest a proper way
that is the way a singleton works, use model instead.
Mage::getResourceModel('catalog/category_tree')
For a backend module I have the need to check if a product id is valid, that is: is there a product with that id? I've found two solutions for that, but I'm not very happy with either one:
Directy query the catalog_product_entity table. Very fast but definitely not very elegant and I fear there might be issues I'm not aware of.
Use the following code:
$product = Mage::getModel('catalog/product')->load($productID)
if ($product->getId()) {
//valid id
}
else {
//not a valid id
}
This should work but it's painfully slow, because I have to check several IDs at once. And since I don't need the actual product data, it doesn't really make sense to load it.
Any better suggestions?
$productIds = array(16,17,18,19,290993,25 /*...*/);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('entity_id',array('in'=>$productIds))->load();
$foundIds = array_intersect($productIds,array_keys($collection->toArray()));
var_dump($foundIds); /* each array value should be a valid ID */
Thanks benmarks and james, you've pointed me in the right direction and I just combined your approches:
$productIds = array(16,17,18,19,290993,25 /*...*/);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('entity_id',array('in'=>$productIds));
$foundIds = $collection->getAllIds();
var_dump($foundIds); /* each array value should be a valid ID */
For some reason, getAllIds() returns Ids as strings rather than ints, but that's fine.
Try this:
$collection->getAllIds();
You'll get an array of all the product IDs, and you can then do an in_array to check for validity. Take a look at
Mage_Catalog_Model_Resource_Product_Collection
to see how it's done.
EDIT
Example:
function isProductIdValid($productId)
{
// Allows model overrides (other modules) to work correctly.
// Returns all valid IDs
$collection = Mage::getModel('catalog/product')->getCollection();
$productIds = $collection->getAllIds();
if (in_array($productId, $productIds)) {
return true;
} else {
return false;
}
}
For having it fast, raw queries seem definitely the fastest to me always :-)
But maybe preload all id's like this could help?
$collection = Mage::getModel('catalog/product')->getCollection()->getSelect()->reset(Zend_Db_Select::COLUMNS)->columns(array('entity_id'));
$ids = array();
foreach ($collection as $product) {
$ids[] = $product->getId();
}
Then you can just test with the in_array method if an id exists.
I've been grabbing attribute options from Magento like so:
<?php
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
?>
It's been working fine until I tried to get the options for the built in 'color' attribute -- I got the following error:
PHP Fatal error: Call to a member function setAttribute() on a non-object in app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php on line 374
It would appear that the getSource() call fails and causes this error. Does anyone know why this happens and how I can get color options out?
Thanks!
It looks like that you initialize attribute by yourself, instead of using Magento attribute initialization process:
Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode)
Because since 1.4.x Magento has separate attribute models for catalog and customers model and definition of default source model for catalog_product now is moved from EAV attribute model (Mage_Eav_Model_Entity_Attribute) to the catalog one (Mage_Catalog_Model_Resource_Eav_Attribute).
As a result, some catalog attributes won't work with the EAV attribute model. Particularly those that use Mage_Eav_Model_Entity_Attribute_Source_Table but don't explicitly define it (color, manufacturer, etc.).
The following code snippet should work perfectly on your installation:
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
By the way Mage_Eav_Model_Config model has a lot of helpful methods, that can be used in your development, so don't hesitate to look into this model.
The above code does not work if the resource_model is empty. The following snippet does the job:
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'YOUR_ATTRIBUTE_CODE');
/** #var $attribute Mage_Eav_Model_Entity_Attribute */
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getId())
->setStoreFilter(0, false);
$attribute = Mage::getModel('eav/config')->getAttribute('customer','cateinterest');
$options = $attribute->getSource()->getAllOptions();
<?php
//Possible color value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
$id = $instance['value']; //id of the option
$value = $instance['label']; //Label of the option
Sorry for an incomplete answer, but take a look at the database, specifically in the backend_model column. I seem to remember having this same problem until I set this field to match some of the system fields in this respect.
In Magento, if you need to get / fetch the Shopping Cart's Item details, you can do it in any of the two possible ways, which will provide you with all the shopped Items in an array:-
$cartItems1 = $cart->getQuote()->getAllItems();
$cartItems2 = $cart->getItems()->getData();
But before using any one of the above two methods, you need to initialize the shopping cart object as:-
$cart = new Mage_Checkout_Model_Cart();
$cart->init();
Can anyone please describe in details as to what the two options provide & their differences between each other, along with their possible usage.
In any more such option is available in Magento, can anyone please highlight it?
If you look at the code of the Cart and Quote classes everything will become clear.
Here's the code for $cart->getItems():
public function getItems()
{
return $this->getQuote()->getAllVisibleItems();
}
Plain and simple - it just calls a method of the Quote object. So the question now is: What is the difference between getAllVisibleItems() and getAllItems()?
Let's look at the code of both methods:
public function getAllItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted()) {
$items[] = $item;
}
}
return $items;
}
public function getAllVisibleItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted() && !$item->getParentItemId()) {
$items[] = $item;
}
}
return $items;
}
The only difference: getAllVisibleItems() has an additional check for each item:
!$item->getParentItemId()
which tests if the product has a parent (in other words, it tests if it is a simple product). So this method's return array will be missing simple products, as opposed to getAllItems().
Are there any other ways to retrieve items?
One would be to directly get the product collection from the quote object:
$productCollection = $cart->getQuote()->getItemsCollection();