Custom url in Magento - php

I want to create url like {base_url}/customize/{product_slug}.phtml
Could please help how to make this of url Magento?
Currently I have created "CustomizeController.php" file
<?php
class ProductCustomizer_ProductCustomizer_CustomizeController extends Mage_Core_Controller_Front_Action {
public function indexAction(){
echo 'test mamethode';
$product = Mage::getModel("productcustomizer/customizeproduct")->getProduct();
}
public function mamethodeAction(){
echo 'test mamethode';
}
}
and below code in config.xml file
<productcustomizer_customize_index>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="productcustomizer/customize" name="productcustomizer_customize" template="productcustomizer/customize.phtml"/>
</reference>
</productcustomizer_customize_index>

Please assign in $url_key to resolve the issue.
$rewrite = Mage::getModel('core/url_rewrite');
$rewrite->setStoreId($store_id) ->setIdPath('customize/'.$url_key) ->setRequestPath('customize/'.$url_key.'.html') ->setTargetPath('customize/index/action/id/'.$url_key) ->setIsSystem(true) ->save();

I have found solution you can set routing using config.xml file
<global>
<rewrite>
<productcustomizer_url>
<from><![CDATA[#^/customize/#]]></from>
<to><![CDATA[/productcustomizer/customize/index/product/]]></to>
<complete>1</complete>
</productcustomizer_url>
</rewrite>
<global>

Related

Magento 2 - Controller returning blank page

Been spending the last 1 day to find a fix for this.
Basically everything works normal if I delete all the contents of generated folder and then generate the files dynamically by accessing the pages rather than doing setup:di:compile.
On the production though, I'm forced to do setup:di:compile and I end up with this certain admin module page being empty.
This is the controller of the page:
namespace Eadesigndev\AWB\Controller\Adminhtml\Index;
use Eadesigndev\Awb\Api\AwbRepositoryInterface;
use Eadesigndev\Awb\Model\Awb;
use Eadesigndev\Awb\Model\AwbFactory;
use Eadesigndev\Awb\Helper\Data as DataHelper;
use Magento\Framework\Registry;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\Session;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\App\Action;
class Edit extends Action
{
/**
* Authorization level of a basic admin session
*
*
*/
const ADMIN_RESOURCE = 'Eadesigndev_Awb::awb';
protected $resultPageFactory;
private $awbRepository;
private $awbFactory;
private $registry;
private $session;
private $dataHelper;
private $awbModel;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
AwbRepositoryInterface $awbRepository,
AwbFactory $awbFactory,
Awb $awbModel,
Registry $registry,
DataHelper $dataHelper
) {
$this->resultPageFactory = $resultPageFactory;
$this->awbRepository = $awbRepository;
$this->awbFactory = $awbFactory;
$this->registry = $registry;
$this->awbModel = $awbModel;
$this->session = $context->getSession();
$this->dataHelper = $dataHelper;
parent::__construct($context);
}
/**
* Edit action new awb.
*
* #return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$id = $this->getRequest()->getParam('entity_id');
if ($id) {
$model = $this->awbRepository->getById($id);
if (!$model->getEntityId()) {
$this->messageManager->addErrorMessage(__('This field no longer exists.'));
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create();
return $resultRedirect->setPath('*/*/');
}
} else {
echo '3';
$model = $this->awbFactory->create();
}
/** #var Session $data */
$data = $this->session->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
$this->registry->register('awb_data', $model);
echo 'wat';
/** #var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->addBreadcrumb(__('Edit new Awb'), __('Edit new Awb'));
$resultPage->getConfig()->getTitle()->prepend(__('Edit new Awb'));
echo 'pac';
return $resultPage;
}
the controller currently returns a blank page with my debug strings:
3watpac
in /vendor/eadesignro/awb/Block/Adminhtml/Xtea I have the next files:
Edit (folder)
Edit.php
EditAwb.php
in /vendor/eadesignro/awb/view/adminhtml/layout I have the next files:
shipping_awb_index_edit.xml
shipping_awb_index_editawb.xml
shipping_awb_index_index.xml
Content of shipping_awb_index_edit.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="editor"/>
<head></head>
<body>
<referenceContainer name="content">
<block class="Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit" name="awb_edit"/>
</referenceContainer>
<referenceContainer name="left">
<block class="Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit\Tabs" name="awb_edit_tabs">
<block class="Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit\Tab\General" name="awb_edit_tab_general"/>
<action method="addTab">
<argument name="name" xsi:type="string">general_section</argument>
<argument name="block" xsi:type="string">awb_edit_tab_general</argument>
</action>
<block class="Eadesigndev\Awb\Block\Adminhtml\Xtea\Edit\Tab\Curier" name="awb_edit_tab_curier"/>
<action method="addTab">
<argument name="name" xsi:type="string">curier_section</argument>
<argument name="block" xsi:type="string">awb_edit_tab_curier</argument>
</action>
</block>
</referenceContainer>
</body>
</page>
This is the page:
magento_admin_secreturl/shipping_awb/index/edit/key/xxxxxx
these are the routers:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="shipping_awb" frontName="shipping_awb">
<module name="Eadesigndev_Awb" before="Magento_Backend"/>
</route>
<route id="edit_awb" frontName="edit_awb">
<module name="Eadesigndev_Awb" before="Magento_Backend"/>
</route>
</router>
</config>
Any possible idea on why $this->resultPageFactory->create(); is simply returning nothing on production but working on development?
The index page works both on production and dev without any problem.
I can provide more info if needed, thank you!
The problem was a CAPSLOCK mistake in the namespace.
namespace Eadesigndev\AWB\Controller\Adminhtml\Index;
should have been
namespace Eadesigndev\Awb\Controller\Adminhtml\Index;
notice the 'AWB' becoming 'Awb'.
Apparently in development mode, the namespaces are not case-sensitive? or perhaps they are all transformed to capslock. If anyone has a clue, leave a comment, thanks!
For anyone who is still getting a blank page in Admin Controller, but the alternative solutions (case-sensitive) doesn't work for you, you may try running php bin/magento setup:di:compile.
This helped me.

Magento Module Not Working : Adminhtml add tab to Product View

I'm having trouble getting a new module to work. To begin with I just want to add an additional tab to the Product Edit screen in the adminhtml. I want this tab to show underneath the standard "Custom Options" tab. I have my module showing up in the Configuration > Advanced > Advanced > Disable Module output section.
So here's what I have, seen any mistakes?
Path : app\etc\modules\ns>_CustomerHistory.xml
<?xml version="1.0"?>
<config>
<modules>
<<ns>_CustomerHistory>
<active>true</active>
<codePool>local</codePool>
</<ns>_CustomerHistory>
</modules>
</config>
Path: app\code\local\\CustomerHistory\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<<ns>_CustomerHistory>
<version>0.1.0</version>
</<ns>_CustomerHistory>
</modules>
<global>
<blocks>
<CustomerHistory>
<class><ns>_CustomerHistory_Block</class>
</CustomerHistory>
</blocks>
<models>
<CustomerHistory>
<class><ns>_CustomerHistory_Model</class>
</CustomerHistory>
</models>
</global>
<adminhtml>
<layout>
<updates>
<CustomerHistory>
<file>CustomerHistory.xml</file>
</CustomerHistory>
</updates>
</layout>
</adminhtml>
</config>
Path : app\code\local\ns>\CustomerHistory\Block\Adminhtml\Product\Edit\Tab.php
<?php
class <ns>_CustomerHistory_Block_Adminhtml_Catalog_Product_Edit_Tab extends Mage_Adminhtml_Block_Widget
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
public function canShowTab()
{
return true;
}
public function getTabLabel()
{
return $this->_('Custom Tab');
}
public function getTabTitle()
{
return $this->_('Custom Tab');
}
public function isHidden()
{
return false;
}
public function getTabUrl()
{
return $this->getUrl('*/*/customtab', array('_current' => true));
}
public function getTabClass()
{
return 'ajax';
}
}
?>
Path : app\design\adminhtml\default\default\layout\CustomerHistory.xml
<?xml version="1.0"?>
<layout>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<block type="CustomerHistory/adminhtml_catalog_product_edit_tab" name="custom_tab" template="CustomerHistory/catalog/product/edit/tab.phtml" />
<action method="addTab">
<name>Custom Tab</name>
<block>custom_tab</block>
</action>
</reference>
</adminhtml_catalog_product_edit>
</layout>
Path : app\design\adminhtml\default\default\template\CustomerHistory\catalog\product\edit\tab.phtml
<?php
/**
* Custom tab template
*/
?>
<div>
<h1>Hello</h1>
</div>
It goes without saying here, any help would be greatly appreciated!

Magento : Can't rewrite Widget Grid for optimize filtering

I working on Magento framework.
I want to rewrite Mage_Adminhtml_Block_Widget_Grid to optimize search filtering.
I add rewrite block on my etc/config.xml and name block as grid_search_date.
<config>
<global>
<helpers>
<core>
<rewrite>
<data>Jdate_Format_Helper_Data</data>
</rewrite>
</core>
</helpers>
<blocks>
<grid_search_date>
<rewrite>
<widget_grid>Jdate_Format_Block_Widget_Grid</widget_grid>
</rewrite>
</grid_search_date>
<topmenu_admin_time>
<rewrite>
<widget_grid_column_renderer_date>Jdate_Format_Block_Widget_Grid_Column_Renderer_Date</widget_grid_column_renderer_date>
</rewrite>
</topmenu_admin_time>
</blocks>
</global>
</config>
You can see Jdate_Format_Block_Widget_Grid want to rewrite widget_grid and we move to this file
class Jdate_Format_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
protected function _addColumnFilterToCollection($column)
{
if ($this->getCollection()) {
$field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
if ($column->getFilterConditionCallback()) {
call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
} else {
$cond = $column->getFilter()->getCondition();
die(var_dump($cond["orig_from"]));
die(var_dump($cond["orig_to"]));
if ($field && isset($cond)) {
$this->getCollection()->addFieldToFilter($field , $cond);
}
}
}
return $this;
}
}
But nothing happen. But if I change directly Widget_Grid file to this, it works.
Any idea?
Try
<global>
<blocks>
<adminhtml> <!-- should be the name of the module -->
<rewrite>
<widget_grid>Jdate_Format_Block_Widget_Grid</widget_grid>
</rewrite>
</adminhtml>
</blocks>
.....

Conditionally remove header/footer in Magento

I have a module page that can be accessed like
www.example.com/module/controller/action/id/10
I want something like this in my controller's action
$page = (int) Mage::app()->getRequest()->getParam('id');
if($page == '12')
{
$this->getLayout()->unsetBlock('header');
$this->getLayout()->unsetBlock('footer');
}
But above method doesn't work, I guess I'm passing wrong alias to unsetBlock method.
I know how to hide header/footer via layout xml but here I want to hide them in controller.
So basically I am searching an alternative for
<remove name="header"/>
<remove name="footer"/>
I found solution to my own question, sharing because it may help others.
1. Create a new layout handle for the page
// Namespace/Modulename/Model/Observer.php
Class Namespace_Modulename_Model_Observer extends Mage_Core_Model_Abstract
{
public function addAttributeSetHandle(Varien_Event_Observer $observer)
{
$page = (int) Mage::app()->getRequest()->getParam('id');
$handle = sprintf('modulename_controller_action_id_%s', $page);
$update = $observer->getEvent()->getLayout()->getUpdate();
$update->addHandle($handle);
}
}
2. Enable the observer in module's config.xml
// Namespace/Modulename/etc/config.xml
<frontend>
<events>
<controller_action_layout_load_before>
<observers>
<attributesethandle>
<class>Namespace_Modulename_Model_Observer</class>
<method>addAttributeSetHandle</method>
</attributesethandle>
</observers>
</controller_action_layout_load_before>
</events>
</frontend>
3. And then easily change the layout for the handle modulename_controller_action_id_12 in modules layout xml.
<modulename_controller_action_id_12>
<remove name="header"/>
<remove name="footer"/>
<reference name="root">
<action method="setTemplate">
<template>page/1column.phtml</template>
</action>
</reference>
</modulename_controller_action_id_12>

How do I display a template to only specific product pages in Magento?

I have a parts configurator that I only want to have appear for a certain product SKU. In my catalog/product/view.html file I have included the following code:
<?php
if ($_product->getSku() == '10007-') {
echo $this->getLayout()->createBlock('cms/block')->setBlockId('partfinder_selector')->toHtml();
}
?>
Then, in the xml file for the configurator:
<catalog_product_view>
<reference name="content">
<block type="partfinder/selector" name="partfinder_selector" before="-" template="partfinder/selector.phtml"/>
</reference>
The issue is, with this setup, the configurator appears on all product pages. Do I have something wrong in my code, or is there a better method for what I am trying to accomplish. Any help would be appreciated.
Note, this solution doesn't require any 'configurator xml', so you can leave that out. You'll be able to use an if statement like in your example to select only the sku you want.
Create directories:
app/code/local/Partfinder/Selector/Block
app/code/local/Partfinder/Selector/etc
For your app/etc/modules as Partfinder_Selector.xml:
<config>
<modules>
<Partfinder_Selector>
<active>true</active>
<codePool>local</codePool>
</Partfinder_Selector>
</modules>
</config>
In your app/code/local/Partfinder/Selector/etc as config.xml:
<?xml version="1.0"?>
<config>
<modules>
<Partfinder_Selector>
<version>0.1.0</version>
</Partfinder_Selector>
</modules>
<global>
<blocks>
<partfinderselector>
<class>Partfinder_Selector_Block</class>
</partfinderselector>
</blocks>
</global>
</config>
In your app/code/local/Partfinder/Selector/Block as Menu.php:
<?php
class Partfinder_Selector_Block_Menu extends Mage_Core_Block_Template
{
}
?>
In your app/design/pkgname/themename/template/Partfinder save a file called selector.phtml containing your block code.
In catalog/product/view.phtml:
<?php if ($_product->getSku() == "10007-"): ?>
<?php echo $this->getLayout()->createBlock('partfinderselector/menu')->setTemplate('partfinder/selector.phtml')->toHtml(); ?>
<?php endif; ?>
First, when you use echo $this->getLayout()->createBlock('cms/block')--setBlockId('partfinder_selector')->toHtml()
Magento tries to output the static block with the id partfinder_selector.
Second, <reference name="content"> refers to the content block which is a "core/text_list" which automatically outputs its children, so your block will be automatically output by the content block.
The solution would be, in your xml file use
<reference name="product.info"> instead of "content"
and use $this->getChildHtml('partfinder_selector') in catalog/product/view.html
You are require to follow below steps to do this:
1) - In the /Namespace/Module/etc/config.xml you have to write the following thing:
<config>
<modules>
<Namespace_Module>
<version>0.1.0</version>
</Namespace_Module>
</modules>
<global>
<helpers>
<module>
<class>>Namespace_Module_Helper</class>
</module>
<catalog>
<rewrite>
<product_view>Namespace_Module_Helper_View</product_view>
</rewrite>
</catalog>
</helpers>
</global>
</config>
2) - Register module in magento module list under app/etc/modules/Namespace_Module.xml
<config>
<modules>
<Namespace_Module>
<active>true</active>
<codePool>local</codePool>
<depends />
</Namespace_Module>
</modules>
</config>
3) - You have to create your file under /Namespace/Module/Helper/view.php.
Please find below code
class KNamespace_Module _Helper_View extends Mage_Catalog_Helper_Product_View
{
public function initProductLayout($product, $controller)
{
$design = Mage::getSingleton('catalog/design');
$settings = $design->getDesignSettings($product);
if ($settings->getCustomDesign()) {
$design->applyCustomDesign($settings->getCustomDesign());
}
$update = $controller->getLayout()->getUpdate();
$update->addHandle('default');
$controller->addActionLayoutHandles();
$update->addHandle('PRODUCT_TYPE_' . $product->getTypeId());
$update->addHandle('PRODUCT_' . $product->getId());
//update code
if(preg_match('/10007-/',$product->getSku())){$update->addHandle('PRODUCT_NEWLAYOUT');}
$controller->loadLayoutUpdates();
// Apply custom layout update once layout is loaded
$layoutUpdates = $settings->getLayoutUpdates();
if ($layoutUpdates) {
if (is_array($layoutUpdates)) {
foreach($layoutUpdates as $layoutUpdate) {
$update->addUpdate($layoutUpdate);
}
}
}
$controller->generateLayoutXml()->generateLayoutBlocks();
// Apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout()) {
$controller->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
}
$currentCategory = Mage::registry('current_category');
$root = $controller->getLayout()->getBlock('root');
if ($root) {
$controllerClass = $controller->getFullActionName();
if ($controllerClass != 'catalog-product-view') {
$root->addBodyClass('catalog-product-view');
}
$root->addBodyClass('product-' . $product->getUrlKey());
if ($currentCategory instanceof Mage_Catalog_Model_Category) {
$root->addBodyClass('categorypath-' . $currentCategory->getUrlPath())
->addBodyClass('category-' . $currentCategory->getUrlKey());
}
}
return $this;
}
}
add code in catalog.xml:-
partfinder/selector.phtml
Hope this will work for you. :)
Best of luck.
Jitendra Padmashali, KrishInc

Categories