Limit number of images per product - Magento - image

Does anyone know how to Limit the number of images per product that can be uploaded to - Magento? Say I only want to allow 6 images per product.
Thanks,

public function saveAction()
{
/* START: Handle the max images per product */
$data = $this->getRequest()->getPost();
$images = Mage::helper('core')->jsonDecode($data['product']['media_gallery']['images']);
if($totalImages = count($images)) {
$maxPhotoPerProduct = 5;
if($totalImages > $maxPhotoPerProduct) {
Mage::getSingleton('core/session')->addError($this->__('Max. number of images per product reached, extra images have been removed'));
}
$_allowedImages = array_slice($images, 0, $maxPhotoPerProduct);
//Return the allowed images back to the $_POST
$_POST['product']['media_gallery']['images'] = Mage::helper('core')->jsonEncode($_allowedImages);
}
/* END: Handle the max images per product */
//Zend_Debug::dump(Mage::helper('core')->jsonDecode($_POST['product']['media_gallery']['images'])); exit;
//Run standard saveAction()
parent::saveAction();
}
Note that code above is from my Mycompany_Mymodule_Adminhtml_Catalog_ProductController class located in the “app/code/local/Mycompany/Mymodule/controllers/Adminhtml/Catalog/ProductController.php” file.
Meaning I overwrote the default “app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php” controller by using the syntax shown below in Mymodule config.xml file.
</config>
...
<admin>
<routers>
<adminhtml>
<args>
<modules>
<sintax before="Mage_Adminhtml">Mycompany_Mymodule_Adminhtml</sintax>
</modules>
</args>
</adminhtml>
</routers>
</admin>
...
</config>

Related

Magento Mass Action, grid does not update

ok, so this isn't my first mass action but, this does have me scratching my head.
I made a module CLR_exportMassAction. Deployed it on the good old localhost with no problems. However when I move the files out to my server, nothing happens. I have reindexed and flushed caches. I have a feeling its some configuration weirdness and magento isn't hooking in my module.
Here is my code:
\local\CLR\exportMassAction\etc\config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<models>
<CLR_exportMassAction>
<class>CLR_exportMassAction_Model</class>
</CLR_exportMassAction>
</models>
</global>
<adminhtml>
<events>
<adminhtml_block_html_before>
<observers>
<CLR_exportMassAction>
<type>singleton</type>
<class>CLR_exportMassAction/observer</class>
<method>addExportMassactionToProductGrid</method>
</CLR_exportMassAction>
</observers>
</adminhtml_block_html_before>
</events>
</adminhtml>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<CLR_exportMassAction before="Mage_Adminhtml">CLR_exportMassAction_Adminhtml</CLR_exportMassAction>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
local\CLR\exportMassAction\Model\Observer.php
<?php
class CLR_exportMassAction_Model_Observer
{
public function addExportMassactionToProductGrid($observer)
{
$block = $observer->getBlock();
if($block instanceof Mage_adminHtml_Block_Catalog_Product_Grid) {
$block ->getMassactionBlock()->addItem('export', array(
'label' => Mage::helper('catalog')->__('Export to CSV'),
'url' => $block->getUrl('*/*/massExport', array('_current'=>true)),
));
}
}
}
local\CLR\exportMassAction\controllers\Adminhtml\Catalog\ProductController.php
<?php
class CLR_exportMassAction_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Controller_Action
{
public function massExportAction()
{
$productIds = $this->getRequest()->getParam('product');
if (!is_array($productIds)) {
$this->_getSession()->addError($this->__('Please select product(s).'));
$this->_redirect('*/*/index');
}
else {
//write headers to the csv file
$content = "id,name,url,sku\n";
try {
foreach ($productIds as $productId) {
$product = Mage::getSingleton('catalog/product')->load($productId);
$content .= "\"{$product->getId()}\",\"{$product->getName()}\",\"{$product->getProductUrl()}\",\"{$product->getSku()}\"\n";
}
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
$this->_redirect('*/*/index');
}
$this->_prepareDownloadResponse('export.csv', $content, 'text/csv');
}
}
}
app\etc\CLR_exportMassAction.xml
<config>
<modules>
<CLR_exportMassAction>
<active>true</active>
<codePool>community</codePool>
</CLR_exportMassAction>
</modules>
</config>
I am just looking for a pointer really on where to go from here; I am not sure what the next troubleshooting options are.
Most likely, your local setup is running on a case-insensitive file system (are you on Windows?), whereas your server is running on a case-sensitive file system (probably Linux).
Magento does a lot of string manipulation to convert class names into file names and such. For example, see Varien_Autoload::autoload(), which I presume is causing your problem:
$classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
If you plug in your observer class (CLR_exportMassAction_Model_Observer, after resolving CLR_exportMassAction/observer), you get:
str_replace('_', ' ', 'CLR_exportMassAction_Model_Observer')
= 'CLR exportMassAction Model Observer'
ucwords('CLR exportMassAction Model Observer')
= 'CLR ExportMassAction Model Observer'
str_replace(' ', DIRECTORY_SEPARATOR, 'CLR ExportMassAction Model Observer')
= 'CLR/ExportMassAction/Model/Observer'
So, Magento is looking for a file called CLR/ExportMassAction/Model/Observer.php, but your file is called CLR/exportMassAction/Model/Observer.php - see the lower-case e?
There might be some other casing issues as well, but this is the most prominent one.
I recommend renaming your module namespace from CLR to Clr, and your actual module from exportMassAction to Exportmassaction, and making sure that all of your class names and file names match precisely. This is the easiest way to avoid casing issues with Magento (even if you have multiple words in the same identifier).

Minimum total of items in shopping cart to allow checkout

I'm trying to find a configuration that allows one of my customer groups (wholesale) to add any given number of items to their shopping carts, but have a restricted checkout equal or greater than 16 items in total.
For example:
8 items from product A
2 items from product B
6 items from product C
That would be a total of 16 items and it would be possible for them to checkout.
I tried configuring the Minimum Qty Allowed in Shopping Cart, but then, they have to get 16 items of each product.
Do you know if there is a way to configure, add an extension or hardcode something to solve this problem?
Thank you for your time!
Easiest way is to set a minimum order amount
/admin/system_config/edit/section/sales
To implement a minimum item restriction, you can place an observer to fire on one page checkout which checks the item quantity it cart and redirects you back to the cart if its below your threshold with a message.
Full prototype, flavor as needed:
app\etc\modules\Spirit_Cms.xml
<?xml version="1.0"?>
<config>
<modules>
<Spirit_Cms>
<active>true</active>
<codePool>local</codePool>
</Spirit_Cms>
</modules>
</config>
app\code\local\Spirit\Cms\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<Spirit_Cms>
<version>0.0.1</version>
</Spirit_Cms>
</modules>
<frontend>
<events>
<controller_action_predispatch_checkout_onepage_index>
<observers>
<spirit_cms_restrict_checkout>
<class>Spirit_Cms_Model_Observer</class>
<method>restrictCheckout</method>
</spirit_cms_restrict_checkout>
</observers>
</controller_action_predispatch_checkout_onepage_index>
</events>
</frontend>
<global>
<models>
<spirit_cms>
<class>Spirit_Cms_Model</class>
</spirit_cms>
</models>
</global>
</config>
app\code\local\Spirit\Cms\Model\Observer.php
<?php
class Spirit_Cms_Model_Observer
{
public function restrictCheckout( $oObserver )
{
// Ensure we only observe once.
if( Mage::registry( 'restrict_checkout_flag' ) )
{
return $this;
}
else
{
$oQuote = Mage::getSingleton( 'checkout/cart' )->getQuote();
$oCartItems = $oQuote->getAllItems();
$iTotalQty = 0;
foreach( $oCartItems as $oCartItem )
{
$iTotalQty = $iTotalQty + $oCartItem->getQty();
}
if( $iTotalQty < 12 )
{
$oSession = Mage::getSingleton( 'checkout/session' );
$oSession->addError( 'Please add at least 12 items to your cart.' );
Mage::app()->getResponse()->setRedirect( Mage::getUrl( 'checkout/cart' ) );
}
Mage::register( 'restrict_checkout_flag', 1, TRUE );
}
}
}
?>

Magento Admin Sales > Order > Item Url

I am trying to create a link from my helpdesk software to the sales order page in the backend of magento.
Magento constructs the url as the example below where the number represents the Order ID.
/index.php/admin/sales_order/view/order_id/12394/
However, the Order Id does not equal the Order Number because credit invoices etc. are included in the count.
Is there an other way for me to link to the order page using the Order Number.
Thanks!
There are 2 types of order number in magento
Order increment id
Order id (mostly use internal in magento).
The admin uses the order id while the number on you invoice is the order increment id.
The quickest way to get around this is to create a custom module that look up order id by order increment id and the redirect to the view page using the order id.
In /app/etc/modules/MageIgniter_OrderRedirect.xml
<?xml version="1.0"?>
<config>
<modules>
<MageIgniter_OrderRedirect>
<active>true</active>
<codePool>local</codePool>
</MageIgniter_OrderRedirect>
</modules>
</config>
In /app/code/local/MageIgniter/OrderRedirect/controllers/RedirectOrderController.php
<?php
class MageIgniter_OrderRedirect_RedirectOrderController extends Mage_Core_Controller_Front_Action
{
public function viewAction(){
$increment_id = Mage::app()->getRequest()->getParam('id');
$order = Mage::getModel('sales/order')->loadByIncrementId($increment_id);
$order_id = $order->getId();
Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl("adminhtml/sales_order/view", array('order_id'=> $order_id)));
}
}
in In /app/code/local/MageIgniter/OrderRedirect/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MageIgniter_OrderRedirect>
<version>0.1.0</version>
</MageIgniter_OrderRedirect>
</modules>
<frontend>
<routers>
<orderredirect>
<use>standard</use>
<args>
<module>MageIgniter_OrderRedirect</module>
<frontName>orderredirect</frontName>
</args>
</orderredirect>
</routers>
</frontend>
<global>
<helpers>
<orderredirect>
<class>MageIgniter_OrderRedirect_Helper</class>
</orderredirect>
</helpers>
</global>
</config>
url
www.site.com/orderredirect/redirectOrder/view/id/101512486

Redirect https to http on non secure magento pages

Magento has some great built in functionality that if you request a page that should be secure e.g. the checkout via http then it redirects to https. However what seems to be lacking is if somebody requests a page that doesn't need to be secure e.g. a category page via https then there does not seem to be the functionality to redirect them to http.
So if somebody requests:
https://www.mysite.com/mycategory
they get 301 redirected to
http://www.mysite.com/mycategory
Anybody managed to achieve this?
If not then anybody able to point me in the direction of the bit of the magento core code that does the redirect to HTTPS and then I should be able to work from that to come up with a solution.
Take a look at the following method
app/code/core/Mage/Core/Controller/Varien/Router/Standard.php::_checkShouldBeSecure()
So thanks to Rich i tracked this down in app/code/core/Mage/Controller/Varien/Router/Standard.php::_checkShouldBeSecure()
I amended this function (created copy in app/code/local add amended it there) adding in the elseif part
protected function _checkShouldBeSecure($request, $path='')
{
if (!Mage::isInstalled() || $request->getPost()) {
return;
}
if ($this->_shouldBeSecure($path) && !Mage::app()->getStore()->isCurrentlySecure()) {
$url = $this->_getCurrentSecureUrl($request);
Mage::app()->getFrontController()->getResponse()
->setRedirect($url)
->sendResponse();
exit;
} elseif (!$this->_shouldBeSecure($path) && Mage::app()->getStore()->isCurrentlySecure()) {
$url = $this->_getCurrentUnsecureUrl($request);
Mage::app()->getFrontController()->getResponse()
->setRedirect($url)
->sendResponse();
exit;
}
}
then added this function
protected function _getCurrentUnsecureUrl($request)
{
if ($alias = $request->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS)) {
return Mage::getBaseUrl('link', false).ltrim($alias, '/');
}
return Mage::getBaseUrl('link', false).ltrim($request->getPathInfo(), '/');
}
Thanks so much for the hint above. I integrated the code in a "Magento-like-module" without overloading the Core.
There are only three files to create:
app/code/local/MyCompany/MyModule/Controller/Varien/Router/Standard.php
app/code/local/MyCompany/MyModule/etc/config.xml
app/etc/modules/MyCompany_MyModule.xml
In app/etc/modules/MyCompany_MyModule.xml paste the following uninteresting stuff:
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyCompany_MyModule>
</modules>
</config>
In app/code/local/MyCompany/MyModule/etc/config.xml paste the Router-Configuration:
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_MyModule>
<version>0.1.0</version>
</MyCompany_MyModule>
</modules>
<default>
<web>
<routers>
<standard>
<area>frontend</area>
<class>MyCompany_MyModule_Controller_Varien_Router_Standard</class>
</standard>
</routers>
</web>
</default>
</config>
And in app/code/local/MyCompany/MyModule/Controller/Varien/Router/Standard.php implement the logic from above:
<?php
class MyCompany_MyModule_Controller_Varien_Router_Standard extends Mage_Core_Controller_Varien_Router_Standard {
protected function _checkShouldBeSecure($request, $path='') {
parent::_checkShouldBeSecure($request, $path);
if (!$this->_shouldBeSecure($path) && Mage::app()->getStore()->isCurrentlySecure()) {
$url = $this->_getCurrentUnsecureUrl($request);
Mage::app()->getFrontController()->getResponse()
->setRedirect($url)
->sendResponse();
exit;
}
}
protected function _getCurrentUnsecureUrl($request) {
if ($alias = $request->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS)) {
return Mage::getBaseUrl('link', false).ltrim($alias, '/');
}
return Mage::getBaseUrl('link', false).ltrim($request->getPathInfo(), '/');
}
}
Unfortunately there was no event to observe which fixed the problem... But overriding the Router is better (for updates) than (technically) replacing.
For additional knowledge please go through following
Check how to resolve issue of custom module frontend url is forced to https. instead of http. If you have used module creator to create module. Than you will definetly find below code in config.xml
<admin>
<routers>
<[ModuleName]>
<use>admin</use>
<args>
<module>[NameSpace_ModuleName]</module>
<frontName>[frontName]</frontName>
</args>
</[ModuleName]>
</routers>
</admin>
To resolve issue of redirection to https instead of http, you need to comment above code from module config.xml file Path: app/code/(codepool)/(NameSpace)/(ModuleName)/etc/config.xml OR you can add different “frontName” for and routers. Eg.:
<admin>
<routers>
<[ModuleName]>
<use>admin</use>
<args>
<module>[NameSpace_ModuleName]</module>
<frontName>[frontName]</frontName>
</args>
</[ModuleName]>
</routers>
</admin>
<frontend>
<routers>
<[ModuleName]>
<use>standard</use>
<args>
<module>[NameSpace_ModuleName]</module>
<frontName>[frontName1]</frontName>
</args>
</[ModuleName]>
</routers>
</frontend>
As per above code admin url will be http://yourdomain.com/index.php/frontName/adminhtml_moduleName/ and frontend url will be like: http://yourdomain.com/index.php/frontName1
I hope above content is useful to you.
The code above strips the query string from the request, both functions should be changed to add it to the request before the redirect.
See below:
$querystring = $_SERVER['QUERY_STRING'];
return Mage::getBaseUrl('link', false).ltrim($request->getPathInfo(), '/').($querystring ? '?'.$querystring : '');

How to use getUrl() in Magento to refer to another module?

My module in Magento adminpanel has URL like as http://example.com/index.php/mymodule/... and contains custom grid with the orders. I want to redirect user to the standard "Order view" page when he clicks on a grid row.
public function getRowUrl($row)
{
if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
return $this->getUrl('sales_order/view', array('order_id' => $row->getId()));
}
return false;
}
But this URL points to http://example.com/index.php/sales_order/view/... instead of http://example.com/index.php/admin/sales_order/view/... Any suggestion?
UPD. config.xml:
<admin>
<routers>
<mymodule>
<use>admin</use>
<args>
<module>Foo_Mymodule</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
</admin>
Quite simply you need to replace sales_order/view with */sales_order/view. The * means use the current router which in the admin is adminhtml.
Edit
To explain in more detail put this in your config,
<admin>
<routers>
<adminhtml>
<args>
<modules>
<mymodule after="Mage_Adminhtml">Foo_Mymodule_Adminhtml</mymodule>
</modules>
</args>
</adminhtml>
</routers>
</admin>
Now the value */mymodule/index will generate an URL http://example.com/index.php/admin/mymodule/index which in turn will load the file Foo/Mymodule/controllers/Adminhtml/MymoduleController.php and try to find the method Foo_Mymodule_Adminhtml_MymoduleController::indexAction(). If the method exists it is run, otherwise the admin router takes over and shows a 404 or redirects to the dashboard.

Resources