/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { parent::execute($ids, $storeId, $val); $hlp = Mage::helper('ampaction'); if (!preg_match('/^[+-][0-9]+(\\.[0-9]+)?%?$/', $val)) { throw new Exception($hlp->__('Please provide the difference as +12.5, -12.5, +12.5% or -12.5%')); } $sign = substr($val, 0, 1); $val = substr($val, 1); $percent = '%' == substr($val, -1, 1); if ($percent) { $val = substr($val, 0, -1); } $val = floatval($val); if ($val < 1.0E-5) { throw new Exception($hlp->__('Please provide a non empty difference')); } $attrCode = $this->_getAttrCode(); $this->_updateAttribute($attrCode, $ids, $storeId, array('sign' => $sign, 'val' => $val, 'percent' => $percent)); if (version_compare(Mage::getVersion(), '1.4.1.0') > 0) { $obj = new Varien_Object(); $obj->setData(array('product_ids' => array_unique($ids), 'attributes_data' => array($attrCode => true), 'store_id' => $storeId)); // register mass action indexer event Mage::getSingleton('index/indexer')->processEntityAction($obj, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_MASS_ACTION); } $success = $hlp->__('Total of %d products(s) have been successfully updated', count($ids)); return $success; }
/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { $success = ''; $hlp = Mage::helper('ampaction'); if (!$this->isMultiWay()) { $success = parent::execute($ids, $storeId, $val); } elseif (!is_array($ids)) { throw new Exception($hlp->__('Please select product(s)')); } $vals = array(); if (!$this->isMultiWay()) { $vals = explode(',', $val); foreach ($vals as $val) { $mainId = intVal(trim($val)); if (!$mainId) { throw new Exception($hlp->__('Please provide a valid product ID')); } } } $num = 0; if ($this->isTwoWay()) { foreach ($vals as $mainId) { foreach ($ids as $id) { $num += $this->_createNewLink($mainId, $id); $num += $this->_createNewLink($id, $mainId); } } } elseif ($this->isMultiWay()) { foreach ($ids as $id) { foreach ($ids as $id2) { if ($id == $id2) { continue; } $num += $this->_createNewLink($id, $id2); } } } else { // default one-way relation foreach ($vals as $mainId) { foreach ($ids as $id) { if (Mage::getStoreConfig('ampaction/links/' . $this->_type . '_reverse')) { $num += $this->_createNewLink($id, $mainId); } else { $num += $this->_createNewLink($mainId, $id); } } } } if ($num) { if (1 == $num) { $success = $hlp->__('Product association has been successfully added.'); } else { $success = $hlp->__('%d product associations have been successfully added.', $num); } } return $success; }
/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { $success = parent::execute($ids, $storeId, $val); $hlp = Mage::helper('ampaction'); $catIds = explode(',', trim($val)); if (!is_array($catIds)) { throw new Exception($hlp->__('Please provide comma separated category IDs')); } if ('replacecategory' == $this->_type) { // remove product(s) from all categories $db = Mage::getSingleton('core/resource')->getConnection('core_write'); $table = Mage::getSingleton('core/resource')->getTableName('catalog/category_product'); $db->delete($table, array('product_id IN(?)' => $ids)); $this->_type = 'addcategory'; } $numAffectedCats = 0; $allAffectedProducts = array(); foreach ($catIds as $categoryId) { $category = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryId); if (!$category->getId()) { $this->_errors[] = $hlp->__('ID = `%s` has been skipped', $categoryId); continue; } $positions = $category->getProductsPosition(); $currentAffectedProducts = array(); foreach ($ids as $productId) { $has = isset($positions[$productId]); if ('addcategory' == $this->_type && !$has) { // add only new $positions[$productId] = 0; $currentAffectedProducts[] = $productId; } elseif ('removecategory' == $this->_type && $has) { //remove only existing unset($positions[$productId]); $currentAffectedProducts[] = $productId; } } $category->setPostedProducts($positions); try { $_FILES['image'] = array(); $_FILES['thumbnail'] = array(); $category->save(); ++$numAffectedCats; $allAffectedProducts = array_merge($allAffectedProducts, $currentAffectedProducts); $allAffectedProducts = array_unique($allAffectedProducts); } catch (Exception $e) { $this->_errors[] = $hlp->__('Can not handle the category ID=%s, the error is: %s', $categoryId, $e->getMessage()); } } if ($numAffectedCats) { $success = $hlp->__('Total of %d categories(s) and %d products(s) have been successfully updated.', $numAffectedCats, count($allAffectedProducts)); } return $success; }
/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { $success = parent::execute($ids, $storeId, $val); $hlp = Mage::helper('ampaction'); $srcId = intVal($val); $collection = $this->_getCollection($val); if (!count($collection)) { throw new Exception($hlp->__('Please provide a product with custom options')); } $options = array(); $countOptions = $collection->getSize(); foreach ($collection as $option) { $options[] = $this->_convertToArray($option); } $num = 0; foreach ($ids as $id) { if ($srcId == $id) { continue; } try { $product = Mage::getModel('catalog/product'); /* @var Mage_Catalog_Model_Product */ $product->reset()->load($id)->setIsMassupdate(true)->setExcludeUrlRewrite(true); // set new $product->setProductOptions($options); $product->setCanSaveCustomOptions(!$product->getOptionsReadonly()); // delete old $option = $product->getOptionInstance(); $optionsCollection = $this->_getCollection($id); $optionsCollection->walk('delete'); $option->unsetOptions(); $product->save(); $product->reset()->load($srcId); $option = $product->getOptionInstance(); $option->getResource()->duplicate($option, $srcId, $id); $this->_clean($id, $countOptions); ++$num; } catch (Exception $e) { $this->_errors[] = $hlp->__('Can not copy the options to the product ID=%d, the error is: %s', $id, $e->getMessage()); } } if ($num) { $success = $hlp->__('Total of %d products(s) have been successfully updated.', $num); } return $success; }
/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { $success = parent::execute($ids, $storeId, $val); $hlp = Mage::helper('ampaction'); $fromId = intVal(trim($val)); if (!$fromId) { throw new Exception($hlp->__('Please provide a valid product ID')); } if (in_array($fromId, $ids)) { throw new Exception($hlp->__('Please remove source product from the selected products')); } $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($fromId); if (!$product->getId()) { throw new Exception($hlp->__('Please provide a valid product ID')); } // check attributes $codes = Mage::getStoreConfig('ampaction/general/attr'); if (!$codes) { throw new Exception($hlp->__('Please set attribute codes in the module configuration')); } $config = array(); $codes = explode(',', $codes); foreach ($codes as $code) { $code = trim($code); $attribute = Mage::getModel('catalog/product')->getResource()->getAttribute($code); if (!$attribute || !$attribute->getId()) { throw new Exception($hlp->__('There is no product attribute with code `%s`, please compare values in the module configuration with catalog > attibutes > manage attributes section.', $code)); } if ($attribute->getIsUnique()) { throw new Exception($hlp->__('Attribute `%s` is unique and can not be copied. Please remove the code in the module configuration.', $code)); } $type = $attribute->getBackendType(); if ('static' == $type) { throw new Exception($hlp->__('Attribute `%s` is static and can not be copied. Please remove the code in the module configuration.', $code)); } if (!isset($config[$type])) { $config[$type] = array(); } $config[$type][] = $attribute->getId(); } // we do not use store id as it is global action $this->_copyData($fromId, $ids, $config); $success = $hlp->__('Attributes have been successfully copied.'); return $success; }
/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { $success = parent::execute($ids, $storeId, $val); $hlp = Mage::helper('ampaction'); $num = 0; foreach ($ids as $productId) { try { Mage::getSingleton('catalog/product')->unsetData()->setStoreId($storeId)->load($productId)->setAttributeSetId($val)->setIsMassupdate(true)->save(); ++$num; } catch (Exception $e) { $this->errors[] = $hlp->__('Can not change the attribute set for product ID %d, error is:', $e->getMessage()); } } Mage::dispatchEvent('catalog_product_massupdate_after', array('products' => $ids)); if ($num) { $success = $hlp->__('Total of %d products(s) have been successfully updated.', $num); } return $success; }
/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { $success = parent::execute($ids, $storeId, $val); $hlp = Mage::helper('ampaction'); $mainId = intVal(trim($val)); if (!$mainId) { throw new Exception($hlp->__('Please provide a valid product ID')); } $num = 0; if ($this->isTwoWay()) { foreach ($ids as $id) { $num += $this->_createNewLink($mainId, $id); $num += $this->_createNewLink($id, $mainId); } } elseif ($this->isMultiWay()) { $ids[] = $mainId; foreach ($ids as $id) { foreach ($ids as $id2) { if ($id == $id2) { continue; } $num += $this->_createNewLink($id, $id2); } } } else { // default one-way relation foreach ($ids as $id) { $num += $this->_createNewLink($mainId, $id); } } if ($num) { if (1 == $num) { $success = $hlp->__('Product association has been successfully added.'); } else { $success = $hlp->__(' %d product associations have been successfully added.', $num); } } return $success; }
/** * Executes the command * * @param array $ids product ids * @param int $storeId store id * @param string $val field value * @return string success message if any */ public function execute($ids, $storeId, $val) { $success = parent::execute($ids, $storeId, $val); $hlp = Mage::helper('ampaction'); $fromId = intVal(trim($val)); if (!$fromId) { throw new Exception($hlp->__('Please provide a valid product ID')); } if (in_array($fromId, $ids)) { throw new Exception($hlp->__('Please remove source product from the selected products')); } $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($fromId); if (!$product->getId()) { throw new Exception($hlp->__('Please provide a valid product ID')); } // we do not use store id as it is a global action; $attribute = $product->getResource()->getAttribute('media_gallery'); foreach ($ids as $id) { $this->_copyData($attribute->getId(), $fromId, $id, $product); } $success = $hlp->__('Images and labels have been successfully copied.'); return $success; }