Last active
July 31, 2020 12:03
-
-
Save mauricioprado00/2b730532689d28dcdb2b to your computer and use it in GitHub Desktop.
magento product website assigner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Intellimage | |
* | |
* NOTICE OF LICENSE | |
* | |
* This source file is subject to the Open Software License (OSL 3.0) | |
* that is bundled with this package in the file LICENSE.txt. | |
* It is also available through the world-wide-web at this URL: | |
* http://www.opensource.org/licenses/osl-3.0.php | |
* If you did not receive a copy of the license and are unable to | |
* obtain it through the world-wide-web, please send an email | |
* to [email protected] so we can send you a copy immediately. | |
* | |
* DISCLAIMER | |
* | |
* Do not edit or add to this file if you wish to upgrade your | |
* Intellimage extension to newer versions in the future. | |
* If you wish to customize your Intellimage extension to your | |
* needs please refer to [email protected] for more information. | |
* | |
* @author Hugo Mauricio Prado Macat | |
* @copyright 2013 | |
* @email [email protected] I do freelance work please | |
* contact me for estimations I am an experienced magento developer | |
* @license http://www.opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
*/ | |
require_once 'abstract.php'; | |
/** | |
* @see http://stackoverflow.com/questions/4526914/how-can-i-delete-test-order-from-magento | |
*/ | |
class Mage_Shell_Custom extends Mage_Shell_Abstract | |
{ | |
protected $_dryRun = false; | |
/** | |
* Run script | |
* | |
*/ | |
public function run() | |
{ | |
$this->_dryRun = (boolean) $this->getArg('dry-run'); | |
$products = $this->_getProducts(); | |
$websites = $this->_getWebsites(); | |
$action = $this->_getAction(); | |
if ($products && $websites && $action) { | |
if (!$this->_doAction($action, $products, $websites)) { | |
echo "could not complete action\n"; | |
} else { | |
echo "action taken\nyou might want to run: \n\tphp -f shell/indexer.php -- reindexall\n"; | |
} | |
} elseif ($this->getArg('list-websites')) { | |
$this->_listWebsites(); | |
} else { | |
if (!$action) { | |
echo "invalid action\n"; | |
} | |
if (!$products) { | |
echo "missing products parameter\n"; | |
} | |
if (!$websites) { | |
echo "missing websites parameter\n"; | |
} | |
echo $this->usageHelp(); | |
} | |
echo PHP_EOL . PHP_EOL; | |
} | |
private function _listWebsites() | |
{ | |
$websites = Mage::getModel('core/website')->getCollection(); | |
foreach ($websites as $website) { | |
echo PHP_EOL; | |
foreach ($website->getData() as $code => $value) { | |
echo $code . ': ' . $value . PHP_EOL; | |
} | |
} | |
} | |
/** | |
* @return array | |
*/ | |
private function _getAction() | |
{ | |
$action = $this->getArg('action'); | |
if (!$action) { | |
$action = 'add'; | |
} | |
if (!in_array($action, array('add', 'remove'))) { | |
return null; | |
} | |
return $action; | |
} | |
/** | |
* @return array | |
*/ | |
private function _getProducts() | |
{ | |
$products = $this->getArg('products'); | |
if ($products == 'all') { | |
$products = Mage::getResourceModel('catalog/product_collection')->getAllIds(); | |
} elseif($products) { | |
$products = explode(',', $products); | |
} else { | |
$products = null; | |
} | |
return $products; | |
} | |
/** | |
* @return array | |
*/ | |
private function _getWebsites() | |
{ | |
$websites = $this->getArg('websites'); | |
if ($websites == 'all') { | |
$websites = Mage::getResourceModel('core/website_collection')->getAllIds(); | |
} elseif($websites) { | |
$websites = explode(',', $websites); | |
} else { | |
$websites = null; | |
} | |
return $websites; | |
} | |
/** | |
* @param Mage_Sales_Model_Order | |
*/ | |
private function _canDoAction($order) | |
{ | |
return true; | |
} | |
/** | |
* @param array $products | |
* @param array $websites | |
* @return boolean | |
*/ | |
private function _doAction($action, $products, $websites) | |
{ | |
try { | |
if ($action == 'add') { | |
Mage::getModel('catalog/product_website')->addProducts($websites, $products); | |
} else { | |
Mage::getModel('catalog/product_website')->removeProducts($websites, $products); | |
} | |
} catch(Exception $e) { | |
echo $e->getMessage() . PHP_EOL; | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Retrieve Usage Help Message | |
* | |
*/ | |
public function usageHelp() | |
{ | |
$file = basename(__FILE__); | |
return <<<USAGE | |
This tool allows you to add or remove products from websites | |
Usage: php -f $file -- [options] | |
--list-websites to list websites | |
--action [add|remove] by default it's "add" | |
--dry-run It will just show you the info of the order | |
--products Products Ids can be a list or "all" | |
--websites Websites Ids can be a list or "all" | |
example to add products: | |
php -f shell/$file -- --action add --products 1,2,3,4 --websites 1,2 | |
php -f shell/$file -- --products 1,2,3,4 --websites all | |
php -f shell/$file -- --products all --websites all | |
example to remove products: | |
php -f shell/$file -- --action remove --products 1,2,3,4 --websites 1,2 | |
USAGE; | |
} | |
} | |
$shell = new Mage_Shell_Custom(); | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@seansan this script only allows you to add/remove products to/from a website. Seems like you need some additional script to find out which products to which websites.
I guess you could achieve the second part just with some sql, e.g.:
for each website run something like, e.g. website 1, in upper case things you should complete:
php -f shell/product-website.php -- --action add --products $(mysql -uYOURUSER -pYOURPASSWORD YOURDB -e "select group_concat(product_id) from catalog_category_product where THE PRODUCT TYPE IS SIMPLE and THE PARENT IS CONFIGURABLE and THE PARENT IS ASSIGNED TO WEBSITE 1") --websites 1
I cannot write the sql right now from the top of my head, but those are things you could easily find out with a bit of SQL. If you need additional support I am available for hire [email protected]