Skip to content

Instantly share code, notes, and snippets.

@mauricioprado00
Last active July 31, 2020 12:03
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioprado00/2b730532689d28dcdb2b to your computer and use it in GitHub Desktop.
Save mauricioprado00/2b730532689d28dcdb2b to your computer and use it in GitHub Desktop.
magento product website assigner
<?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 mauricioprado00@gmail.com 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 mauricioprado00@gmail.com for more information.
*
* @author Hugo Mauricio Prado Macat
* @copyright 2013
* @email mauricioprado00@gmail.com 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();
@igabc
Copy link

igabc commented Jan 4, 2016

Nice script :)
How to add ONLY the products of a specific ROOT category?

@gnysek
Copy link

gnysek commented Jan 15, 2016

@igabc: you should add filter on line 122, where product collection is loaded. That way you will get only products you want. Or you can load IDs from "catalog_category_product".

@mauricioprado00
Copy link
Author

@igabc you could do something like:

php -f shell/product-website.php -- --action add --products $(mysql -uYOURUSER -pYOURPASSWORD YOURDB -e "select group_concat(product_id) from catalog_category_product where category_id = X") --websites 1,2

that should do it.

@emiliano1
Copy link

Great, job although I found this late and had it done in SQL. But I will star it 👍

@seansan
Copy link

seansan commented Jul 31, 2020

Hi! FOund this in 2020 ;) We have the problem where PARENT (configurable) products have a certain websites scope set (e.g. (1) or (1,3,5)) and the child products are not set as the SAME.

Is there a quick method to add that could do this? And maybe preventing/skipping from updating when the scope between parent and simple is already the same

So we have
Parent_sku websites 1,2,3
Child_sku1 websites 1
Child_sku2 websites 2

And this should become
Parent_sku websites 1,2,3
Child_sku1 websites 1,2,3
Child_sku2 websites 1,2,3

@seansan
Copy link

seansan commented Jul 31, 2020

And another question also :)

Suppose we would run this frequently to update a certain product selection, say all products (parent/configurable and child/simple) that belong to brand X and set the websites to (1,2,3) - how would we do that?

(remember brand/manufacturer is only set at the parent/configurable level)

Cool! Glad I found this .. help appreciated

@mauricioprado00
Copy link
Author

mauricioprado00 commented Jul 31, 2020

And another question also :)

Suppose we would run this frequently to update a certain product selection, say all products (parent/configurable and child/simple) that belong to brand X and set the websites to (1,2,3) - how would we do that?

(remember brand/manufacturer is only set at the parent/configurable level)

Cool! Glad I found this .. help appreciated

@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 mauricioprado00@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment