_name = __('Create Product', 'ai-copilot-content-generator'); $this->_desc = __('Only simple products can be created.', 'ai-copilot-content-generator'); $this->setBlock($block); } public function getSettings() { if (empty($this->_settings)) { $this->setSettings(); } return $this->_settings; } public function setSettings() { $this->_settings = array( 'title' => array( 'type' => 'input', 'label' => __('Product Name *', 'ai-copilot-content-generator'), 'default' => '', 'variables' => true, ), 'description' => array( 'type' => 'textarea', 'label' => __('Description', 'ai-copilot-content-generator'), 'default' => '', 'rows' => 3, 'html' => true, 'variables' => true, ), 'short_desc' => array( 'type' => 'textarea', 'label' => __('Short Description', 'ai-copilot-content-generator'), 'default' => '', 'rows' => 3, 'html' => true, 'variables' => true, ), 'regular_price' => array( 'type' => 'input', 'label' => __('Regular price (use +,-,*)', 'ai-copilot-content-generator'), 'default' => '', 'variables' => true, ), 'sale_price' => array( 'type' => 'input', 'label' => __('Sale price (use +,-,*)', 'ai-copilot-content-generator'), 'default' => '', 'variables' => true, ), 'stock_quantity' => array( 'type' => 'input', 'label' => __('Stock quantity (use +,-,*)', 'ai-copilot-content-generator'), 'default' => '', 'variables' => true, ), 'stock_status' => array( 'type' => 'select', 'label' => __('Stock Status', 'ai-copilot-content-generator'), 'default' => 'instock', 'options' => $this->getStockStatusesList(), ), 'sku' => array( 'type' => 'input', 'label' => __('SKU', 'ai-copilot-content-generator'), 'default' => '', 'variables' => true, ), 'status' => array( 'type' => 'select', 'label' => __('Publishing Status', 'ai-copilot-content-generator'), 'default' => 'publish', 'options' => get_post_statuses(), ), ); } public function getVariables() { if (empty($this->_variables)) { $this->setVariables(); } return $this->_variables; } public function setVariables() { $this->_variables = $this->getProductVariables(); return $this->_variables; } public function getResults( $taskId, $variables, $step = 0 ) { if (!WaicUtils::isWooCommercePluginActivated()) { $this->_results = array('result' => array(), 'error' => 'WooCommerce is not activated', 'status' => 7); return $this->_results; } $error = ''; $productObj = new WC_Product_Simple(); $title = $this->replaceVariables($this->getParam('title'), $variables); if (empty($title)) { $error = 'Product title needed'; } if (empty($error)) { $productObj->set_name(htmlspecialchars_decode($title, ENT_QUOTES)); $description = $this->getParam('description'); if (!empty($description)) { $productObj->set_description(htmlspecialchars_decode($this->replaceVariables($description, $variables), ENT_QUOTES)); } $short = $this->getParam('short_desc'); if (!empty($short)) { $productObj->set_short_description(htmlspecialchars_decode($this->replaceVariables($short, $variables), ENT_QUOTES)); $needUpdate = true; } $sku = $this->getParam('sku'); if (!empty($sku)) { $productObj->set_sku($this->replaceVariables($sku, $variables)); } $stockStatus = $this->getParam('stock_status'); if (!empty($stockStatus)) { $productObj->set_stock_status($this->replaceVariables($stockStatus, $variables)); } $status = $this->getParam('status'); if (!empty($status)) { $productObj->set_status($this->replaceVariables($status, $variables)); } } if (empty($error)) { $regularPrice = $this->getParam('regular_price'); if (!empty($regularPrice)) { $regularPrice = $this->replaceVariables($regularPrice, $variables); $price = $this->calcExpression($regularPrice); if (false === $price) { $error = 'Error Regular Price Expression: ' . $regularPrice; } else { $productObj->set_regular_price((float) $price); } } } if (empty($error)) { $salePrice = $this->getParam('sale_price'); if (!empty($salePrice)) { $salePrice = $this->replaceVariables($salePrice, $variables); $price = $this->calcExpression($salePrice); if (false === $price) { $error = 'Error Sale Price Expression: ' . $salePrice; } else { $productObj->set_sale_price((float) $price); } } } if (empty($error)) { $stockQuantity = $this->getParam('stock_quantity'); if (!empty($stockQuantity)) { $stockQuantity = $this->replaceVariables($stockQuantity, $variables); $quantity = $this->calcExpression($stockQuantity); if (false === $quantity) { $error = 'Error Stock Quantity Expression: ' . $stockQuantity; } else { $productObj->set_manage_stock(true); $productObj->set_stock_quantity((int) $quantity); } } } $productId = empty($error) ? $productObj->save() : 0; $this->_results = array( 'result' => array('waic_product_id' => (int) $productId), 'error' => $error, 'status' => empty($error) ? 3 : 7, ); return $this->_results; } }