Ejemplo n.º 1
0
 public function ajaxSetImageVariation()
 {
     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'));
         }
         if (!isset($_POST['image_id']) || !is_numeric($_POST['image_id'])) {
             throw new Exception(__('Image is not not specified.', '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'));
         }
         if (!$product->hasVariation((int) $_POST['variation_id'])) {
             throw new Exception(__('Variation does not exists.', 'jigoshop'));
         }
         $this->wp->setPostThumbnail($_POST['variation_id'], $_POST['image_id']);
         if ($_POST['image_id'] > 0) {
             $url = $this->wp->wpGetAttachmentImageSrc($_POST['image_id'], Options::IMAGE_SMALL);
         } else {
             $url = \JigoshopInit::getUrl() . '/assets/images/placeholder.png';
         }
         echo json_encode(array('success' => true, 'url' => $url));
     } catch (Exception $e) {
         echo json_encode(array('success' => false, 'error' => $e->getMessage()));
     }
     exit;
 }