Есть код, если выполнять его без джобса, то все приходит. При добавлении в джобс, функция set_product_quantity не срабатывает, устанавливает количество 0 у всех продуктов. Данный код передаю в джобс, а сам джобс вызываю из контроллера. И подскажите, как можно вывести сообщение, после выполнения джобса?
class CreateProductService
{
public static function searchProduct(Collection $collection)
{
$value = config('prestashop');
$webService = new PrestaShopWebservice($value['path'], $value['key'], $value['debug']);
foreach ($collection as $product) {
$xml = $webService->get([
'resource' => 'products',
'display' => 'full',
'filter[id_manufacturer]' => $product->item_code
]);
$resource = $xml->children()->children();
$flag = false;
if ($resource->product->id_manufacturer == $product->item_code) {
$flag = true;
}
if ($flag !== true) {
self::addProductOnPrestaShop($product);
}
}
}
}
public static function addProductOnPrestaShop($product)
{
$value = config('prestashop');
try {
$webService = new PrestaShopWebservice($value['path'], $value['key'], $value['debug']);
$products = array('resource' => 'products');
$xml = $webService->get(array('url' => $value['path'] . '/api/products?schema=blank'));
$resource_product = $xml->children()->children();
unset($resource_product->position_in_category);
$resource_product->id_category_default = $product->category->prestashop_id;
$resource_product->id_manufacturer = $product->item_code;
$resource_product->unity = $product->unit;
$resource_product->description->language[0] = $product->description;
$resource_product->description_short->language[0] = $product->short_description;
$resource_product->price = $product->price_aed;
$resource_product->reference = $product->product_number;
$resource_product->active = 1;
$resource_product->name->language[0] = $product->product_name;
$resource_product->state = 1;
$products['postXml'] = $xml->asXML();
$xml = $webService->add($products);
$ProductId = $xml->product->id;
self::getIdStockAvailableAndSet($ProductId, $product);
} catch (PrestaShopWebserviceException $ex) {
echo 'Error: <br />' . $ex->getMessage();
}
}
public static function getIdStockAvailableAndSet($ProductId, $product)
{
$value = config('prestashop');
$webService = new PrestaShopWebservice($value['path'], $value['key'], $value['debug']);
$opt['resource'] = 'products';
$opt['id'] = $ProductId;
$xml = $webService->get($opt);
$item = $xml->product->associations->stock_availables->stock_available;
self::set_product_quantity($ProductId, $item->id, $item->id_product_attribute, $product);
}
public static function set_product_quantity($ProductId, $StokId, $AttributeId, $product)
{
$value = config('prestashop');
$webService = new PrestaShopWebservice($value['path'], $value['key'], $value['debug']);
$xml = $webService->get(array('url' => $value['path'] . '/api/stock_availables?schema=blank'));
$resources = $xml->children()->children();
$resources->id = $StokId;
$resources->id_product = $ProductId;
$resources->quantity = $product->quantity;
$resources->id_shop = 1;
$resources->out_of_stock = 1;
$resources->depends_on_stock = 0;
$resources->id_product_attribute = $AttributeId;
try {
$opt = array('resource' => 'stock_availables');
$opt['putXml'] = $xml->asXML();
$opt['id'] = $StokId;
$xml = $webService->edit($opt);
self::uploadImagePrestashop($product);
} catch (PrestaShopWebserviceException $ex) {
echo 'Error: <br />' . $ex->getMessage();
}
}
public static function uploadImagePrestashop($product)
{
$value = config('prestashop');
$webService = new PrestaShopWebservice($value['path'], $value['key'], $value['debug']);
// Get product
$xml = $webService->get([
'resource' => 'products',
'display' => 'full',
'filter[id_manufacturer]' => $product->item_code
]);
$resource = $xml->products->children()->children();
// Upload on server rme.rywal.dev
$filename = public_path('/storage/' . $product->image);
if ($product->image == true) {
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$POST_DATA = array(
'file' => base64_encode($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://qwe.ru/upload/handle.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close($curl);
// Create image to product
$urlImage = $value['path'] . '/api/images/products/' . $resource->id;
$key = $value['key'];
$link_image = $value['path'] . '/upload/subins.jpg';
$image_mime = 'image/jpg';
$args['image'] = new CurlFile($link_image, $image_mime);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_URL, $urlImage);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $key . ':');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
}
}
jobs:
class CreateProductJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $collection;
public function __construct(Collection $collection)
{
$this->collection = $collection;
}
public function handle()
{
CreateProductService::searchProduct($this->collection);
}
}
Controller:
class CreateProductController extends Controller
{
public static function createProduct(Collection $collection)
{
CreateProductJob::dispatch($collection);
return redirect()->route('filament.resources.products.index');
}
}