Magento: Get Customer ID in Admin - magento

In Magento 1.9 I want to get the customerId of opened customer in Mage_Adminhtml_Block_Customer_Edit_Tab_View.
I tried like this:
$customer_session = Mage::getSingleton('customer/session'); //I saw here in Stackoverflow this
$customer = $customer_session->getCustomer();
$customerID = $customer->getId();
But I got a null.
I also tried with $this->getId() and Mage::helper('customer')->getId(), but neither worked.
How do achieve this?

For admin we should use 'admin/session' for 'customer/session'
$user = Mage::getSingleton('admin/session');
$userId = $user->getUser()->getUserId();
$userEmail = $user->getUser()->getEmail();

I figured out that the correct way is:
$customerID = $this->getCustomer()->getId();

Related

how can i access directly to magento order history from observer?

How can i get the value ,what i set by addStatusHistoryComment while creating order by php script.
$order = $observer->getEvent()->getOrder();
$dbOrderId = $order->getId();
$MagOrderId = $order->getRealOrderId();
Mage::log('dbOrderId : '. $dbOrderId);
Mage::log('MagOrderId : '. $MagOrderId);
I need to get like something $order->getStatusHistoryComment()
it is not working.
Need help.
The following data is not working as order is not commit yet.
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT comment FROM sales_flat_order_status_history WHERE parent_id=' $dbOrderId' limit 1 ";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
foreach ($connection->fetchAll($sql) as $arr_row) {
$comments=$arr_row['comment'];
Use getStatusHistoryCollection instead of getStatusHistoryComment and it should work. The method is defined in Mage_Sales_Model_Order.
Or you can use getVisibleStatusHistory if you want only the comments visible on frontend.

how to get sku and product name on the head.php page?

how to get sku and product name on the head.php (\app\code\core\Mage\Page\Block\Html\head.php) page? thank you.
when i used $this->_data['sku']; or $this->getSku(); are all not work.
The previous answer is fine, except it doesn't check if product exists in registry. So you will get fatal error on non-product pages. Always make a check if variable exists.
$product = Mage::registry('current_product');
if ($product) //sometimes need check for instanse, use instanseof
{
$product->getSku();
}
You should be able to pull the current product back from the registry and access the values from that.
$product = Mage::registry('current_product');
$product->getSku();
Working Code:
if($_product = Mage::registry('current_product')){
$id = $_product->getId();
$sku = $_product->getSku();
}

Magento external Script and Session

I want to add an external script which gets a sku via GET check the ID and then redirect to the cart if available otherwise it sets en error and also redirecting to the cart.
The script is called from a product page:
http://myhost/scripts/addto.php?sku=12345
Here is the colmplete code
<?php
include_once '../../../../../app/Mage.php';
Mage::app();
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
$sku = $_GET['sku'];
if (!isset($_GET['qty'])) { $qty = '1'; } else { $qty = $_GET['qty']; }
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
if ($id == '') {
$id = $sku;
Mage::getSingleton('checkout/session')->addError("Product not found!");
}
Works fine, but after logout and relogin the error message is missing. I found out that's because of a cookie which is set. After deleting that cookie the error message is working again after relogin.
What does not work?
You logout and login and then the script stop working? So the session is not found? The product ist not found? The product is not loaded? The user is not forwarded? :-)
Sure it does not. The Message is a Notice. If it is shown once, it is deleted.
What behaviour do you want?
use Mage::getSingleton('core/session')->addError("Product not found!");, maybe checkout/session is user specific...
cheers

How to access Magento order 'visible_on_front' property?

How to access Magento order 'visible_on_front' property?
I've tried the following bits:
$order = $this->getOrder();
$order->setStatus('processing');
$order->setData('visible_on_front', 1);
or
$history = $this->getOrder()->addStatusHistoryComment('msg');
$history->setStatus('processing');
$history->setData('state', 'visible');
or
$history = $this->getOrder()->addStatusHistoryComment('msg');
$history->setStatus('processing');
$history->setData('visible_on_front', 1);
Thanks in advance.
try this and see if it has changed after you changed the data
$order = $this->getOrder();
$order->setStatus('processing');
$order->setVisibleOnFront(1);
print_r($order->getData());
I've just had this need, I solved it like this:
$order->addStatusHistoryComment(<your comment>,<your status>)
->setIsVisibleOnFront(1);
Of course you load into $order the order you want to use.
With these you will have the comment visible into the user account page under the order details.

Checking for Magento login on external page

I'm hitting a wall here while trying to access items from Magento on an external page (same server, same domain, etc, etc). I want to see if the user is logged into Magento before showing them certain parts on the site.
Keep in mind that this code exists outside of Magento.
Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));
if (empty($session))
{
$session = Mage::getSingleton("customer/session");
}
if($session->isLoggedIn())
echo "hi";
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo $cart;
$cart returns 0, where I definitely have products in my cart. isLoggedIn() also returns false. What am I doing wrong here? Is there an option in Magento that I need to turn on or off to be able to access this information outside of Magento?
Using the code above, I created a php file in the Magento folder. From there, added the number of items in the cart and whether you were logged in or not to an array and encoded it as json. I used some jquery on my external page to grab the file and pull the data I needed.
Not quite the ideal situation, but it works for now.
Are you including Mage.php (which defines getSingleton)?
require_once ($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php');
What does $session have in it after the getSingleton() call?
print_r ($session);
EDIT: I tried this on my end and was not able to get accurate isLoggedIn() or getItemsCount() data. When I dumped out $session its showing all the fields as 'protected.'
I'm not interested in requiring the user to log in...I merely want to access data from the already logged in session.
Anyone else have any thoughts on this? All the examples out there seem to be pre 1.4.x.
require_once "app/Mage.php";
umask(0);
Mage::app();
// require_once $_SERVER['DOCUMENT_ROOT'] . "/mage1/app/Mage.php";
// Customer Information
$firstname = "krishana";
$lastname = "singh";
$email = "krish.bhati#gmail.com";
$password = "myverysecretpassword";
// Website and Store details
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
$mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';
$mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';
$app = Mage::app ( $mageRunCode, $mageRunType );
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');
$session->start();
$customer->loadByEmail($email);
$customer_id= $customer->getId();
if($customer_id){
Mage_Core_Model_Session_Abstract_Varien::start();
$session->login($email, $password);
$session->setCustomerAsLoggedIn($session->getCustomer());
echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';
}

Resources