Exemplo n.º 1
0
 /**
  * Returns product class variant attribute by name and group
  *
  * @param \XLite\Model\Product $model Product
  * @param string               $name  Attribute name
  * @param string               $group Attribute group
  *
  * @return \XLite\Model\Attribute
  */
 protected function findProductClassAttribute($model, $name, $group)
 {
     $attributes = $model->getProductClass() ? $model->getProductClass()->getAttributes()->toArray() : array();
     /** @var \XLite\Model\Attribute $item */
     return array_reduce($attributes, function ($carry, $item) use($name, $group) {
         return null === $carry && $name === $item->getName() && $group === ($item->getAttributeGroup() ? $item->getAttributeGroup()->getName() : '') ? $item : $carry;
     }, null);
 }
Exemplo n.º 2
0
 /**
  * Import 'attributes' value
  *
  * @param \XLite\Model\Product $model  Product
  * @param array                $value  Value
  * @param array                $column Column info
  *
  * @return void
  */
 protected function importAttributesColumn(\XLite\Model\Product $model, array $value, array $column)
 {
     $this->multAttributes = array();
     foreach ($value as $attr => $v) {
         if (preg_match('/(.+)\\(field:(global|product|class)([ ]*>>>[ ]*(.+))?\\)(_([a-z]{2}))?/iSs', $attr, $m)) {
             $type = $m[2];
             $name = trim($m[1]);
             $lngCode = isset($m[6]) ? $m[6] : null;
             $productClass = 'class' === $type ? $model->getProductClass() : null;
             $attributeGroup = isset($m[4]) && 'product' !== $type ? $this->normalizeValueAsAttributeGroup($m[4], $productClass) : null;
             $product = 'product' === $type ? $model : null;
             $values = array();
             foreach ($v as $value) {
                 $values = array_merge($values, $value);
             }
             $values = array_values(array_unique($values));
             $data = array('value' => array(), 'default' => array(), 'price' => array(), 'weight' => array());
             $hasOptions = false;
             foreach ($values as $k => $value) {
                 if (preg_match('/(.+)=(default)?(\\/)?((w|\\$)(([+-]?\\d+\\.?\\d*)(%)?))?(\\/)?((w|\\$)(([+-]?\\d+\\.?\\d*)(%)?))?/iSs', $value, $m)) {
                     $data['value'][$k] = $m[1];
                     if (isset($m[2]) && 'default' === $m[2]) {
                         $data['default'][$k] = true;
                     }
                     $hasOptions = true;
                     foreach (array(5, 11) as $id) {
                         if (isset($m[$id])) {
                             $data['$' === $m[$id] ? 'price' : 'weight'][$k] = $m[$id + 1];
                         }
                     }
                 } else {
                     $data['value'][$k] = $value;
                 }
             }
             $data['multiple'] = 1 < count($data['value']);
             $cnd = new \XLite\Core\CommonCell();
             $cnd->product = $product;
             $cnd->productClass = $productClass;
             $cnd->attributeGroup = $attributeGroup;
             $cnd->name = $name;
             $attribute = \XLite\Core\Database::getRepo('XLite\\Model\\Attribute')->search($cnd);
             if ($attribute) {
                 $attribute = $attribute[0];
             } else {
                 $type = !$data['multiple'] && !$hasOptions ? \XLite\Model\Attribute::TYPE_TEXT : \XLite\Model\Attribute::TYPE_SELECT;
                 if (1 === count($data['value']) || 2 === count($data['value'])) {
                     $isCheckbox = true;
                     foreach ($data['value'] as $val) {
                         $isCheckbox = $isCheckbox && $this->verifyValueAsBoolean($val);
                     }
                     if ($isCheckbox) {
                         $type = \XLite\Model\Attribute::TYPE_CHECKBOX;
                     }
                 }
                 $attribute = \XLite\Core\Database::getRepo('\\XLite\\Model\\Attribute')->insert(array('name' => $name, 'productClass' => $productClass, 'attributeGroup' => $attributeGroup, 'product' => $product, 'type' => $type));
                 if ($attributeGroup && $productClass) {
                     $attributeGroup->setProductClass($productClass);
                 }
             }
             if ($data['multiple']) {
                 $this->multAttributes[$attribute->getId()] = $v;
             }
             $data['ignoreIds'] = true;
             if ($lngCode) {
                 $oldCode = $this->importer->getLanguageCode();
                 $this->importer->setLanguageCode($lngCode);
             }
             if ($attribute->getType() === \XLite\Model\Attribute::TYPE_CHECKBOX) {
                 foreach ($data['value'] as $k => $val) {
                     $data['value'][$k] = $this->normalizeValueAsBoolean($val);
                 }
             }
             $attribute->setAttributeValue($model, $data);
             if ($lngCode) {
                 \XLite\Core\Database::getEM()->flush();
                 $this->importer->setLanguageCode($oldCode);
             }
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Generate attribute values
  *
  * @param \XLite\Model\Product $product         Product
  * @param boolean              $useProductClass Use product class OPTIONAL
  *
  * @return void
  */
 public function generateAttributeValues(\XLite\Model\Product $product, $useProductClass = null)
 {
     $cnd = new \XLite\Core\CommonCell();
     $cnd->productClass = $useProductClass ? $product->getProductClass() : null;
     $cnd->product = null;
     $cnd->type = array(\XLite\Model\Attribute::TYPE_CHECKBOX, \XLite\Model\Attribute::TYPE_SELECT, \XLite\Model\Attribute::TYPE_TEXT);
     foreach ($this->search($cnd) as $a) {
         $a->addToNewProduct($product);
     }
 }
Exemplo n.º 4
0
 /**
  * This attribute is multiple or not flag
  *
  * @param \XLite\Model\Product $product Product
  *
  * @return boolean
  */
 public function isMultiple(\XLite\Model\Product $product)
 {
     $repo = \XLite\Core\Database::getRepo(static::getAttributeValueClass($this->getType()));
     return (!$this->getProduct() || $this->getProduct()->getId() == $product->getId()) && (!$this->getProductClass() || $product->getProductClass() && $this->getProductClass()->getId() == $product->getProductClass()->getId()) && 1 < count($repo->findBy(array('product' => $product, 'attribute' => $this)));
 }
Exemplo n.º 5
0
 /**
  * Is coupon valid for product
  *
  * @param \XLite\Model\Product $product Product
  *
  * @return boolean
  */
 public function isValidForProduct(\XLite\Model\Product $product)
 {
     $result = true;
     if (0 < count($this->getProductClasses())) {
         // Check product class
         $result = $product->getProductClass() && $this->getProductClasses()->contains($product->getProductClass());
     }
     if ($result && 0 < count($this->getCategories())) {
         // Check categories
         $result = false;
         foreach ($product->getCategories() as $category) {
             if ($this->getCategories()->contains($category)) {
                 $result = true;
                 break;
             }
         }
     }
     return $result;
 }
Exemplo n.º 6
0
 /**
  * {@inheritDoc}
  */
 public function getProductClass()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getProductClass', array());
     return parent::getProductClass();
 }