File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/WoocommerceController.php
<?php
namespace App\Http\Controllers;
use App\Product;
use Carbon\Carbon;
use App\Category;
use App\Parameter;
use App\Attribute;
use App\Subcategories;
use App\ProductAttribute;
use App\IntegrationProvider;
use Illuminate\Http\Request;
use Automattic\WooCommerce\Client;
use App\Http\Controllers\UserController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\AddressController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\AttributeController;
use App\Order;
class WoocommerceController extends Controller
{
private $validSync = false;
private $woocommerce;
private $last_load_date;
private $integration;
private $parameters;
private $controllerOrder;
private $utilController;
public function __construct()
{
$this->initApiRest();
$this->controllerOrder = new OrderController;
$this->utilController = new UtilController;
}
public function initApiRest()
{
$this->parameters = Parameter::select('sync_woocommerce', 'sucursal_products')->find(1);
if ($this->parameters && $this->parameters->sync_woocommerce) {
$this->integration = IntegrationProvider::where([['ecommerce_type_id', 2], ['active', true]])->first();
if ($this->integration) {
$this->validSync = true;
$this->last_load_date = $this->integration->last_load_date;
$this->woocommerce = new Client(
$this->integration->endpoint,
$this->integration->key_public,
$this->integration->key_private,
[
'version' => 'wc/v3',
]
);
}
}
}
public function newRequest($data)
{
return new Request($data);
}
public function sync()
{
if ($this->validSync) {
$loadDate = Carbon::now();
$this->getOrdersFromWoocommerce();
$this->integration->last_load_date = $loadDate;
$this->integration->update();
}
}
//---------------------------------------------------webhook-------------------------------------------------------
public function webhooksListener(Request $request)
{
try {
$this->utilController->logFile($request, 'woocommerce');
$order = json_decode($request->getContent(), true);
$this->saveData($order);
} catch (\Exception $e) {
$error = [
'order' => $order,
'error' => $e->__toString(),
'function' => 'saveData'
];
$this->utilController->logFile(json_encode($error), 'webhooksListener');
}
return response(array('r' => true, 'm' => "Recibido"));
}
//---------------------------------------------------END webhook-------------------------------------------------------
private function saveData($order)
{
if (!$this->controllerOrder->getOrderByReference($order['number'])) {
$confirmUser = $this->registerUserFromOrder($order);
if ($confirmUser) $this->saveOrder($order);
}
}
public function getOrdersFromWoocommerce()
{
$date = Carbon::parse($this->last_load_date)->subMinutes(30)->toIso8601String();
$page = 1;
$woocommerceOrders = [];
$orders = [];
do {
try {
$woocommerceOrders = $this->woocommerce->get('orders', array('per_page' => 100, 'after' => $date, 'page' => $page));
} catch (HttpClientException $e) {
die("Can't get orders: $e");
}
$orders = array_merge($orders, $woocommerceOrders);
$page++;
} while (count($woocommerceOrders) > 0);
foreach ($orders as $order) {
$order = json_decode(json_encode($order), true);
$this->saveData($order);
}
}
private function registerUserFromOrder($order)
{
$userController = new UserController();
if (isset($order['customer']))
$customer = $order['customer'];
else if (isset($order['billing'])) {
$customer = $order['billing'];
}
$billingAddress = $order['billing'];
if (!$customer) {
return;
}
$email = $customer['email'] ?? $order['contact_email'] ?? $order['email'];
$password = base64_encode(random_bytes(4));
$tags = [];
$userController->createClient(new Request([
'first_name' => $customer['first_name'] ?? $billingAddress['first_name'] ?? 'User',
'last_name' => $customer['last_name'] ?? $billingAddress['last_name'],
'email' => $email,
'password' => $password,
'tags' => $tags,
'phone' => $customer['phone'] ?? $billingAddress['phone'] ?? '1234',
'document' => $customer['document'] ?? $billingAddress['document'] ?? $billingAddress['company'],
'document_type' => ''
]), true);
return true;
}
private function saveOrder($order)
{
//logica para el estado de la orden
$orderStatus = $this->getOrderStatusWoo($order['status']);
//logica pasarelas de pago
$transaction = $this->getTransactionId($order);
if (!$order['transaction_id']) {
$order['transaction_id'] = $transaction;
}
if ($orderStatus)
if ($order['number']) {
$order_reference_woo = $order['number'];
$validateOrder = Order::where('order_reference', $order_reference_woo)->first();
if ($validateOrder) {
if ($orderStatus == 'completed') {
$validateOrder->gw_state = "CONFIRMED";
$validateOrder->update();
}
if ($orderStatus == 'cancelled') {
$this->restoreInventory($order);
$validateOrder->gw_state = "REJECTED";
$validateOrder->update();
}
return response(array('r' => true, 'm' => "Pedido actualizado", 'd' => null));
}
}
$request_params = new Request([]);
$controllerOrder = new OrderController();
$controllerAddress = new AddressController();
$controllerUser = new UserController($request_params);
$document = null;
$address_id = null;
if (isset($order['customer']))
$customer = json_decode(json_encode($order['customer']), true);
else if (isset($order['billing'])) {
$customer = json_decode(json_encode($order['billing']), true);
}
$shippingAddress = json_decode(json_encode($order['shipping']), true);
if (!$shippingAddress['address_1'])
$shippingAddress = json_decode(json_encode($order['billing']), true);
if (!$customer || !$customer['email']) {
return;
}
$user = $controllerUser->searchUser(
$document,
$customer['email'],
$customer['first_name'],
$customer['last_name'],
$customer['phone']
);
if ($user) {
$user_id = $user->id;
if ($shippingAddress) {
$address = $controllerAddress->searchAddressByAddress($shippingAddress['address_1'], $user_id);
if ($address) {
$address_id = $address;
}
}
}
if ($shippingAddress) {
$city_id = $controllerAddress->searchCityByName($shippingAddress['city']);
}
$products = [];
$subtotal = 0;
$size = '';
foreach ($order['line_items'] as $key => $product) {
$current_product = Product::where('reference_woocommerce_product_id', $product['product_id'])->first();
if ($current_product) {
if (isset($product['meta_data'][0]['value'])) {
$size = $product['meta_data'][0]['value'];
}
$valuesAttributes = [];
$product_atribute = ProductAttribute::where([['product_id', $current_product->id], ['value', $size]])->with('attribute')->first();
if ($product_atribute) {
$arrayAttribute = array(
'attribute' => $product_atribute->attribute->toArray(),
'attribute_id' => $product_atribute->attribute->id,
'available_units' => $product_atribute->available_units,
'dispatched_units' => $product_atribute->dispatched_units,
'ean' => $product_atribute->ean,
'high' => $product_atribute->high,
'id' => $product_atribute->id,
'length' => $product_atribute->length,
'main_position' => $product_atribute->main_position,
'observation' => $product_atribute->observation,
'packaging' => $product_atribute->packaging,
'pmi' => $product_atribute->pmi,
'price_additional' => $product_atribute->price_additional,
'product_id' => $product_atribute->product_id,
'quantity' => $product['quantity'],
'sku' => $product_atribute->sku,
'stowage_pattern' => null,
'value' => $product['meta_data'][0]['value'],
'weight' => null,
'width' => null,
'created_at' => null,
'updated_at' => null,
);
$valuesAttributes[] = $arrayAttribute;
}
$newProduct = array(
'quantity' => $product['quantity'],
'max_units_per_order' => $current_product->max_units_per_order,
'price' => $product['subtotal'],
'priceTotal' => $product['total'],
'id' => $current_product->id,
'plu' => $current_product->plu,
'name' => $current_product->name,
'priceAfterDiscount' => null,
'priceAfterFlash' => null,
'percentage_discount' => null,
'start_discount' => null,
'limit_discount' => null,
'limit_hour_discount' => null,
'flash_price' => null,
'start_flash_discount' => null,
'limit_flash_discount' => null,
'limit_hour_flash_discount' => null,
'product_attributes_selected' => $valuesAttributes
);
$products[] = $newProduct;
}
$subtotal = $subtotal + $product['total'];
}
$priceDomicile = $order['shipping_lines'];
if (count($priceDomicile) > 0) {
$priceDomicile = $priceDomicile[0]['total'] ?? $priceDomicile[0]['price'];
} else {
$priceDomicile = 0;
}
$phone = $shippingAddress['phone'];
$address = $shippingAddress['address_1'];
$data = array('data' => array(
'first_name' => $customer ? $customer['first_name'] : null,
'last_name' => $customer ? $customer['last_name'] : null,
'document' => $document,
'phone' => $phone,
'email' => $customer ? $customer['email'] : null,
'order_reference' => $order['number'],
'sucursal' => null,
'products' => $products,
'confirmOrder' => array(
'observations' => "",
'effectivePayment' => 0,
'cellphone' => $phone,
'wayPay' => 5,
'address' => $address_id,
'address_new' => array(
'direction' => $shippingAddress['address_1'],
'district' => '',
'instru' => $shippingAddress['address_2'],
'lat' => '',
'lng' => '',
),
),
'dCampana' => null,
'dReferred' => null,
'dCoupone' => null,
'subtotal' => $subtotal,
'discount' => $order['discount_total'],
'priceDomicile' => $order['shipping_total'],
'total' => $order['total'],
'productsNotFound' => null,
'order_type_id' => 5,
'client_id' => $user_id,
'city_id' => $city_id,
'delivery_man_id' => null,
'payment_transaction_id' => $order['transaction_id'],
"purchase_origin" => 'OTROS MEDIOS'
));
$result;
if (count($products)) {
$request = new Request($data);
$result = $controllerOrder->create($request, false);
}
return response(array('r' => true, 'm' => "Recibido", 'd' => $result));
}
// Categorias
public function createCategory($data)
{
try {
if ($this->validSync) {
$category = $this->woocommerce->post('products/categories', $data);
return $category->id;
}
return null;
} catch (\Exception $e) {
return null;
}
}
public function updateCategory($id, $data)
{
try {
if ($this->validSync && $id && $id != "") {
$this->woocommerce->put('products/categories/' . $id, $data);
}
} catch (\Exception $e) {
return null;
}
}
public function deleteCategory($id)
{
try {
if ($this->validSync && $id && $id != "") {
$this->woocommerce->delete('products/categories/' . $id, ['force' => true]);
}
} catch (\Exception $e) {
return null;
}
}
public function listAllCategory($categoryName)
{
try {
if ($this->validSync && $categoryName && $categoryName != "") {
$categoryWoo = $this->woocommerce->get('products/categories/' . $categoryName);
return $categoryWoo;
}
} catch (\Exception $e) {
return null;
}
}
// Atributos
public function createAttribute($data)
{
try {
if ($this->validSync) {
$attribute = $this->woocommerce->post('products/attributes', $data);
return $attribute->id;
}
return null;
} catch (\Exception $e) {
return null;
}
}
public function updateAttribute($id, $data)
{
try {
if ($this->validSync && $id && $id != "") {
$this->woocommerce->put('products/attributes/' . $id, $data);
}
} catch (\Exception $e) {
return null;
}
}
public function deleteAttribute($id)
{
try {
if ($this->validSync && $id && $id != "") {
$this->woocommerce->delete('products/attributes/' . $id, ['force' => true]);
}
} catch (\Exception $e) {
return null;
}
}
// Attribute terms
public function createAttributeTerm($attribute_id, $data)
{
try {
if ($this->validSync && $attribute_id && $attribute_id != "") {
$attributeTerm = $this->woocommerce->post('products/attributes/' . $attribute_id . '/terms', $data);
return $attributeTerm->id;
}
return null;
} catch (\Exception $e) {
return null;
}
}
public function updateAttributeTerm($attribute_term_id, $attribute_id, $data)
{
try {
if ($this->validSync && $attribute_term_id && $attribute_term_id != "" && $attribute_id && $attribute_id != "") {
$this->woocommerce->put('products/attributes/' . $attribute_id . '/terms/' . $attribute_term_id, $data);
}
} catch (\Exception $e) {
return null;
}
}
public function deleteAttributeTerm($attribute_term_id, $attribute_id)
{
try {
if ($this->validSync && $attribute_term_id && $attribute_term_id != "" && $attribute_id && $attribute_id != "") {
$this->woocommerce->delete('products/attributes/' . $attribute_id . '/terms/' . $attribute_term_id, ['force' => true]);
}
} catch (\Exception $e) {
return null;
}
}
public function getAllAttributeTerm($attribute_id)
{
try {
if ($this->validSync && $attribute_id && $attribute_id != "") {
$page = 1;
$attributeTerms = [];
$all_attribute = [];
do {
try {
$attributeTerms = $this->woocommerce->get('products/attributes/' . $attribute_id . '/terms', array('per_page' => 100, 'page' => $page));
} catch (HttpClientException $e) {
die("Can't get attributes: $e");
}
$all_attribute = array_merge($all_attribute, $attributeTerms);
$page++;
} while (count($attributeTerms) > 0);
return $all_attribute;
}
return null;
} catch (\Exception $e) {
return null;
}
}
// produtcs
public function createProduct($data)
{
try {
if ($this->validSync) {
$product = $this->woocommerce->post('products', $data);
return $product->id;
}
return null;
} catch (\Exception $e) {
return null;
}
}
public function updateProduct($id, $data)
{
try {
if ($this->validSync && $id && $id != "") {
$this->woocommerce->put('products/' . $id, $data);
}
} catch (\Exception $e) {
return null;
}
}
public function deleteProduct($id)
{
try {
if ($this->validSync && $id && $id != "") {
$this->woocommerce->delete('products/' . $id, ['force' => true]);
}
} catch (\Exception $e) {
return null;
}
}
public function searchProduct($data)
{
try {
if ($this->validSync && $data && $data != "") {
$product = $this->woocommerce->get('products', ['search' => $data]);
return $product;
}
} catch (\Exception $e) {
return null;
}
}
public function searchProductId($dataId)
{
try {
if ($this->validSync && $dataId && $dataId != "") {
$product = $this->woocommerce->get('products/' . $dataId);
return $product;
}
} catch (\Exception $e) {
return null;
}
}
// products Variation
public function createProductVariation($product_id, $data)
{
try {
if ($this->validSync && $product_id && $product_id != "") {
$variation = $this->woocommerce->post('products/' . $product_id . '/variations', $data);
return $variation->id;
}
return null;
} catch (\Exception $e) {
return null;
}
}
public function updateProductVariation($product_id, $variation_id, $data)
{
try {
if ($this->validSync && $product_id && $product_id != "" && $variation_id && $variation_id != "") {
$this->woocommerce->put('products/' . $product_id . '/variations/' . $variation_id, $data);
}
} catch (\Exception $e) {
return null;
}
}
public function deleteProductVariation($product_id, $variation_id)
{
try {
if ($this->validSync && $product_id && $product_id != "" && $variation_id && $variation_id != "") {
$this->woocommerce->delete('products/' . $product_id . '/variations/' . $variation_id, ['force' => true]);
}
} catch (\Exception $e) {
return null;
}
}
public function searchProductVariation($sku)
{
try {
if ($this->validSync && $sku && $sku != "") {
$variation = $this->woocommerce->get('products', ['sku' => $sku]);
return $variation;
}
} catch (\Exception $e) {
return null;
}
}
// products order
public function createOrder($data)
{
try {
if ($this->validSync) {
$ordersWoo = $this->woocommerce->post('orders', $data);
return $ordersWoo;
}
return null;
} catch (\Exception $e) {
return null;
}
}
public function listAllOrder()
{
try {
if ($this->validSync) {
$ordersWoo = $this->woocommerce->get('orders?status=completed');
return $ordersWoo;
}
} catch (\Exception $e) {
return null;
}
}
public function updateOrder($id, $data)
{
try {
if ($this->validSync && $id && $id != "") {
$orderStatus = $this->woocommerce->put('orders/' . $id, $data);
return $orderStatus->status;
}
return null;
} catch (\Exception $e) {
return null;
}
}
public function syncProducts()
{
try {
if ($this->validSync) {
$request_params = $this->newRequest([]);
$page = 1;
$woocommerceProducts = [];
$products = [];
do {
try {
$woocommerceProducts = $this->woocommerce->get('products', array('per_page' => 100, 'page' => $page));
} catch (HttpClientException $e) {
die("Can't get products: $e");
}
$products = array_merge($products, $woocommerceProducts);
$page++;
} while (count($woocommerceProducts) > 0);
$category = new CategoryController($request_params);
$product_controller = new ProductController($request_params);
$productAttribute_controller = new ProductAttributeController($request_params);
$attribute_controller = new AttributeController($request_params);
if (count($products)) {
foreach ($products as $product) {
$current_product = Product::where('name', $product->name)->first();
// si existe actualizamos si no creamos
if ($current_product) {
$current_product->reference_woocommerce_product_id = $product->id;
foreach ($product->attributes as $attribute) {
$search_attribute = Attribute::where('name', $attribute->name)->first();
if ($search_attribute) {
$search_attribute->reference_woocommerce_attribute_id = $attribute->id;
$search_attribute->update();
}
}
foreach ($product->categories as $item_category) {
$search_category = Category::where('name', $item_category->name)->first();
if ($search_category) {
$search_category->reference_woocommerce_category_id = $item_category->id;
$search_category->update();
$search_subcategory = Subcategories::where([['category_id', $search_category->id], ['name', $item_category->name]])->first();
if ($search_subcategory) {
$search_subcategory->reference_woocommerce_category_id = $item_category->id;
$search_subcategory->update();
}
}
}
foreach ($product->variations as $variant) {
$search_variant = $this->woocommerce->get('products/' . $product->id . '/variations/' . $variant);
if (count($search_variant->attributes)) {
$current_product_attribute = ProductAttribute::select('id', 'reference_shopify_id', 'reference_shopify_variation_id')
->where('product_id', $current_product->id)
->where('value', $search_variant->attributes[0]->option)
->first();
if ($current_product_attribute) {
$current_product_attribute->reference_woocommerce_variation_id = $search_variant->id;
$current_product_attribute->update();
}
}
}
$current_product->update();
} else {
$sucursal_id = $this->parameters->sucursal_products ? Auth::user()->userInfo->sucursal_id : null;
$brand_id = null;
$sub_categories = [];
if (count($product->categories)) {
foreach ($product->categories as $subcategory) {
$data_category = [
'name' => $subcategory->name,
'sucursal_id' => $sucursal_id,
'priority' => 1,
'reference_woocommerce_category_id' => $subcategory->id,
];
$c_category = $category->create($this->newRequest($data_category), false);
if ($c_category) {
$sub_categories[] = $c_category["s"];
}
}
}
$available_units = $product->stock_quantity != null ? $product->stock_quantity : 0;
if (count($product->variations)) {
foreach ($product->variations as $variant) {
$search_variant = $this->woocommerce->get('products/' . $product->id . '/variations/' . $variant);
if ($search_variant) {
$available_units = $available_units + $search_variant->stock_quantity;
}
}
}
$price = 0;
if ($product->price != null || $product->price != '') {
$price = $product->price;
}
$plu = $product->slug;
if ($product->sku != null || $product->sku != '') {
$plu = $product->sku;
}
$product_id = null;
$data_product = [
'name' => $product->name,
'plu' => $plu,
'bar_code' => null,
'price' => $price,
'available_units' => $available_units,
'max_units_per_order' => 100,
'percentage_discount' => null,
'start_discount' => null,
'limit_discount' => null,
'limit_hour_discount' => null,
'flash_price' => null,
'start_flash_discount' => null,
'limit_flash_discount' => null,
'limit_hour_flash_discount' => null,
'brand_id' => $brand_id,
'order' => 1,
'chkAgeProduct' => false,
'sucursal_id' => $sucursal_id,
'sub_categories' => $sub_categories,
'reference_woocommerce_product_id' => $product->id,
];
$cproduct = $product_controller->create($this->newRequest($data_product), false);
if ($cproduct) {
$product_id = $cproduct["d"]["id"];
}
foreach ($product->attributes as $attribute) {
$name_attribute = "Tallas checkbox único";
foreach ($attribute->options as $option) {
$current_sku = null;
$current_available_units = 0;
$current_variation_id = 0;
foreach ($product->variations as $variant) {
$search_variant = $this->woocommerce->get('products/' . $product->id . '/variations/' . $variant);
if ($search_variant) {
foreach ($search_variant->attributes as $search) {
if ($option == $search->option) {
$current_sku = $search_variant->sku;
$current_available_units = $search_variant->stock_quantity;
$current_variation_id = $search_variant->id;
$name_attribute = $search->name;
break;
}
}
}
}
$attribute_id = null;
$data_attribute = [
"name" => $name_attribute,
"display_name" => $name_attribute,
"is_more_one" => false,
"required" => true,
"attribute_type_id" => 2,
"active" => true,
"reference_woocommerce_attribute_id" => $attribute->id,
];
$catrribute = $attribute_controller->store($this->newRequest($data_attribute), false);
if ($catrribute) {
$attribute_id = $catrribute["d"];
}
$data_productAttribute = [
"product_id" => $product_id,
"attribute_id" => $attribute_id,
"value" => $option,
"price_additional" => 0,
"sku" => $current_sku,
"available_units" => $current_available_units,
"pmi" => 0,
"packaging" => 0,
"weight" => 0,
"ean" => 0,
"width" => 0,
"length" => 0,
"high" => 0,
"observation" => 0,
"main_position" => 0,
"stowage_pattern" => 0,
"reference_woocommerce_variation_id" => $current_variation_id,
];
$productAttribute_controller->store($this->newRequest($data_productAttribute), $product_id, false);
}
}
}
}
}
return response(array('r' => true, 'm' => 'Catálogo sincronizado exitosamente', 'd' => null));
}
} catch (\Throwable $th) {
$error = [
'message' => $th->getMessage(),
'getFile' => $th->getFile(),
'getLine' => $th->getLine(),
];
$this->utilController->logFile(json_encode($error));
return response(array('r' => false, 'm' => 'Hubo un error, intenta más tarde', 'd' => $th->getMessage()));
}
}
public function getOrderStatusWoo($status)
{
$orderStatusWoo = null;
switch ($status) {
case 'pending':
$orderStatusWoo = $status;
break;
case 'processing':
$orderStatusWoo = $status;
break;
case 'completed':
$orderStatusWoo = $status;
break;
case 'cancelled':
$orderStatusWoo = $status;
break;
}
return $orderStatusWoo;
}
private function restoreInventoryProductoWoo($product_id, $sku, $quantity)
{
$variation_woo = $this->searchProductVariation($sku);
$current_product = Product::select('reference_woocommerce_product_id')->where('id', $product_id)->first();
$reference_woo_variation_id = intval($current_product->reference_woocommerce_product_id);
if ($reference_woo_variation_id && $variation_woo) {
foreach ($variation_woo as $value) {
$variation_id = $value->id;
}
$numQuantity = $variation_woo[0]->stock_quantity + $quantity;
$inventory_management_woo = [
'manage_stock' => true,
'stock_quantity' => $numQuantity,
];
$result = $this->updateProductVariation($reference_woo_variation_id, $variation_id, $inventory_management_woo);
}
}
private function getTransactionId($order)
{
$transaction_id = null;
switch ($order['payment_method']) {
case 'fullculqi':
$key = array_column($order['meta_data'], 'key');
$found_key = array_search('_culqi_charge_id', $key);
if ($found_key) {
$transaction_id = $order->meta_data[$found_key]->value;
}
break;
case 'wompi':
$key = array_column($order['meta_data'], 'key');
$found_key = array_search('_wompi_payment_method_type', $key);
if ($found_key) {
$transaction_id = $order->meta_data[$found_key]->value;
}
break;
case 'zonapagos':
if (!$order['transaction_id']) {
if (!$order['order_key']) {
$key = array_column($order['meta_data'], 'key');
$found_key = array_search('_billing_document', $key);
if ($found_key) {
$transaction_id = $order->meta_data[$found_key]->value;
}
} else {
$transaction_id = $order['order_key'];
}
} else {
$transaction_id = $order['transaction_id'];
}
break;
case 'openpay_cards':
if (!$order['transaction_id']) {
if (!$order['order_key']) {
$key = array_column($order['meta_data'], 'key');
$found_key = array_search('_openpay_customer_sandbox_id', $key);
if ($found_key) {
$transaction_id = $order->meta_data[$found_key]->value;
}
} else {
$transaction_id = $order['order_key'];
}
} else {
$transaction_id = $order['transaction_id'];
}
break;
}
return $transaction_id;
}
private function restoreInventory($order)
{
try {
if ($this->validSync) {
//pending / processing / completed / cancelled
// Si la orden esta cancelada restaura el inventario
foreach ($order['line_items'] as $key => $product) {
$current_product = Product::where('reference_woocommerce_product_id', $product['product_id'])->first();
if ($current_product) {
$product_attributes = ProductAttribute::where([['product_id', $current_product->id], ['sku', $product['sku']]])->first();
if ($product_attributes) {
$current_product->available_units = $current_product->available_units + intval($product['quantity']);
$product_attributes->available_units = $current_product->available_units + intval($product['quantity']);
$current_product->update();
$product_attributes->update();
$this->restoreInventoryProductoWoo($product['product_id'], $product['sku'], intval($product['quantity']));
}
}
}
}
} catch (\Throwable $th) {
$error = [
'message' => $th->getMessage(),
'getFile' => $th->getFile(),
'getLine' => $th->getLine(),
];
$this->utilController->logFile(json_encode($error));
}
}
}