Solving Magento PHP 7 Compatibility Issues: A Comprehensive Guide
Dear Magento developers,
If you’ve recently upgraded your server to PHP 7.x and are facing the dreaded “Argument 1 passed” error when trying to set a website for a store group, this post is for you. I’ll walk you through the steps required to resolve this issue without resorting to band-aid solutions.
Understanding the Error
The error message you’re encountering is due to Magento’s core model inconsistencies that emerge with PHP 7.x versions:
Fatal error: Uncaught TypeError: Argument 1 passed to
Mage_Core_Model_Store_Group::setWebsite() must be an instance of
Mage_Core_Model_Website, null given, called in
This occurs because Magento’s core code expects a specific type for the website parameter but is receiving null.
Why Your Current Fixes Aren’t Working
You mentioned trying the Inchoo PHP 7 patch, which is a great starting point. However, this patch primarily addresses performance and compatibility issues rather than core model inconsistencies.
The main reason your fix isn’t working lies in the Mage_Core_Model_Store_Group::setWebsite() method itself:
public function setWebsite($website)
{
if (null !== $this->getWebsite()) {
$this->getWebsite()->delete();
}
Mage::dispatchEvent('store_group_delete_after',
array(
'group' => $this,
'website_id' => null
)
);
if ($website instanceof Mage_Core_Model_Website) {
$this->setWebsiteId($website->getId());
} else {
throw new Exception("Invalid value: " . print_r($website, true));
}
return $this;
}
As you can see, the method throws an exception if $website is not an instance of Mage_Core_Model_Website. The patch doesn’t address this core logic issue.
The Proper Solution
To resolve this issue permanently:
- Open your
app/code/core/Mage/Core/Model/Store/Group.phpfile. - Locate the
setWebsite()method around line 430. - Modify the method as follows:
public function setWebsite($website)
{
if (null !== $this->getWebsite()) {
$this->getWebsite()->delete();
}
Mage::dispatchEvent('store_group_delete_after',
array(
'group' => $this,
'website_id' => null
)
);
// Modify this line
if ($website instanceof Mage_Core_Model_Website || is_numeric($website)) {
$this->setWebsiteId((is_numeric($website) ? $website : $website->getId()));
} else {
throw new Exception("Invalid value: " . print_r($website, true));
}
return $this;
}
- Save the file.
This modification allows you to pass either a Mage_Core_Model_Website object or its ID as an argument to the method without throwing an exception.
Best Practices for Magento 1.x Upgrades
While you’ve fixed the immediate issue, I encourage you to consider migrating to Magento 2.x if possible. Magento 2 is designed with PHP 7.x in mind and offers significant performance improvements.
If a migration isn’t feasible immediately, ensure your server meets all Magento’s system requirements.
Conclusion
In this post, we’ve identified the root cause of the “Argument 1 passed” error when setting a website for a store group in Magento 1.9.3 with PHP 7.x.
We explored why existing patches weren’t sufficient and provided a direct modification to resolve the issue permanently.
Remember, resolving core model inconsistencies requires careful editing of the Magento codebase. Always backup your files before making changes to ensure you can revert if needed.
For more advanced Magento customizations and migrations, I recommend reaching out to our team at Laravel Company. We specialize in providing high-quality, secure solutions for e-commerce platforms like Magento.
Stay tuned for future posts on managing the complexities of legacy systems and migrating to modern, PHP 7-compatible platforms.