I'm trying to integrate a php file inside an aplication that gives users the posibility to register an account. This account has to be registered inside a magento store.
This is what i have to this momment:
<?php
require_once '../../../app/Mage.php';
umask(0);
Mage::app('default');
Mage::app()->getTranslator()->init('frontend');
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
// get layout object
$layout = Mage::getSingleton('core/layout');
//get block object
$block = $layout->createBlock('core/template');
//print_r(get_class_methods(get_class($block))); <- use for seeing classes
$block = $block->setTemplate('customer/form/register.phtml')->renderView();
echo $block;
?>
This code renders the registration form but stops when he is showing the input fields. I tried with "mini.login.phtml" and it renderes correctly. I'm not very good at Magento, or english. I can provide any other information if necessary.
Any help will be appreciated!
Regards
You are using the wrong block type when dynamically creating the block. Try using this:
createBlock('customer/form_register')
Related
In Magento I'm using <?php if ($this->getIsHomePage()):?> to check if it's the main page or not but how would I check if the user is on a custompage? CMS Page Url Key game-store
To get the URL key / identifier of any CMS page in Magento, use the following bit of code.
<?php
$yourUrlKey = 'game-store';
$cmsPageUrlKey = Mage::getSingleton('cms/page')->getIdentifier();
if($yourUrlKey == $cmsPageUrlKey){
//do something here
}
?>
The above code will print your URL Key
Either
$model = Mage::getModel('cms/page')->load('game-store','identifier');
var_dump($model->getData());
var_dump($model->getPageId());
or
$model = Mage::getModel('cms/page')->getCollection()
->addFieldTofilter('identifier','game-store')
->getFirstItem();
var_dump($model->getData());
var_dump($model->getPageId());
should do it.
I actually can add a category via setup script, the thing is for some reason some of the fields doesn't get set properly. Here's is my code
$this->startSetup();
Mage::register('isSecureArea', 1);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setName('Category Name')
->setUrlKey('category-name')
->setIsActive(0)
->setIncludeInMenu(1)
->setInfinitescroll(1)
->setDisplayMode('PAGE')
->setLandingPage($idToCmsBlock)
->setPageLayout('anotherLayoutThanDefault')
->setCustomUseParentSettings(0)
->setCustomLayoutUpdate('<reference name="head"><action method="addCss"><stylesheet>css/somecss.css</stylesheet></action></reference>')
->save();
$this->endSetup();
After running this script, I have a category created with all my value set in the EAVs table.
However the Flat table will be missing displayMode, landingPage, pageLayout, customLayoutUpdate even if I re-index the flat table.
The weird thing is that if I go in the admin, I can see all those fields properly set but if I go in my frontend most of those fields are ignored. I will have to go to the admin, unset those value and reset them for each of them to work properly.
Also let say I use setEnabled(1), my category will be "enable" in the admin but not show up in the frontend.
PS: I have Flat Category activated, if I disable it seems to work fine but if I re-index it still not working.
I finally found it, I'm not sure why but those fields are not showing up properly because they were inserted for the default store (storeId=1) because my script is running in an update script. You need to use the storeId 0.
With this information you would think that the solution would be something like :
$this->startSetup();
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
But this code doesn't work either. Indeed after looking into Mage::app() (Mage_Core_Model_App Line 804) I noticed a IF condition that would always return the default store if you're in a setup script.
The trick is to fake that you're not in a setup script, my working solution is:
$this->startSetup();
Mage::register('isSecureArea', 1);
// Force the store to be admin
Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
I ran into the same issue when updating a category via a data install script. The solution provided in the accepted answer did work for updating the category, but can be improved upon as follows:
In the solution, the user that triggers the update script is forced to the admin environment. This can be remedied by saving the current store id and switching back at end of the script.
It doesn't seem that adding isSecureArea to the registry or disabling update mode had any use (at least for the use case of updating a category).
I ended up with the following data install script for updating a category (in this example, a category is loaded by name, after which the name is updated):
<?php
$this->startSetup();
//Switch to admin store (workaround to successfully save a category)
$originalStoreId = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
//update category
$category = Mage::getModel('catalog/category')
->loadByAttribute('name', 'OLD_CATEGORY_NAME');
if ($category) {
$category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('NEW_CATEGORY_NAME')
->save();
}
//Set store to original value
Mage::app()->setCurrentStore($originalStoreId);
$this->endSetup();
?>
Try this
<?php
require_once "../app/Mage.php";
umask(0);
Mage::app('default');
$proxy = new SoapClient("http://127.0.0.1/magento/index.php/api/soap/?wsdl");
$sessionId = $proxy->login($magento_webservices_username, $magento_webservices_passwd);
$data = array('name'=>'Nokia',
'description'=>'',
'meta_description'=>'',
'meta_keywords'=>'',
'default_sort_by'=>'price',
'available_sort_by'=>'price',
'is_active'=>1
);
$newCategoryId = $proxy->call($sessionId, 'category.create', array(3, $data, 1));
echo "Category ID: ".$newCategoryId;
?>
And also have a look Magento create category
Take a look at this. Hope it will help you.
http://inchoo.net/ecommerce/magento/how-to-add-new-custom-category-attribute-in-magento/
I have created multiple categories via installer script.
<?php
$installer = $this;
$installer->startSetup();
Mage::register('isSecureArea', 1);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2/4') // set parent to be root category
->setName('CAT NAME') //Category Name
->setIsActive(1) // Category Status
->setIncludeInMenu(1) // Show in Menu
->setIsAnchor(1) // used for Layered navigation
->setDisplayMode('PAGE') // Product Only
->setPageLayout('one_column') // Page layout
->save();
$installer->endSetup();
I have researched this topic pretty thoroughly but can't find an answer.
I am trying to include a Magneto head into a WordPress page. I created a new wordpress template and added the following code to it.
try {
require_once ABSPATH . '/app/Mage.php';
if (Mage::isInstalled()) {
$mage = Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
if(Mage::getDesign()->designPackageExists('xxx')) {
Mage::getDesign()->setPackageName('xxx');
Mage::getDesign()->setTheme('xxx');
}
// init translator
$mage->getTranslator()->init('frontend');
// init head
$head = $mage->getLayout()->getBlockSingleton('page/html_head');
} }
Then a bit further down in the template I have
echo $head->toHtml();
Now what is happening is some parts of the head are being echoed and some parts are not.
When I go into head.phtml and try to figure out what is happening I notice that any line that contains
$this->getChildHtml()
does not get echoed.
I looked at this example and noticed that the author is manually adding the html and CSS. Why is this? Why don't they get added automatically? Is this a related problem
Thanks
To show a block that is generated inside the header block, you need to first create it, then set it as child of the header block.
eg. Here is how I display within Wordpress a Magento header block, including the currency drop-down block that was generated by getChildHtml() inside the original header.phtml:
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton("customer/session");
$layout = Mage::getSingleton('core/layout');
$headerBlock = $layout->createBlock('page/html_header')->setTemplate('page/html/header.phtml');
$currencyBlock = $layout->createBlock('directory/currency')->setTemplate('currency/currency.phtml');
$headerBlock->setChild('currency_selector', $currencyBlock);
$headerBlock = $headerBlock->toHtml();
Then you can write the block where you need it on the page:
echo $headerBlock;
I know it's a little late but hopefully this helps others with this issue.
Are you familiar with how magento layouts are rendered? With $head = $mage->getLayout()->getBlockSingleton('page/html_head'); you create a new block without any children. That's why the author needs to add JS and CSS again. To load the default head block have a look at this thread Load block outside Magento. You can load it with $layout->getBlock('head')->toHtml();.
I'm trying to pull the header of our Magento store in a standalone php page. Everything works as expected except the 'Log In' link does not appear. The customer.xml file uses the standard 'customer_logged_in' node to 'addLink' but it seems like the login status isn't getting assessed with the method I'm using. How do I get this Log In | Log Out link to display?
Here is the code I'm using:
require_once $mage_path;
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
$layout = Mage::app()->getLayout();
$layout->getUpdate()->addHandle('default')->load();
$layout->generateXml()->generateBlocks();
echo $layout->getBlock('header')->toHtml();
I'm able to get the correct login state independently using the following:
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
if ($session->isLoggedIn()) {
/* logged in */
} else {
/* not logged in */
}
However, I don't want to manage two different styles (one through the default magento XML and another for this custom page). I would rather have the getBlock call return the whole block with the correct login status. Any insight is appreciated.
You need to add the customer_logged_in to your handles, as well as default. For example:
...
$handles = array('default');
if (Mage::helper('customer')->isLoggedIn()) {
$handles[] 'customer_logged_in';
}
$layout->getUpdate()->addHandle($handles);
...
I have magento installed in a folder called store. The min site is installed above that at the root of the hosting.
I have followed many ideas on how to get the cart to display on external pages, but I cannot seem to get any info passed from magento.
<?
$mageFilename = 'store/app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
/* Init User Session */
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
if ($session->isLoggedIn()) {
echo'logged in <br />';
/* do something if logged in */
} else {
echo'not logged in<br />';
/* do something else if not logged in */
}
/* Magento uses different sessions for 'frontend' and 'adminhtml' */
Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo 'cart items count: ' . $cart;
?>
I have this code at present to call the number of items in the cart, and also state whether the user is logged in or not. But nothing seems to get passed to it.
I believe you must run this from a Magento Controller.
Magento is an incredibly powerful, advanced, professional PHP platform. It is one of the most advanced PHP MVC frameworks out there. If you're trying to build a PHP site that has a Magento store in it, why wouldn't you use Magento to build the whole site? Are you using a better platform?