Skip to content

Instantly share code, notes, and snippets.

@ozzpy
Created September 29, 2020 21:39
Show Gist options
  • Save ozzpy/1f2ec014449b77883ac35766fe94d994 to your computer and use it in GitHub Desktop.
Save ozzpy/1f2ec014449b77883ac35766fe94d994 to your computer and use it in GitHub Desktop.
addWoo
/**
* addProductWoo - approved
*/
if( $_filter && $_filter=='addProductWoo' ) {
$id = isset( $_dataPost['id']) ? $_dataPost['id'] : null;
try {
$productId = $decrypto->block(base64_decode($id));
$row = $this->ecomProductsTable->getByIdWithUser((int)$productId);
$row = $this->prepareProductRow($row);
$woocommerce = new Client(
self::urlApiWoo,
self::ck,
self::cs, [
'version' => 'wc/v3',
'verify_ssl' => true,
'timeout' => 7200
]
);
$endpoint = "products";
// images
$imgUrl = "https://app.conexaob.com";
$endpointImg = "/images/uploads/images/";
$imagesProductList = [];
foreach ( $row->ecom_products_images as $img ) {
$imgName = str_replace('O_','T_450_',$imgUrl.$endpointImg.$img->images_name);
array_push($imagesProductList,['src' => $imgName]);
}
$productNameSlug = str_replace("","", strtolower($this->funcs->prepareFilename($row->ecom_products_name)));
$dimensions = $row->ecom_products_dimensions;
$data = [
'name' => $row->ecom_products_name,
'slug' => $productNameSlug,
'type' => 'simple',
'regular_price' => $row->ecom_products_price,
'description' => $row->ecom_products_description,
'short_description' => "",
'stock_quantity' => $row->ecom_products_stock,
'status' => 'private', // pending
//'catalog_visibility' => 'invisible', // visible
'sku' => $productNameSlug,
'categories' => [
[
'id' => 15
],
],
//'images' => $imagesProductList,
];
$responseWoo = $woocommerce->post($endpoint, $data);
if ( $responseWoo->id ) {
// add variations
$dataVariation = [
'weight' => $dimensions->weight,
'dimensions' => [
'length' => $dimensions->length,
'width' => $dimensions->width,
'height' => $dimensions->height
]
];
$responseWooV = $woocommerce->post("products/".$responseWoo->id."/variations",$dataVariation);
//return new JsonResponse(['status'=>'teste','data'=>$responseWooV],200);
// update to already done
$updateDone = [
'ecom_products_id' => $row->ecom_products_id,
'ecom_products_woo' => Json::encode([$responseWoo,$responseWooV],Json::TYPE_ARRAY),
'ecom_products_status' => 'active'
];
$productID = $this->ecomProductsTable->save($updateDone,$tokendata->users_id);
$product = $this->ecomProductsTable->getById($productID);
$user = $this->usersTable->getById($product->ecom_products_users_id);
// get current user from post to notify pntendiost status
$this->sendMailProduct($user,$product,"approved");
return new JsonResponse(["status" => "success", "public_message" => "Ok!"], 200);
} else{
return new JsonResponse(["status" => "error", "public_message" => "Houve um erro ao adicionar via API."], 200);
}
} catch (\Exception $exception) {
return new JsonResponse(['status' => 'error', 'public_message' => $exception->getMessage()], 200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment