vendor/abcpremium/magento2pimcore/Controller/Rest/OrderQuantity.php line 20

Open in your IDE?
  1. <?php
  2. namespace Abcpremium\Magento2PimcoreBundle\Controller\Rest;
  3. use Abcpremium\Magento2PimcoreBundle\Model\Api\Connector\SimpleProduct;
  4. use Abcpremium\Magento2PimcoreBundle\Model\Configuration;
  5. use Abcpremium\Magento2PimcoreBundle\Model\Log\Logger;
  6. use Pimcore\Controller\FrontendController;
  7. use Pimcore\Model\DataObject\Concrete;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class OrderQuantity extends FrontendController
  10. {
  11.     public function requestAction(Request $request)
  12.     {
  13.         $configurationModel = new Configuration();
  14.         $content json_decode($request->getContent(), true);
  15.         $host $content['config']['base_url'];
  16.         $mapping null;
  17.         if (($content['config']['use_mapping'] ?? false) === true) {
  18.             $lines explode(PHP_EOL$content['config']['mapping']);
  19.             foreach ($lines as $line) {
  20.                 $parts array_map('trim'explode(','$line));
  21.                 $mapping[$parts[0]] = $parts[1];
  22.             }
  23.         }
  24.         if ($host === 'localhost') {
  25.             $entry $configurationModel->getByHost('https://pimcore.abcpremium.de/');
  26.         } else {
  27.             if (str_contains($host'http') === false) {
  28.                 $host 'https://' $host '/';
  29.             }
  30.             $entry $configurationModel->getByHost($host);
  31.         }
  32.         if (empty($entry) && $host !== 'localhost') {
  33.             return $this->json(["authenticated" => false]);
  34.         }
  35.         $authToken $request->headers->all('Authorization');
  36.         if (count($authToken) === 0) {
  37.             return $this->json(["authenticated" => false]);
  38.         } else {
  39.             $authToken str_replace('Bearer '''$authToken[0]);
  40.         }
  41.         $token $entry->getAccess_token();
  42.         if ($authToken !== $token) {
  43.             return $this->json(["authenticated" => false]);
  44.         }
  45.         foreach ($content['skus'] as $sku => $qty) {
  46.             $dataObject self::getClassInstance($entry);
  47.             $objects $dataObject::getByArtikelNr($sku);
  48.             foreach ($objects as $object) {
  49.                 $object self::getLatestVersion($object);
  50.                 $currentQty $object->get($content['config']['qty_field']);
  51.                 /*if ((int)$currentQty === 0) {
  52.                     continue;
  53.                 }*/
  54.                 if ($currentQty 0) {
  55.                     $currentQty 0;
  56.                 }
  57.                 if ($content['config']['action'] === 'add') {
  58.                     $newQty $currentQty $qty;
  59.                 } else {
  60.                     $newQty $currentQty $qty;
  61.                 }
  62.                 if ($newQty 0) {
  63.                     $newQty 0;
  64.                 }
  65.                 $object->set($content['config']['qty_field'], $newQty);
  66.                 $object->save();
  67.                 $transferQty $newQty;
  68.                 $webshops $object->getWebshops();
  69.                 foreach ($webshops as $webshop) {
  70.                     if ($webshop === $host) {
  71.                         continue;
  72.                     }
  73.                     $entry $configurationModel->getByHost($webshop);
  74.                     if (empty($entry)) {
  75.                         Logger::toProductLog('Could not find the configuration entry of host ' $webshop);
  76.                         continue;
  77.                     }
  78.                     if ($mapping !== null && array_key_exists($webshop$mapping)) {
  79.                         $pimcoreQtyField $mapping[$webshop];
  80.                         $transferQty $object->get($pimcoreQtyField);
  81.                     }
  82.                     $apiConfig = [
  83.                         'host' => $webshop,
  84.                         'access_token' => $entry->getAccess_token(),
  85.                         'store_code' => 'all'
  86.                     ];
  87.                     $body = [
  88.                         'extension_attributes' => [
  89.                             'stock_item' => [
  90.                                 'qty' => $transferQty
  91.                             ]
  92.                         ],
  93.                         'sku' => $sku
  94.                     ];
  95.                     $response SimpleProduct::update($apiConfig$body);
  96.                     if (empty($response['id'])) {
  97.                         Logger::toProductLog('Could not set the quantity (' $transferQty ') of product ' $sku ' in webshop ' $webshop);
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.         return $this->json(["success" => true]);
  103.     }
  104.     private static function getClassInstance($entry)
  105.     {
  106.         $productClass "\Pimcore\Model\DataObject\\" $entry->getProduct_class();
  107.         return new $productClass();
  108.     }
  109.     private static function getLatestVersion($object)
  110.     {
  111.         $latestVersion $object->getLatestVersion();
  112.         if (null === $latestVersion) {
  113.             return $object;
  114.         }
  115.         $latestVersionObject $latestVersion->getData();
  116.         if (!$latestVersionObject instanceof Concrete) {
  117.             return $object;
  118.         }
  119.         return $latestVersionObject;
  120.     }
  121. }