Magento how to get attribute value by option id? - php

So I have
$attribute_option_id = Mage::getResourceModel('catalog/product')
->getAttributeRawValue($productId, 'my_attribute', $storeId);
This gives me something like $attribute_option_id = 12345
Now how do I get the (text) value for this id?
Thanks

This should work.
$product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);
$text = $product->getAttributeText('my_attribute');
[EDIT]
If you don't want to load the full product you can do a sneaky think. Impersonate a product.
So you get the option id like you already do
$attribute_option_id = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'my_attribute', $storeId);
Then create just an empty instance of the product and set some attributes to it.
$product = Mage::getModel('catalog/product')
->setStoreId($storeId)
->setData('my_attribute', $attribute_option_id);//the result from above
$text = $product->getAttributeText('my_attribute');
There is already a confirmation that this works. See it here.

Here is a method that is a different approach that works for me.
Assume Known: $_current_productid
$_Current_OptionId = Mage::getResourceModel('catalog/product')->getAttributeRawValue($_current_productid, $attribute_id,$store_id);
$_write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $_write is an instance of Zend_Db_Adapter_Abstract
$_readresult=$_write->query("SELECT value FROM eav_attribute_option_value WHERE option_id ='".$_Current_OptionId."'");
while ($row = $_readresult->fetch() ) {$_Current_OptionValue=$row['value'];}

Related

Magento: Get Website ID from Website Name

As the title says. I don't want the ID of the current website, I want the ID of any website by supplying only that websites name.
I have this code:
$siteModel = Mage::getResourceModel( 'core/website_collection' )->addFieldToFilter( 'name', $site )->getFirstItem();
$siteId = $siteModel->getId();
But $siteId ends up empty.
If all I have is the name of a website, how do I get its IDs?
Many thanks.
You need debug the collection by ->getSelect()->__toString()
for checking it give right value or not.
$siteModelCollection = Mage::getResourceModel( 'core/website_collection' )->addFieldToFilter( 'name', $site );
// print the Query
echo $Query = $siteModelCollection ->getSelect()->__toString();
echo $siteId =$siteModelCollection->getFirstItem()->getId();
Update:
Use load():
for this case you need to call load() function of resource model of core/website for getting proper collection
Now you can get id of first store by using below code:
$siteModelCollection = Mage::getResourceModel('core/website_collection')->load()->addFieldToFilter( 'name', $site);
echo $Query = $siteModelCollection ->getSelect()->__toString();
echo $siteId =$siteModelCollection->getFirstItem()->getId();
You use addFieldToFilter method incorrectly.
Try this approach:
$website = Mage::getModel('core/website')
->getCollection()
->addFieldToFilter('name', array('eq', $siteName))
->getFirstItem();
$websiteId = $website->getId();

Search filter in cakephp with dynamic options

I want to apply search filters in my project. I have options tables where options are being saved with the option values with parent id of option id. For example brand is saving as option with parent id set to 0 and all brands have brand id as their parent id set and while saving product I am saving product options in product_options table. Now i want to apply filters in product listing page. I am using following code for filtration:
$conditions = array();
$product_options = $this->ProductOption->find('list',array('fields'=>array('product_id'),'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])));
$conditions = array_merge($conditions,array('Product.id'=>array_unique($product_options)));
$prod_info = $this->paginate('Product',$conditions);
$this->set(compact('prod_info'));
When I search any product with their brand name it works fine but if I try to search with the price (also an option) then it gives other brand products also which have price equal to filter price option. Please check following link to understand problem correctly.
http://primemart.in/Food-Processors-Ii4zRGAKYAo=
Please anyone help me to come out my problem.
Thanks.
Please have a look on my code which I used to pass conditions in and to get results
$product_options = $this->ProductOption->find('list',array(
'fields'=>array('product_id'),
'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])
));
//$this->Option->unBindModel(array('belongsTo'=>'Product'));
$product_options = $this->Option->find('all', array(
'conditions'=>array('Option.id'=>$data['data']['options'])
));
//pr($product_options);
$opt_arr = array();
foreach ($product_options as $op) {
$opt_arr[$op['Option']['parent_id']][] = $op['Option']['id'];
}
$conditions_arr = array();
foreach($opt_arr as $opt) {
$key_arr = array();
foreach($opt as $op) {
$key_arr['OR']['ProductOption.option_value_id'][] = $op;
}
$conditions_arr['AND'][] = $key_arr;
}
$pr_options = $this->ProductOption->find('list', array(
'conditions'=>$conditions_arr,
'fields'=>array('product_id')
));
$conditions = array_merge($conditions, array('Product.id'=>array_unique($pr_options)));
I would try code bellow. I assume that $conditions constist of the other conditions you mention in your question.
$conditions = ... // other conditions you mentioned
$conditions = array('AND'=>array($conditions, array('Product.id'=>array_unique($product_options))));
$prod_info = $this->paginate('Product',$conditions);

Accessing image field of line items in Drupal 7 module

I am creating custom emails to be sent to customers wanting to share their cart with others. So far, I have each product's quantity, SKU, price, path (node/XYZ), and title. The last item I need in the email is the product's image path.
I found all the other information with the following:
$order = commerce_cart_order_load($user->uid);
foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
$sku = $line_item_wrapper->line_item_label->value();
//...
Printing out the following I was able to see a protected "data" property for the wrapper object:
print_r($line_item_wrapper->commerce_product);
Then, I tried finding the getter method for the field_image property with the following:
print_r($line_item_wrapper->commerce_product->getPropertyInfo('field_image');
I ended up here with entity_metadata_field_verbatim_get() but I don't know what parameters to pass. Also, in the last print statement above I didn't see anything else of value.
I'm wondering if I need to query for this data, and what table / columns to query for? Or maybe use something like node_load()? However, i'm not finding it too easy to find the node ID from the line item wrapper.
I was able to solve the above problem with this code:
$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$result = array();
foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
$product = array();
$product['quantity'] = $line_item_wrapper->quantity->value();
$product['sku'] = $line_item_wrapper->line_item_label->value();
$product['path'] = $line_item_wrapper->commerce_display_path->value();
$product['price'] = $line_item_wrapper->commerce_unit_price->amount->value();
$product['title'] = $line_item_wrapper->commerce_product->title->value();
$product['image'] = $line_item_wrapper->commerce_product->field_image->value();
$product['image'] = $product['image'][0]['filename'];
array_push($result, $product);
}
I have to say, finding all these discrete functions was quite difficult. I did not find any clear documentation about Drupal Commerce metadata_wrappers. If anyone could provide further information I'd love to take a look.
In general, I used a lot of print_r(); to find these values.

get magento order collection with different field values for the same field

Hi I want to get order collection with multiple field values for same field.Already I tried to get the collections with addFieldToFilter for individual field values('status':pening & 'status':processing) and I merged both the collecions to a single collection.Now I'm getting problem like I can't able to access the resulted collection, its troughs error like getMethod can't call on non object.Give me any suggestions.
Here is my code :
$shippedCollection = array();
$processingCollection = array();
$orderSCollection = Mage::getModel('sales/order')->getCollection();
$shippedCollection = $orderSCollection->addFieldToFilter('status','delivered_carrier');
$orderPCollection = Mage::getModel('sales/order')->getCollection();
$processingCollection = $orderPCollection->addFieldToFilter('status','processing');
$order = array_merge($shippedCollection->getAllIds(),$processingCollection->getAllIds());
$order->getData('increment_id');
Hello check below code may be help you
$order_collection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', array('in' => array('delivered_carrier','pending')));
echo count($order_collection->getAllIds());

get Order_Item_Id using magento api

I want to create invoice in to magento store using magento api in php.For that I want to create invoice for particular quantity and item means If anyone wants to invoice one item in paricular quantity then It shoud be done.My code is working for array() or all quantity.
Below is pseudo code for creating invoice
$client = new Zend_XmlRpc_Client('http://127.0.0.1:8080/AndroidMagento/api/xmlrpc')
$session = $client->call('login', array('tester','tester'));
$saleorderno = '100000007';
Mage::init();
$order = Mage::getModel('sales/order')->load($saleorderno);
$orderItems = $order->getAllItems();
$invoiceItems = array();
foreach ($orderItems as $_eachItem) {
$invoiceItems[$_eachItem->getItemId()] = $_eachItem->getQtyOrdered();
}
$result = $client->call('call',array($session,'sales_order_invoice.create',array($saleorderno,array('order_item_id' => 9474, 'qty' => 1),'Invoice Created by Test',false,false)));
I have seen this link where i found somewhat idea but i can't understand exactly.I can't understand how to get value of order_item_id.???
Any idea??? Please suggest me Thanks in advance...
item_id and product_id are different id.
order or quote has item_id and product_id.
You can try this:
$order = Mage::getModel('sales/order')->load($saleorderno);
$orderItems = $order->getAllItems();
foreach ($orderItems as $item){
print_r($item->getData());
print_r($item->getItemId()); //magic function to get ['item_id']
}
You can do it in 'sales/quote' Model.
Cheers ^^
Try this,
echo "<pre>";
$result = $client->call($session, 'sales_order.info', 'orderIncrementId');
print_r($result['item_id']);
print_r($result['product_id']);
and the $result will return all info of order including item_id and product_id,
with $result['item_id'] you can pass it to call
sales_order_invoice.create
then do
$result = $client->call(
$session,
'sales_order_invoice.create',
array('orderIncrementId' => '200000008',
array('order_item_id' => $result['item_id'],
'qty' => $result['total_qty_ordered'])
)
);
and the qty, You have to get it from $result['total_qty_ordered']
First, try to print_r[$result]; then You'll get some hints from it.
^^

Categories