public function getAttribute(\Club\ShopBundle\Entity\Product $product) { $attr = new \Club\ShopBundle\Model\Attribute(); foreach ($product->getProductAttributes() as $a) { $v = 'set' . preg_replace("/_/", "", $a->getAttribute()); if ($a->getAttribute() == 'location') { $res = new \Doctrine\Common\Collections\ArrayCollection(); $locations = $this->em->getRepository('ClubUserBundle:Location')->getByIds(explode(",", $a->getValue())); foreach ($locations as $location) { $res[] = $location; } $attr->{$v}($res); } else { $attr->{$v}($a->getValue()); } } return $attr; }
private function validateAttributes(\Club\ShopBundle\Entity\Product $product) { foreach ($product->getProductAttributes() as $attr) { switch ($attr->getAttribute()) { case 'only_member': if (!$this->security_context->isGranted('IS_AUTHENTICATED_FULLY')) { throw new \Exception('You have to be logged in to buy this product'); } elseif ($attr->getValue() && !$this->club_user->isMember($this->token->getUser())) { throw new \Exception('Cannot buy due to missing subscription'); } break; case 'amount_per_member': if ($attr->getValue() > 0) { $cartProduct = $this->em->getRepository('ClubShopBundle:CartProduct')->getProductInCart($product, $this->cart); if ($cartProduct && $cartProduct->getQuantity() >= $attr->getValue()) { throw new \Exception('Cannot buy more of this item'); } if ($this->security_context->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $bought = $this->em->getRepository('ClubShopBundle:OrderProduct')->getBoughtByUser($product, $this->cart->getUser()); if (count($bought) >= $attr->getValue()) { throw new \Exception('You cannot buy anymore of this product'); } } } break; } } }