0

I know this question is asked like a hundred times in hundred different ways. But I just can't get this particular one to work. I am really confused.

This is the very beginning of my script:

<?php

class API {

    protected static $message, $data, $status, $session, $token, $dbo, $app;

    const FAIL = 0;
    const SUCCESS = 1;
    const INCOMPLETE_ARGS = 2;
    const GROUP_ALLOW = array(5);

    public static function Init() {
        global $app;
        self::$status = false;
        self::$message = "Invalid request.";
        self::$data = "";
    }

    public static function Render() {
        echo preg_replace("/\\\\u([0-9abcdef]{4})/", "&#x$1;", json_encode(array(
            "status" => self::$status,
            "message" => self::$message,
            "data" => self::$data))
        );
    }

Then there is this function:

      public static function Login() {
      $email = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : "";
                $password = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : "";

                require_once ("../app/Mage.php");
                umask(0);
                ob_start();
                session_start();
                Mage::app('default');
                Mage::getSingleton("core/session", array("name" => "frontend"));
                $b2bwebsiteid = Mage::getStoreConfig('base/general/b2bwebsite');
                //$websiteId            = Mage::app()->getWebsite()->getId();
                //$store                = Mage::app()->getStore();

                $customer = Mage::getModel("customer/customer");
                $customer->website_id = $b2bwebsiteid;
                $customer->loadByEmail($email);
                $countryCodeBE = $customer->getDefaultBillingAddress()->getCountry();
                $countrybe = Mage::getModel('directory/country')->loadByCode($countryCodeBE);
                $countryid = $countrybe->getCountryId();
    .....
    .....

And in between there is a lot more code and functions.

Now a bit further, there is another function:

public static function ProductInfoNew() {

And now the thing I want, is to load the variable $countryid from that first function in the ProductInfoNew function so that I can do something like this:

if ($countryid == "BE") {
                $groupid = 6;
            }
            else {
                $groupid = 4;
            }

But the output always seems to be NULL or Empty.

I tried setting the $countryid variable as global, static, tried to load the variable with self:: and I tried so many things but I can't get it to work.

Does anyone know the correct solution to do this? Thanks a lot in advance.

3
  • In your question, the part where you say "Then there is this function:" and you put a bunch of code underneath.. where is the function? I just see a bunch of code. Commented Jan 30, 2017 at 23:42
  • Ah correct, sorry. It's there now Commented Jan 30, 2017 at 23:50
  • Either pass variable as parameter to function or make it static inside API class. Just remember to use correct syntax for it. It should be like class_name::$var_name. Commented Jan 30, 2017 at 23:50

1 Answer 1

1

$countryid should be a field inside of the class you are using.

class API {

    private static $country_id;

    public static function ProductInfoNew() {
        if (self::$country_id == "BE") {
            $groupid = 6;
        } else {
            $groupid = 4;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It still shows output NULL.. should it be $country_id or $countryid? Tried both by the way but no success..
@ErjenRijnders Please do a var_dump($country_id) and tell us what it says. Also, make sure that you are setting it at some point prior to referencing its value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.