Example #1
0
 public function save(EntityInterface $object)
 {
     // TODO: Do we want to save variations via update button?
     if ($object instanceof Product\Variable) {
         $wpdb = $this->wp->getWPDB();
         $this->removeAllVariationsExcept($object->getId(), array_map(function ($item) {
             /** @var Product\Variable\Variation $item */
             return $item->getId();
         }, $object->getVariations()));
         foreach ($object->getVariations() as $variation) {
             /** @var Product\Variable\Variation $variation */
             if ($variation->getProduct() === null) {
                 $variation->setProduct($this->_createVariableProduct($variation, $object));
                 $variation->setId($variation->getProduct()->getId());
             }
             $this->productService->save($variation->getProduct());
             foreach ($variation->getAttributes() as $attribute) {
                 /** @var Product\Variable\Attribute $attribute */
                 $data = array('variation_id' => $variation->getId(), 'attribute_id' => $attribute->getAttribute()->getId(), 'value' => $attribute->getValue());
                 if ($attribute->exists()) {
                     $wpdb->update($wpdb->prefix . 'jigoshop_product_variation_attribute', $data, array('variation_id' => $variation->getId(), 'attribute_id' => $attribute->getAttribute()->getId()));
                 } else {
                     $wpdb->insert($wpdb->prefix . 'jigoshop_product_variation_attribute', $data);
                     $attribute->setExists(Product\Variable\Attribute::VARIATION_ATTRIBUTE_EXISTS);
                 }
             }
         }
     }
 }
Example #2
0
 public function ajaxFeatureProduct()
 {
     /** @var Product $product */
     $product = $this->productService->find((int) $_POST['product_id']);
     $product->setFeatured(!$product->isFeatured());
     $this->productService->save($product);
     echo json_encode(array('success' => true));
     exit;
 }
Example #3
0
 public function ajaxRemoveVariation()
 {
     try {
         if (!isset($_POST['product_id']) || empty($_POST['product_id'])) {
             throw new Exception(__('Product was not specified.', 'jigoshop'));
         }
         if (!is_numeric($_POST['product_id'])) {
             throw new Exception(__('Invalid product ID.', 'jigoshop'));
         }
         if (!isset($_POST['variation_id']) || empty($_POST['variation_id'])) {
             throw new Exception(__('Variation was not specified.', 'jigoshop'));
         }
         if (!is_numeric($_POST['variation_id'])) {
             throw new Exception(__('Invalid variation ID.', 'jigoshop'));
         }
         $product = $this->productService->find((int) $_POST['product_id']);
         if (!$product->getId()) {
             throw new Exception(__('Product does not exists.', 'jigoshop'));
         }
         if (!$product instanceof Product\Variable) {
             throw new Exception(__('Product is not variable - unable to add variation.', 'jigoshop'));
         }
         $variation = $product->removeVariation((int) $_POST['variation_id']);
         $this->service->removeVariation($variation);
         $this->productService->save($product);
         echo json_encode(array('success' => true));
     } catch (Exception $e) {
         echo json_encode(array('success' => false, 'error' => $e->getMessage()));
     }
     exit;
 }
Example #4
0
 public function ajaxRemoveAttribute()
 {
     try {
         if (!isset($_POST['product_id']) || empty($_POST['product_id'])) {
             throw new Exception(__('Product was not specified.', 'jigoshop'));
         }
         if (!is_numeric($_POST['product_id'])) {
             throw new Exception(__('Invalid product ID.', 'jigoshop'));
         }
         if (!isset($_POST['attribute_id']) || empty($_POST['attribute_id'])) {
             throw new Exception(__('Attribute was not specified.', 'jigoshop'));
         }
         if (!is_numeric($_POST['attribute_id'])) {
             throw new Exception(__('Invalid attribute ID.', 'jigoshop'));
         }
         /** @var \Jigoshop\Entity\Product $product */
         $product = $this->productService->find((int) $_POST['product_id']);
         if (!$product->getId()) {
             throw new Exception(__('Product does not exists.', 'jigoshop'));
         }
         $product->removeAttribute((int) $_POST['attribute_id']);
         $this->productService->save($product);
         echo json_encode(array('success' => true));
     } catch (Exception $e) {
         echo json_encode(array('success' => false, 'error' => $e->getMessage()));
     }
     exit;
 }
Example #5
0
 /**
  * Saves entity to database.
  *
  * @param $object EntityInterface Entity to save.
  */
 public function save(EntityInterface $object)
 {
     $this->queries = array();
     $this->objects[$object->getId()] = $object;
     unset($this->thumbnails[$object->getId()]);
     unset($this->productAttributes[$object->getId()]);
     $this->service->save($object);
 }