Magento Mage not working as expected from external php file

342 Views Asked by At

I have an external php file that I call using ajax on Magento register page. This php code should help validate if the value of a text input field is valid. See php code:

require_once ('app/Mage.php');
Mage::app()->getStore();
Mage::getSingleton('core/session', array('name' => 'frontend'));
$request = Mage::app()->getRequest();
$customer = Mage::getModel('customer/customer');
$inviteCode = $request->getParam('code');

if (($inviteCode) && ($inviteCode != '')) {
        $websiteId = Mage::app()->getWebsite()->getId();
        $customerId = $customer->setWebsiteId($websiteId)->loadByEmail($inviteCode)->getId();

        if ($customerId && ($customerId != '')) {
                $account = Mage::getModel('affiliateplus/account')->getCollection()->addFieldToFilter('customer_id', $customerId)->getFirstItem();

                if ($account && ($account->getAccountId())) {   
                 $result = "Valid Invite Code";
                } 
        }
}

//echo json_encode($result);
echo ($account );

I have a multiple website installation. This code sits in a addon domain hosted in a subfolder yyy.

  1. $websiteId should return "5" (xxx.com/yyy), but it keeps returning "1" (xxx.com).
  2. If I manually set $websiteId to "5", $account returns empty value

The code works fine in a model function. Any help would be appreciated.

Thanks.

1

There are 1 best solutions below

1
Abhinav Kumar Singh On

First, you need to locate the app/Mage.php file and require it in your php file. require_once 'app/Mage.php';

Add Below code in your php file:

$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
    echo $mageFilename." was not found";
    exit;
}
require_once $mageFilename;
Mage::app();