Created
December 8, 2020 10:39
-
-
Save SebSept/982e016f60ba9383be5eb30be3e17826 to your computer and use it in GitHub Desktop.
first implementation (wip) of an ajax admin controller. (for review).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SebSept\Adminproductquantities\Controller; | |
use PrestaShopBundle\Api\Stock\Movement; | |
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; | |
use PrestaShopBundle\Entity\ProductIdentity; | |
use PrestaShopBundle\Entity\Repository\StockRepository; | |
use PrestaShopBundle\Service\ProductService; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
class Update extends FrameworkBundleAdminController | |
{ | |
/** | |
* Update Product stock | |
* | |
* (pas de gestion des déclinaisons, c'est normal) | |
* | |
* @param int $quantity new quantity (absolute value) | |
* @param int $id_product | |
* | |
* @return JsonResponse | |
*/ | |
public function update(int $quantity, int $id_product) | |
{ | |
try { | |
$product_provider = $this->get('prestashop.adapter.data_provider.product'); | |
$stock_repository = $this->get('prestashop.core.api.stock.repository'); | |
$delta = $quantity - (int)$product_provider->getQuantity($id_product); | |
$stock_data = $stock_repository->updateStock(new Movement(new ProductIdentity($id_product), $delta)); | |
$fetched_quantity = $stock_data['product_available_quantity'] ?? 'no-data'; | |
if ($quantity !== $fetched_quantity) | |
{ | |
throw new \Exception("Echec mise à jour stock : quantité voulue et recupérée differentes. [$quantity] vs [$fetched_quantity]"); | |
} | |
} | |
catch (\Exception $exception) | |
{ | |
return new JsonResponse(['error' => $exception->getMessage()]); | |
} | |
// @todo implement reponse positive | |
return new JsonResponse(['q' => $quantity, 'id' => $id_product]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the feedback 😄
👍 For sure, booting the kernel and grab it, but I suppose performances is going to suffer from that.