This is my first time with Magento. I have to prepare module which adds select field (yes/no) into General information (Category in admin Panel). I have already done this part. Next step is to check value selected in General information form when user goes to category side. If the user is not logged-in and admin option has been selected to "yes" in General information form, system should display information like: "you must log in".
Below my folder structure:
- app
-> code
-> community
-> AttributeCategory
->CustomAttributeCategory->
- etc
-> config.xml
<?xml version="1.0"?>
<config>
<modules>
<AttributeCategory_CustomAttributeCategory>
<version>0.0.3</version>
</AttributeCategory_CustomAttributeCategory>
</modules>
<global>
<resources>
<add_category_attribute_login>
<setup>
<module>AttributeCategory_CustomAttributeCategory</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</add_category_attribute_login>
<add_category_attribute_login_write>
<connection>
<use>core_write</use>
</connection>
</add_category_attribute_login_write>
<add_category_attribute_login_read>
<connection>
<use>core_read</use>
</connection>
</add_category_attribute_login_read>
</resources>
</global>
</config>
- sql -> add_category_attribute_login ->
- mysql4-install-0.0.3.php :
<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'is_category_allowed', [
'group' => 'General Information',
'type' => 'int',
'input' => 'select',
'label' => 'required logged-in user',
'sort_order' => 1000,
'visible' => true,
'required' => true,
'source' => 'eav/entity_attribute_source_boolean',
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'option' => [
'values' => [
0 => 'No',
1 => 'Yes',
]
],
]);
$this->endSetup();
AND
- app->etc->modules:
AttributeCategory_CustomAttributeCategory.xml:
<?xml version="1.0"?>
<config>
<modules>
<AttributeCategory_CustomAttributeCategory>
<active>true</active>
<codePool>community</codePool>
</AttributeCategory_CustomAttributeCategory>
</modules>
</config>
Please, tell me how can I check value in front when users visit category pages?
You should create an observer which looks at the attribute value on the category that is loaded, then performs the check and if necessary sets an error message and redirects to the customer's login page.
You could observe the event catalog_controller_category_init_after which is dispatched in Mage_Catalog_CategoryController::_initCategory after the category is loaded. This means that all attributes for the category will be available to look at, regardless of whether they are in the category flat tables or not.
Create an observer:
// File: app/code/community/AttributeCategory/CustomAttributeCategory/Model/Observer.php
class AttributeCategory_CustomAttributeCategory_Model_Observer
{
public function checkLoggedInForCategory(Varien_Event_Observer $event)
{
// Get the category from the event
/** #var Mage_Catalog_Model_Category $category */
$category = $event->getCategory();
// Get the customer's session model
/** #var Mage_Customer_Model_Session $customerSession */
$customerSession = Mage::getSingleton('customer/session');
if ($category->getIsCategoryAllowed() && !$customerSession->isLoggedIn()) {
// Add a custom message here?
$customerSession->addError('You must be logged in to view this category.');
// Redirect to login page
Mage::app()
->getResponse()
->setRedirect(Mage::getUrl('customer/account/login'))
->sendResponse();
exit;
}
}
}
The logic here basically says "get the category from the event" which can be done because the point where it is dispatched passes it as an argument, "get the customer session" which is always available regardless of whether the customer is logged in or not, "check 'is_category_allowed' is truthy and that the customer is not logged in" and if so add a validation error message, and redirect to the login page.
The login page automatically renders and displays all message block entries, so you don't need to handle the display manually.
Now you need to define your observer in your config.xml and connect it to the event:
<!-- File: app/code/community/AttributeCategory/CustomAttributeCategory/etc/config.xml -->
<?xml version="1.0"?>
<config>
<modules>
<AttributeCategory_CustomAttributeCategory>
<version>0.0.3</version>
</AttributeCategory_CustomAttributeCategory>
</modules>
<global>
...
</global>
<frontend>
<events>
<catalog_controller_category_init_after>
<observers>
<ensure_customer_can_view_category>
<class>AttributeCategory_CustomAttributeCategory_Model_Observer</class>
<method>checkLoggedInForCategory</method>
</ensure_customer_can_view_category>
</observers>
</catalog_controller_category_init_after>
</events>
</frontend>
</config>
I hope this helps. There's plenty of resource online about how to create observers etc, they're very useful things to use.
This registers an observer in the class AttributeCategory_CustomAttributeCategory_Model_Observer, method named checkLoggedInForCategory which is connected to the catalog_controller_category_init_after event in the frontend only. You can always define this in the global scope as well, but there's no point since it's only dispatched in the frontend, and should only be used for customers in the frontend.
Related
Basically what i am trying to do : I want some custom attributes for Customer entity. And to add those fields in db i am executing script.
I tried lots & lots of tweaks , methods but i cant solve SQL script error.
The important thing in it is : It makes entry in core_resource table.
My script made entry in core_resource.
I made extension according to this Alan storm Site .
Any help is highly appreciated.
My extension structure
Here is my coding part.
Config file of module
<?xml version="1.0"?>
<config>
<modules>
<StackExchange2_Customer>
<version>0.1.0</version>
</StackExchange2_Customer>
</modules>
<global>
<resources>
<stackExchange2_customer_setup>
<setup>
<module>StackExchange2_Customer</module>
<class>StackExchange2_Customer_Model_Resource_Mysql4_Setup</class>
<!-- <class>StackExchange2_Customer_Sql</class>-->
</setup>
<!--Mew node-->
<connection>
<use>core_setup</use>
</connection>
</stackExchange2_customer_setup>
<!--as per sOverflow-->
<stackExchange2_customer_write>
<connection>
<use>core_write</use>
</connection>
</stackExchange2_customer_write>
<stackExchange2_customer_read>
<connection>
<use>core_read</use>
</connection>
</stackExchange2_customer_read>
</resources>
</global>
</config>
Setup.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//Mage_Eav_Model_Entity_Setup
class StackExchange2_Customer_Model_Resource_Mysql4_Setup extends Mage_Core_Model_Resource_Setup{
}
mysql4-install-0.1.0.php
<?php
//echo "in script";die;
$installer = $this;
echo "hi123";
$installer->startSetup();
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//attr1
$installer->addAttribute('customer','name_of_child', array(
'type' => 'varchar',
'label' => 'Name of child',
'input' => 'text',
'position' => 120,
'required' => false,//or true
'is_system' => 0,
));
$attribute1 = Mage::getSingleton('eav/config')->getAttribute('customer', 'name_of_child');
//
$attribute1->setData('used_in_forms', array(
'adminhtml_customer',
'checkout_register',
'customer_account_create',
'customer_account_edit',
));
//
$attribute1->setData('is_user_defined', 0);
//
$attribute1->save();
Mage::log(__FILE__ . 'Update installed.');
$installer->endSetup();
Any help is highly appreciated
NOTE : In localhost it gives error : Am I using wrong class or anything missing?
( ! ) Fatal error: Call to undefined method StackExchange2_Customer_Model_Resource_Mysql4_Setup::addAttribute() in D:\xampp\htdocs\mykidscare\app\code\local\StackExchange2\Customer\sql\stackexchange2_customer_setup\mysql4-install-0.1.0.php on line 12
Add the following.
<resources>
<stackExchange2_customer_setup>
<setup>
<module>StackExchange2_Customer</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</stackExchange2_customer_setup>
<stackExchange2_customer_write>
<connection>
<use>core_write</use>
</connection>
</stackExchange2_customer_write>
<stackExchange2_customer_read>
<connection>
<use>core_read</use>
</connection>
</stackExchange2_customer_read>
</resources>
You need to add Mage_Eav_Model_Entity_Setup class in setup in order to add any attribute.
Finally i solved it :) .
NOW it works after extending proper class (Mage_Customer_Model_Entity_Setup).
class StackExchange2_Customer_Model_Resource_Mysql4_Setup extends Mage_Customer_Model_Entity_Setup{
}
I have some trouble in Magento sql updating table. I create extension and I would like to add some new columns to existing table 'customer_group'.
<modules>
<Module_Name>
<module>1.0.0</module>
</Module_Name>
</modules>
<global>
<models>
<module_name>
<class>Module_Name_Model</class>
<resourceModel>module_name_resource</resourceModel>
</module_name>
<module_name_resource>
<class>Module_Name_Model_Resource</class>
</module_name_resource>
</models>
<resources>
<module_name_setup>
<setup>
<module>Module_Name</module>
<class>Module_Name_Model_Resource_Setup</class>
</setup>
</module_name_setup>
</resources>
</global>
In sql/module_name_setup/mysql4-install-1.0.0.php I have this code:
$installer = $this;
$connection = $installer->getConnection();
$installer->startSetup();
$installer->getConnection()
->addColumn($installer->getTable('customer/customer_group'), 'column_one', array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_TEXT,
'NULLABLE' => false,
'COMMENT' => 'Column One'
))
->addColumn($installer->getTable('customer/customer_group'), 'column_two', array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_TEXT,
'NULLABLE' => false,
'COMMENT' => 'Column Two'
));
$installer->endSetup();
But when I refresh Magento frontend page I didn't saw in core_resources table any changes and in customer_group these two columns didn't exist.
What I do wrong?
Thanks!
For what it's worth, this is how I do a install script:
Place the install script at:
Company/Module/sql/your_module_setup/install-1.0.0.0.php
And in the etc/config.xml of the module:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Company_Module>
<version>1.0.0.0</version>
</Company_Module>
</modules>
<global>
<resources>
<your_module_setup>
<setup>
<module>Company_Module</module>
<class>Company_Module_Model_Resource_Setup</class>
</setup>
</your_module_setup>
</resources>
</global>
</config>
Then make create the file Company/Module/Model/Resource/Setup.php:
<?php
class Company_Module_Model_Resource_Setup extends Mage_Sales_Model_Resource_Setup
{
}
I've solve this bug. I forgot to set version in app/etc/Module_Name.xml, it should be
<modules>
<Module_Name>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Shipping/>
</depends>
<version>1.0.0</version>
</Module_Name>
</modules>
and in config.xml version should be set too
I am creating category page and i want to add two static blocks to it. While I was going through CMS/Static Blocks, I realized I can Only add One Static Block to any page. I couldn't find anything where i can add 2 or more static blocks. Is there a way i can add two or more such static blocks in a single category page.
If you want to add static block from Catalog->Manage Categories, then as you know you can call 1 static block at a time, but by using simple trick, you can call as many static blocks as you want.
Call 1 static block from admin panel Catalog->Manage Categories. Then call other static blocks from static block, which you are calling from category.
I hope this will help you.
First add new field into category.I have create a new field name "landing_page_2". I have created and extension for that works...
Step1:Create config.xml
Under: app\code\local\Amit\Catmattribute\etc
<?xml version="1.0"?>
<config>
<modules>
<Amit_Catmattribute>
<version>0.1.0</version>
</Amit_Catmattribute>
</modules>
<global>
<helpers>
<catmattribute>
<class>Amit_Catmattribute_Helper</class>
</catmattribute>
</helpers>
<models>
<catmattribute>
<class>Amit_Catmattribute_Model</class>
<resourceModel>catmattribute_mysql4</resourceModel>
</catmattribute>
</models>
<resources>
<categoryattribute1394603225_setup>
<setup>
<module>Amit_Catmattribute</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</categoryattribute1394603225_setup>
<categoryattribute1394603225_write>
<connection>
<use>core_write</use>
</connection>
</categoryattribute1394603225_write>
<categoryattribute1394603225_read>
<connection>
<use>core_read</use>
</connection>
</categoryattribute1394603225_read>
</resources>
</global>
</config>
step2:Create mysql4-install-0.1.0.php under:app\code\local\Amit\Catmattribute\sql\categoryattribute1394603225_setup
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("catalog_category", "landing_page_2", array(
"type" => "int",
"backend" => "",
"frontend" => "",
"label" => "CMS Block 2",
"input" => "select",
"class" => "",
"source" => "catalog/category_attribute_source_page",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'group' => 'Display Settings',
"visible" => true,
"required" => false,
"user_defined" => false,
"default" => "",
"searchable" => false,
"filterable" => false,
"comparable" => false,
"visible_on_front" => false,
"unique" => false,
"note" => ""
));
$installer->endSetup();
Step3:create Data.php
app\code\local\Amit\Catmattribute\Helper
<?php
class Amit_Catmattribute_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Step4:Amit_Catmattribute.xml under app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<Amit_Catmattribute>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Amit_Catmattribute>
</modules>
</config>
Copy view.php from app/code/core/Mage/Catalog/Block/Category/
to app/code/local/Mage/Catalog/Block/Category/
add new function
public function getCmsBlocktwoHtml()
{
if (!$this->getData('cms_block_html_2')) {
$html = $this->getLayout()->createBlock('cms/block')
->setBlockId($this->getCurrentCategory()->getLandingPage2())
->toHtml();
$this->setData('cms_block_html_2', $html);
}
return $this->getData('cms_block_html_2');
}
app\design\frontend\your package\your template\template\catalog\category\view.ptml
below code add after <?php echo $this->getCmsBlockHtml() ?>
<?php echo $this->getCmsBlocktwoHtml()?>
Hope it will be works.The section will manage fro madmin
You can put the following code in .phtml file to call static block
<?php $app = Mage::app(); ?>
<?php echo $app->getLayout()
->createBlock('cms/block')
->setBlockId('your_block_id')->toHtml(); ?>
What you can do is create the two required static blocks from cms>static blocks (eg:st1 and st2) and an another static block that will include both these static blocks (eg: dual_block).
In the dual_block static block you can insert widget, choose widget type as CMS Static Block then select the required block. You can similarly add other blocks as well. Make the required formatting to display the blocks as required.
Then in the required category, click on display settings tabs, select display mode as static block only and in CMS Block select dual_block static block. Then save the category and now you have two static blocks displayed in the category page.
Hope it helps!!
I need to extend the custom success message after the customer registration. Now after registration the user is redirected to the dashboard and the standard success message is shown: "Thank you for registering with...".
I need that this message changes base on the customer group attribute. I read on-line but didn't find a working solution... but I think I'm making it in the wrong way. I started from here: http://mydons.com/simple-example-using-magento-event-observer/ to make a custom observer on the customer_register_success event, so I made the module xml named Bbox_Regmess.xml in app/etc/modules:
<config>
<modules>
<Bbox_Regmess>
<active>true</active>
<codePool>local</codePool>
</Bbox_Regmess>
</modules>
</config>
Than I made the app/code/local/Bbox/Regmess/etc and app/code/local/Bbox/Regmess/Model folders with inside the config.xml:
<config>
<modules>
<Bbox_Regmess>
<version>0.1.0</version>
</Bbox_Regmess>
</modules>
<frontend>
<events>
<customer_register_success>
<observers>
<Bbox_Regmess_Model_Observer>
<type>singleton</type>
<class>Bbox_Regmess_Model_Observer</class>
<method>Customregmess</method>
</Bbox_Regmess_Model_Observer>
</observers>
</customer_register_success>
</events>
</frontend>
</config>
And the Observer.php that is just a first try to see if I'm able to add a custom success message:
<?php
class Bbox_Regmess_Model_Observer {
public function Customregmess($observer) {
$event = $observer->getEvent(); //Fetches the current event
$customer = $event->getCustomer();
$eventmsg = "Current Event Triggered : <I>" . $event->getName() . "</I><br/> Currently Added Product : <I> " . $customer->getCustomerName()."</I>";
//Adds Custom message to shopping cart
Mage::getSingleton('customer/session')->addSuccess($eventmsg);
}
}
?>
Now if a user registers to the shop, he get the standard registration message and there is not the custom $eventmsg
What I'm making wrong? There is another way to to that? Thanks
UPDATE:
looking deeper I found out the default success message is defined in the app/code/core/Mage/Customer/controllers/AccountController.php at line 390 (just after the line 334 where there is the definition of customer_register_success event I'm trying to work with).
at line 390 there is the _welcomeCustomer function that is in charge of setting the success message, send confirmation email and set the success redirect url:
protected function _welcomeCustomer(Mage_Customer_Model_Customer $customer, $isJustConfirmed = false)
{
$this->_getSession()->addSuccess(
$this->__('Thank you for registering with %s.', Mage::app()->getStore()->getFrontendName())
);
$customer->sendNewAccountEmail(
$isJustConfirmed ? 'confirmed' : 'registered',
'',
Mage::app()->getStore()->getId()
);
$successUrl = Mage::getUrl('*/*/index', array('_secure'=>true));
if ($this->_getSession()->getBeforeAuthUrl()) {
$successUrl = $this->_getSession()->getBeforeAuthUrl(true);
}
return $successUrl;
}
Is there any chance to extend this function so I can manage multiple success message base on the customer group?
I've look for some resource about it, but I didn't find anything usefull
I think that you must place the events tag in the global tag
<config><global>...<events>...</events>...</global>...</config>
EDIT:
and the observer file extends Varien_Event_Observer, at least that worked with mine
class Bbox_Regmess_Model_Observer extends Varien_Event_Observer{}
I was wondering whether there is any way to create custom Orders statuses in Magento. I am developing a Magento Extension in which I have to add some custom order status to magneto orders.
I googled a lot but didn't find any good resources for this.
Could anybody explain how to do this, any resources to refer.
Lets say you want to add "Authorized Payment" status with "authorized" code.
Add the following to config.xml of your module under config/global:
<sales>
<order>
<statuses>
<authorized translate="label">
<label>Authorized Payment</label>
</authorized>
</statuses>
<states>
<authorized translate="label">
<label>Authorized Payment</label>
<statuses>
<authorized default="1"/>
</statuses>
<visible_on_front>1</visible_on_front>
</authorized>
</states>
</order>
</sales>
Earlier it was quite enough but in recent versions (1.5.x.x if I recall correctly) the following bit is also required. Add the following to mysql setup/update file of your extension:
<?php
$installer = $this;
$statusTable = $installer->getTable('sales/order_status');
$statusStateTable = $installer->getTable('sales/order_status_state');
$statusLabelTable = $installer->getTable('sales/order_status_label');
$data = array(
array('status' => 'authorized', 'label' => 'Authorized Payment')
);
$installer->getConnection()->insertArray($statusTable, array('status', 'label'), $data);
$data = array(
array('status' => 'authorized', 'state' => 'authorized', 'is_default' => 1)
);
$installer->getConnection()->insertArray($statusStateTable, array('status', 'state', 'is_default'), $data);
?>
This technically adds new status to your system. Now you can set it to your order like this:
$order->setState('authorized', true, 'Status history message')
->save();
Please let me know if you have any questions.
So that the status is visible at the front is needed:
<config>
<modules>
<MyCompany_MyModule>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Sales/>
</depends>
</MyCompany_MyModule>
</modules>
</config>