Retrieve default product image without product - Magento - magento

I would like to retrieve the default product thumbnail in Magento to apply it to something else other than a product, so I don't have access to $product. Would it be possible?
Thanks,
Krt_Malta

Untested but this should work:
/**
* Get the resource model
*/
$resource = Mage::getSingleton('core/resource');
/**
* Retrieve the read connection
*/
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT thumbnail FROM ' . $resource->getTableName('catalog/product_flat') . ' WHERE sku = "(insert your product SKU here)"'; // Insert SKU here
/**
* Execute the query and store the results in $results
*/
$results = $readConnection->fetchAll($query);
/**
* Print out the results
*/
echo sprintf('<pre>%s</pre>' print_r($results, true));

Related

Need to convert to magento MVC structure

I have developed a code that contains a aquery which returns ratings of a product in json format. The code is as follows:
<?php header('content-type: application/json; charset=utf-8');
require_once('/opt/phpapps/magento/app/Mage.php');
umask(0);
Mage::app();
$cid=$_GET['catid'];
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "SELECT round(t1.rating_summary / 20) AS rating, t2.product_id FROM review_entity_summary AS t1 INNER JOIN catalog_category_product AS t2 ON t1.entity_pk_value = t2.product_id WHERE category_id =" . $cid . " AND store_id = '1'";
$results = $read->fetchAll($query);
$json = json_encode($results);
print_r( $json );
?>
I am instructed to convert this into MVC pattern. I knew that MVC can be done by creating separate folders like blocks, controllers, models,sql,etc, helpers folders. But I am not sure what is the next stepa nd how to execute the developed to get the json data..
Help me in this...
The best way to is create a custom Extension/Model, there's a lot of tutorials out there to do this, however you could use something to generate an example for you to get started:
http://www.silksoftware.com/magento-module-creator/
However, for something this simple you could just create a custom block in the local namespace, for example:
app/code/local/Mage/Catalog/Block/Product/Ratingsjson.php
<?php
/**
* Ratingsjson.php
*/
class Mage_Catalog_Block_Product_Ratingsjson extends Mage_Catalog_Block_Product_Abstract
{
/**
* Get products with special offer attribute set
*
* #return type
*/
public function getRatings()
{
/**
* This will be injected from the tag / XML below
* you can pass what ever variables you want this way.
* getSomeAttribute() will get the value 'some_attribute' from the
* CMS tag or XML config.
*/
$categoryId = $this->getCategoryId();
if($categoryId == NULL) {
$categoryId = 1; // or some default;
}
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
// Do your stuff here...
$query = "SELECT round(t1.rating_summary / 20) AS rating, t2.product_id FROM review_entity_summary AS t1 INNER JOIN catalog_category_product AS t2 ON t1.entity_pk_value = t2.product_id WHERE category_id =" . $cid . " AND store_id = '1'";
$results = $read->fetchAll($query);
return json_encode($results);
}
}
Create a template to do what you want:
template/mymodeule/mytemplate.phtml
<?php
echo $this->getRatings();
You can then use your new block inside CMS pages:
{{block type="catalog/ratignsjson" category_id="3" temaplte="mymodeule/mytemplate.phtml"}}
Or if you want to load it via XML config:
<block type="catalog/ratignsjson" category_id="3" name="ratignsjson" template="mymodeule/mytemplate.phtml"/>
To do this properly and output strict Json data you would want to set json content type headers etc but I think that's a little too much for this particular case.

magento - get attribute option label by store view

Like the title, how can I get the attribute option value of a specific store view instead of the current store view ?
What I am trying to implement is something like this :
public function getAttributeOptionValue($attributeCode, $attributeOptionLabel, $storeView)
The attributeOptionLabel is the current attribute option label.
Simple! Ref Mage_Eav_Model_Entity_Attribute::getStoreLabel() [link]:
<?php
include 'app/Mage.php';
Mage::app('default');
//Generic access
$eavConfig = Mage::getSingleton('eav/config');
/* #var $eavConfig Mage_Eav_Model_Config */
$eavAttribute = $eavConfig->getAttribute(
Mage_Catalog_Model_Product::ENTITY, // 'catalog_product'; see db.eav_entity_type
'description' // attribute code
);
/* #var $eavAttribute Mage_Eav_Model_Entity_Attribute */
Zend_Debug::dump(
$eavAttribute->getStoreLabel('admin')
);
//For a given EAV entity-based model, the resource requires only the attribute
$product = Mage::getModel('catalog/product')->load(2);
/* #var $product Mage_Catalog_Model_Product */
$productAttribute = $product->getResource()->getAttribute('description');
/* #var $productAttribute Mage_Eav_Model_Entity_Attribute */
Zend_Debug::dump(
$productAttribute->getStoreLabel('admin')
);
//It's also possible to get all labels:
Zend_Debug::dump(
$productAttribute->getStoreLabels()
);

Using event sales_order_place_after doesn't return custom product attributes in magento

I try to get the products which have been bought.
This is my code:
/** #var $order Mage_Sales_Model_Order */
$order = $eventObserver->getOrder();
/** #var $items Mage_Sales_Model_Resource_Order_Collection */
$items = $order->getItemsCollection(array(), TRUE);
/** #var $item Mage_Sales_Model_Order_Item */
foreach($items as $item) {
$product = $item->getProduct();
var_dump($product->getData('language'));
}
Language is a custom attribute. In this case it is empty, and I have no idea why. All default attributes, like name, id or sku are working.
This language is used as configurable attribute.
What have I to do, to get the value?
Try this:
$items = $order->getAllVisibleItems()
Got it!
When I take the product ID and load the product again:
$product = $item->getProduct();
$product = Mage::getModel('catalog/product')->load( $product->getId() );
I get the language as number.

How to print collection mysql query in magento

Let's say I have a collection like:
$products = Mage::getModel('catalog/product')
->getCollection()
...
->load();
How do I print the actual MySQL code that gets executed?
You can always view your sql query at a certain point by echoing getSelect as shown:
$products = Mage::getModel('catalog/product')
->getCollection();
echo $products->getSelect();
To change query parameters you want to check out methods like:
$products->addAttributeToSelect('someattribute');
$products->addAttributeToFilter('someattribute', array('eq'=>'1'));
You can print collection using below code:
We can print query of collection using getSelect()->__toString()
$products = Mage::getModel(‘catalog/product’)
->addAttributeToFilter(‘status’, array(‘eq’ => 1));
echo $products->getSelect()->__toString();
Have you seen http://kuldipchudasama.wordpress.com/2012/07/16/magento-print-query-of-collection/?
This works well.
Most other answers here say that $products->getSelect() will do it - this is fine if all you're going to do with it is echo, but in fact getSelect() doesn't just return a string, it returns a Varien_Db_Select object.
Invoking echo on that object automatically triggers its __toString() method, so you just get the SQL string, but try passing it to Mage::log() and you'll get a lot more than you expected.
If you just want to log the SQL, you can use:
Mage::log($products->getSelect()->__toString());
Or how about using the object's own:
$products->printLogQuery(false, true); // don't echo, do log
printLogQuery is defined in lib/Varien/Data/Collection/Db.php.
You can print
$products->getSelect()->assemble();
I work with collections every day. This is without a doubt the correct way.
echo $collection->getSelectSql(true);
If you simple set the first parameter of ->load() to true, like so:
$products = Mage::getModel('catalog/product')
->getCollection()
...
->load(true);
In Magento 2:-
namespace <Company>\<Module>\Block\Adminhtml\Tab\Log;
class Grid
extends \Magento\Backend\Block\Widget\Grid\Extended
{
protected $_collectionFactory;
/**
* Constructor
*
* #param \Magento\Backend\Block\Template\Context $context
* #param \Magento\Backend\Helper\Data $backendHelper
* #param \<Company>\<Module>\Model\ResourceModel\Log\CollectionFactory $collectionFactory
* #param Psr\Log\LoggerInterface $logger
* #param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\<Company>\<Module>\Model\ResourceModel\Log\CollectionFactory $collectionFactory,
\Psr\Log\LoggerInterface $logger,
array $data = []
) {
$this->_logger = $logger;
$this->_collectionFactory = $collectionFactory;
parent::__construct($context, $backendHelper, $data);
}
/**
* {#inheritdoc}
*/
protected function _prepareCollection()
{
$collection = $this->_collectionFactory->create();
$this->_logger->info($collection->getSelect()->__toString());
$this->setCollection($collection);
return parent::_prepareCollection();
}
}
And remember that the collection factory is a magic class that can attaches to every class as Magento 1 wasn't complicated enough.
Step 1-
$result_colletion = print_r($collection->getSelect());
Mage::log($$result_colletion, null, custom_collection.log,true);
Step 2-
After that Login into magento admin section and enable to log setting . Please see below .
System > Configuration > Developer > Log Settings
Step 3-
After that see the log file custom_collection.log in var/log/ folder .
Try following code.
$products = Mage::getModel('catalog/product')
->getCollection();
echo $products->getSelect();

In magento - same code for getting all categories running well with sorted in localhost but not in web server

I have got all categories sorted as in admin in localhost but in web server its coming without sorted. I tried same code and order in both localhost and web server.
That is really a pain.Please help me!
Here is my code:
$categories = Mage::helper('catalog/category');
$collection = $categories->getStoreCategories(false,true,false);
foreach($collection as $_category)
{
//Do something
echo $_category->getName();
}
In helper:
/**
* Retrieve current store categories
*
* #param boolean|string $sorted
* #param boolean $asCollection
* #return Varien_Data_Tree_Node_Collection|Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection|array
*/
public function getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)
And your first parameter equals to false, means collection won't be sorted.
$collection = $categories->getStoreCategories(false,true,false);

Resources