public function isSalable() { Mage::dispatchEvent('catalog_product_is_salable_before', array('product' => $this)); $salable = $this->isAvailable(); $object = new Varien_Object(array('product' => $this, 'is_salable' => $salable)); Mage::dispatchEvent('catalog_product_is_salable_after', array('product' => $this, 'salable' => $object)); $_product = Mage::getModel('catalog/product')->load($this->getId()); $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId(); $restrictedCustomerGroups = $_product->getRestrictGroups(); if (empty($restrictedCustomerGroups) || count($restrictedCustomerGroups) == 1 && ($restrictedCustomerGroups[0] == '0' || $restrictedCustomerGroups[0] == '')) { $allowAddToCart = true; } else { if (in_array($groupId, $restrictedCustomerGroups)) { $allowAddToCart = false; } else { $allowAddToCart = true; } } if (!$allowAddToCart) { $object->setIsSalable($allowAddToCart); } return $object->getIsSalable(); }
/** * Validate product attrbute value for condition * * @param Varien_Object $object * @return bool */ public function validate(Varien_Object $object) { $attrCode = $this->getAttribute(); switch ($attrCode) { case 'is_salable': $value = $object->isSalable(); $object->setIsSalable($value); return $this->validateAttribute($value); break; case 'status_parent': $parentProduct = Mage::getSingleton('feedexport/feed_generator_pattern_product')->getParentProduct($object); $value = $parentProduct->getStatus(); return $this->validateAttribute($value); break; case 'image': case 'small_image': case 'thumbnail': $value = $object->getData($attrCode); if ('' === $value || 'no_selection' === $value) { $value = null; } return $this->validateAttribute($value); break; case 'category_ids': return $this->validateAttribute($object->getAvailableInCategories()); break; case 'qty': if ($object->getTypeId() == 'configurable') { $totalQty = 0; $childs = $object->getTypeInstance()->getChildrenIds($object->getId()); $childs = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array('in' => $childs[0]))->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id = entity_id', '{{table}}.stock_id = 1', 'left'); foreach ($childs as $child) { # if product enabled if ($child->getStatus() == 1) { $totalQty += $child->getQty(); } } return $this->validateAttribute($totalQty); } else { $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($object->getId()); return $this->validateAttribute($stockItem->getQty()); } break; case 'is_in_stock': $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($object->getId()); return $this->validateAttribute($stockItem->getIsInStock()); break; case 'manage_stock': $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($object->getId()); $m = 0; if ($stockItem->getManageStock()) { $m = 1; } return $this->validateAttribute($m); break; case 'image_size': case 'small_image_size': case 'thumbnail_size': $imageCode = str_replace('_size', '', $attrCode); $imagePath = $object->getData($imageCode); $path = Mage::getBaseDir('media') . DS . 'catalog/product' . $imagePath; $size = 0; if (file_exists($path) && is_file($path)) { $size = filesize($path); } return $this->validateAttribute($size); break; case 'php': $object = $object->load($object->getId()); extract($object->getData()); $expr = 'return ' . $this->getValue() . ';'; $value = eval($expr); if ($this->getOperator() == '==') { return $value; } else { return !$value; } break; default: if (!isset($this->_entityAttributeValues[$object->getId()])) { $attr = $object->getResource()->getAttribute($attrCode); if ($attr && $attr->getBackendType() == 'datetime' && !is_int($this->getValue())) { $this->setValue(strtotime($this->getValue())); $value = strtotime($object->getData($attrCode)); return $this->validateAttribute($value); } if ($attr && $attr->getFrontendInput() == 'multiselect') { $value = $object->getData($attrCode); $value = strlen($value) ? explode(',', $value) : array(); return $this->validateAttribute($value); } return parent::validate($object); } else { $result = false; // any valid value will set it to TRUE $oldAttrValue = $object->hasData($attrCode) ? $object->getData($attrCode) : null; foreach ($this->_entityAttributeValues[$object->getId()] as $storeId => $value) { $attr = $object->getResource()->getAttribute($attrCode); if ($attr && $attr->getBackendType() == 'datetime') { $value = strtotime($value); } else { if ($attr && $attr->getFrontendInput() == 'multiselect') { $value = strlen($value) ? explode(',', $value) : array(); } } $object->setData($attrCode, $value); $result |= parent::validate($object); if ($result) { break; } } if (is_null($oldAttrValue)) { $object->unsetData($attrCode); } else { $object->setData($attrCode, $oldAttrValue); } return (bool) $result; } break; } }