1

FINAL EDIT (resolved)

A big thanks to Marius for answering this question. With his help and some information from this question, I was able to piece together a working solution. Here is the final (working) code block:

public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
    $cust = $this->getCustomer();
    $custId = $cust->getId();
    $vat = $cust->getData('taxvat');

    $custAddress = Mage::getModel('customer/address')->load($cust->default_shipping);
    $custState = $custAddress->getData('region');

    $customerLoggedIn = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getRegion();
    $taxRate = $taxRate / 100;

    if ($priceIncludeTax) {
        $amount = $price * (1 - 1 / (1 + $taxRate));
    } else if (strtoupper($custState) === 'FLORIDA' || strtoupper($customerLoggedIn) === 'FLORIDA') {
        if ($vat) {
            $amount = $price;
        } else {
            $amount = $price * $taxRate;
        }
    } else {
        $amount = 0;
    }

    if ($round) {
        return $this->round($amount);
    }

    return $amount;
}

Original Question

As seen in this question (self-answered), I am trying to code a custom condition into my Tax setup.

This is working perfectly on the frontend and it was working on the backend earlier. I haven't changed the code, so I'm rather at a loss to why it isn't working now.

How do I load the Customer ID when I am on the Create New Order page?

The method I was using successfully is no longer working with my error log saying:

Undefined variable: customerId  in /app/code/local/Mage/Tax/Model/Calculation.php on line 658

Line 658:

$cust = Mage::getModel('customer/customer')->load($customerId);

This is within the Tax/Model/Calculation.php file, so it references this code block:

public function getCustomer()
{
    if ($this->_customer === null) {
        $session = Mage::getSingleton('customer/session');
        if ($session->isLoggedIn()) {
            $this->_customer = $session->getCustomer();
        } elseif ($session->getCustomerId()) {
            $this->_customer = Mage::getModel('customer/customer')->load($session->getCustomerId());
        } else {
            $this->_customer = false;
        }
    }
    return $this->_customer;
}

The method I am modifying is calcTaxAmount:

public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
    $cust = Mage::getModel('customer/customer')->load($customerId);
    $vat = $cust->getData('taxvat');

    $customerLoggedIn = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getRegion();
    $taxRate = $taxRate / 100;

    if ($priceIncludeTax) {
        $amount = $price * (1 - 1 / (1 + $taxRate));
    } else if (strtoupper($customerLoggedIn) === 'FLORIDA') {
        if ($vat) {
            $amount = $price;
        } else {
            $amount = $price * $taxRate;
        }
    } else {
        $amount = 0;
    }

    if ($round) {
        return $this->round($amount);
    }

    return $amount;
}
6
  • you just need the customer id when create new order right? Jun 24 '15 at 4:37
  • Well, I need the customer ID in order to retrieve the Shipping Address's State. I just need to see if it is being shipped to Florida.
    – travisw
    Jun 24 '15 at 12:02
  • check the customer registry if any when create order Jun 24 '15 at 12:06
  • How would I go about that programmatically?
    – travisw
    Jun 24 '15 at 12:25
  • can you post the lines above 658?
    – Marius
    Jun 24 '15 at 13:47
2

Instead of

$cust = Mage::getModel('customer/customer')->load($customerId);

Try using this:

$cust = $this->getCustomer();

but be careful. $cust may be null in some cases.

On a side note, this does not look good:

$custAdmin = Mage::getSingleton('customer/session')->getCustomer();
$custAdminId = $custAdmin->getId();
$region = Mage::getModel('directory/region')->load($custAdminId);
$stateCode = $region->getCode();
$stateName = $region->getName();

you are basically getting the customer id from the session then, loading the region with the same id as the customer.
I'm sure this is not what you want to do.

1
  • It looks like we're in business now. And yes, the code block you quoted there is terrible and it's actually not doing anything (this variables are processed, but never used) so I deleted it. I'm going to update my question to be a little more concise for future visitors. Thanks for your help!
    – travisw
    Jun 24 '15 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.