public function __construct($controller, $name = "VariationForm")
 {
     parent::__construct($controller, $name);
     $product = $controller->data();
     $farray = array();
     $requiredfields = array();
     $attributes = $product->VariationAttributeTypes();
     foreach ($attributes as $attribute) {
         $attributeDropdown = $attribute->getDropDownField(_t('VariationForm.ChooseAttribute', "Choose {attribute} …", '', array('attribute' => $attribute->Label)), $product->possibleValuesForAttributeType($attribute));
         if ($attributeDropdown) {
             $farray[] = $attributeDropdown;
             $requiredfields[] = "ProductAttributes[{$attribute->ID}]";
         }
     }
     $fields = FieldList::create($farray);
     if (self::$include_json) {
         $vararray = array();
         $query = $query2 = new SQLQuery();
         $query->setSelect('ID')->setFrom('ProductVariation')->addWhere(array('ProductID' => $product->ID));
         if (!Product::config()->allow_zero_price) {
             $query->addWhere('"Price" > 0');
         }
         foreach ($query->execute()->column('ID') as $variationID) {
             $query2->setSelect('ProductAttributeValueID')->setFrom('ProductVariation_AttributeValues')->setWhere(array('ProductVariationID' => $variationID));
             $vararray[$variationID] = $query2->execute()->keyedColumn();
         }
         $fields->push(HiddenField::create('VariationOptions', 'VariationOptions', json_encode($vararray)));
     }
     $fields->merge($this->Fields());
     $this->setFields($fields);
     $requiredfields[] = 'Quantity';
     $this->setValidator(VariationFormValidator::create($requiredfields));
     $this->extend('updateVariationForm');
 }
 /**
  * Update the list of export fields for products
  *
  * @return array
  */
 public function getExportFields()
 {
     if ($this->modelClass == 'Product') {
         return Product::config()->export_fields;
     } else {
         return singleton($this->modelClass)->summaryFields();
     }
 }
 public function testCanPurchase()
 {
     $this->assertTrue($this->tshirt->canPurchase());
     $this->assertTrue($this->socks->canPurchase());
     $this->assertFalse($this->beachball->canPurchase(), "beach ball has AllowPurchase flag to 0");
     $this->assertFalse($this->pdfbrochure->canPurchase(), "pdf brochure has 0 price");
     //allow 0 prices
     Product::config()->allow_zero_price = true;
     $this->assertTrue($this->pdfbrochure->canPurchase());
     //disable purchasing globally
     Product::config()->global_allow_purchase = false;
     $this->assertFalse($this->tshirt->canPurchase());
     Product::config()->allow_zero_price = false;
     Product::config()->global_allow_purchase = true;
 }
 /**
  * Get all the {@link ProductAttributeValue} for a given attribute type,
  * based on this product's variations.
  *
  * @return DataList
  */
 public function possibleValuesForAttributeType($type)
 {
     if (!is_numeric($type)) {
         $type = $type->ID;
     }
     if (!$type) {
         return null;
     }
     $list = ProductAttributeValue::get()->innerJoin("ProductVariation_AttributeValues", "\"ProductAttributeValue\".\"ID\" = \"ProductVariation_AttributeValues\".\"ProductAttributeValueID\"")->innerJoin("ProductVariation", "\"ProductVariation_AttributeValues\".\"ProductVariationID\" = \"ProductVariation\".\"ID\"")->where("TypeID = {$type} AND \"ProductVariation\".\"ProductID\" = " . $this->owner->ID);
     if (!Product::config()->allow_zero_price) {
         $list = $list->where('"ProductVariation"."Price" > 0');
     }
     return $list;
 }
Example #5
0
 public function canPurchase($member = null, $quantity = 1)
 {
     $allowpurchase = false;
     if ($product = $this->Product()) {
         $allowpurchase = ($this->sellingPrice() > 0 || Product::config()->allow_zero_price) && $product->AllowPurchase;
     }
     $extended = $this->extendedCan('canPurchase', $member, $quantity);
     if ($allowpurchase && $extended !== null) {
         $allowpurchase = $extended;
     }
     return $allowpurchase;
 }
 public function canPurchase($member = null, $quantity = 1)
 {
     $allowpurchase = false;
     if ($product = $this->Product()) {
         $allowpurchase = ($this->sellingPrice() > 0 || Product::config()->allow_zero_price) && $product->AllowPurchase;
     }
     $permissions = $this->extend('canPurchase', $member, $quantity);
     $permissions[] = $allowpurchase;
     return min($permissions);
 }