public function ajaxAddVariation() { 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')); } $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 = $this->factory->createVariation($product); $this->wp->doAction('jigoshop\\admin\\product_variation\\add', $variation); $product->addVariation($variation); $this->productService->save($product); $types = array(); foreach ($this->allowedSubtypes as $type) { /** @var $type Type */ $types[$type->getId()] = $type->getName(); } $taxClasses = array(); foreach ($this->options->get('tax.classes') as $class) { $taxClasses[$class['class']] = $class['label']; } echo json_encode(array('success' => true, 'html' => Render::get('admin/product/box/variations/variation', array('variation' => $variation, 'attributes' => $product->getVariableAttributes(), 'allowedSubtypes' => $types, 'taxClasses' => $taxClasses)))); } catch (Exception $e) { echo json_encode(array('success' => false, 'error' => $e->getMessage())); } exit; }
/** * Finds and fetches variation for selected product and variation ID. * * @param Product\Variable $product Parent product. * @param int $variationId Variation ID. * * @return Product\Variable\Variation The variation. */ public function find(Product\Variable $product, $variationId) { return $this->factory->getVariation($product, $variationId); }