Skip to content

Instantly share code, notes, and snippets.

@michaelhoang
Last active August 29, 2015 14:20
Show Gist options
  • Save michaelhoang/7e43dedc8d531bb252ae to your computer and use it in GitHub Desktop.
Save michaelhoang/7e43dedc8d531bb252ae to your computer and use it in GitHub Desktop.
Magento Shell
// Change product attribute value
public function run()
{
// --edit 1-10,color,5
// $default = array(
// 'product_ids' => array(1, 10),
// 'attribute_code' => 'color',
// 'attribute_value' => 5
// );
if (isset($this->_args['edit'])) {
$params = explode(',', $this->_args['edit']);
$default['product_ids'] = explode('-', $params[0]);
$default['attribute_code'] = $params[1];
$default['attribute_value'] = explode('-', $params[2]);
if (count($default['attribute_value']) < 2) {
$default['attribute_value'] = $default['attribute_value'][0];
}
$default['product_ids'] = array_map('intval', $default['product_ids']);
$productId = $default['product_ids'][0];
if (!isset($default['product_ids'][1])) {
$default['product_ids'][1] = $productId;
}
while (true) {
$product = Mage::getModel('catalog/product')->load($productId);
$product->setData($default['attribute_code'], $default['attribute_value']);
$product->save();
if ($productId === $default['product_ids'][1]) {
break;
} else {
$productId++;
}
}
} else {
echo $this->usageHelp();
}
}
/**
* Get products collection by category ID
* @param array $categoryIds
* @return null|object
*/
public function getProductsCollectionByCatIds($categoryIds = array())
{
if (empty($categoryIds)) {
return null;
}
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->addAttributeToFilter('status', array('eq' => 1))
->addAttributeToFilter('category_id', array('in' => $categoryIds));
return $collection;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment