Fix cannot get Qty of configable product
Showing
2 changed files
with
12 additions
and
7 deletions
| ... | @@ -8,10 +8,6 @@ | ... | @@ -8,10 +8,6 @@ |
| 8 | 8 | ||
| 9 | namespace FGCT\OverrideCreateInvoice\Block\Adminhtml\Items\Renderer; | 9 | namespace FGCT\OverrideCreateInvoice\Block\Adminhtml\Items\Renderer; |
| 10 | 10 | ||
| 11 | use Magento\Sales\Model\Order\CreditMemo\Item as CreditMemoItem; | ||
| 12 | use Magento\Sales\Model\Order\Invoice\Item as InvoiceItem; | ||
| 13 | use Magento\Sales\Model\Order\Item as OrderItem; | ||
| 14 | |||
| 15 | /** | 11 | /** |
| 16 | * Order item render block | 12 | * Order item render block |
| 17 | * | 13 | * |
| ... | @@ -21,6 +17,7 @@ use Magento\Sales\Model\Order\Item as OrderItem; | ... | @@ -21,6 +17,7 @@ use Magento\Sales\Model\Order\Item as OrderItem; |
| 21 | class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer { | 17 | class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer { |
| 22 | public function __construct( | 18 | public function __construct( |
| 23 | \Magento\Backend\Block\Template\Context $context, | 19 | \Magento\Backend\Block\Template\Context $context, |
| 20 | \Magento\Catalog\Model\Product $product, | ||
| 24 | \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, | 21 | \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, |
| 25 | \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, | 22 | \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, |
| 26 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository, | 23 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository, |
| ... | @@ -28,12 +25,15 @@ class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\Defa | ... | @@ -28,12 +25,15 @@ class DefaultRenderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\Defa |
| 28 | array $data = [] | 25 | array $data = [] |
| 29 | ) { | 26 | ) { |
| 30 | $this->_coreRegistry = $registry; | 27 | $this->_coreRegistry = $registry; |
| 28 | $this->_product = $product; | ||
| 31 | $this->_stockItemRepository = $stockItemRepository; | 29 | $this->_stockItemRepository = $stockItemRepository; |
| 32 | parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data); | 30 | parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data); |
| 33 | } | 31 | } |
| 34 | 32 | ||
| 35 | public function getErrorOutStock($item) { | 33 | public function getErrorOutStock($item) { |
| 36 | $productId = $item->getProductId(); | 34 | //$productId = $item->getProductId(); |
| 35 | $sku = $item->getSku(); | ||
| 36 | $productId = $this->_product->getIdBySku($sku); | ||
| 37 | $_productStock = $this->_stockItemRepository->get($productId); | 37 | $_productStock = $this->_stockItemRepository->get($productId); |
| 38 | $qtySelected = intval($item->getQty()); | 38 | $qtySelected = intval($item->getQty()); |
| 39 | $qtyProduct = $_productStock->getQty(); | 39 | $qtyProduct = $_productStock->getQty(); | ... | ... |
| ... | @@ -54,6 +54,7 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { | ... | @@ -54,6 +54,7 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { |
| 54 | ShipmentSender $shipmentSender, | 54 | ShipmentSender $shipmentSender, |
| 55 | ShipmentFactory $shipmentFactory, | 55 | ShipmentFactory $shipmentFactory, |
| 56 | InvoiceService $invoiceService, | 56 | InvoiceService $invoiceService, |
| 57 | \Magento\Catalog\Model\Product $product, | ||
| 57 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository | 58 | \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository |
| 58 | ) { | 59 | ) { |
| 59 | $this->registry = $registry; | 60 | $this->registry = $registry; |
| ... | @@ -61,6 +62,7 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { | ... | @@ -61,6 +62,7 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { |
| 61 | $this->shipmentSender = $shipmentSender; | 62 | $this->shipmentSender = $shipmentSender; |
| 62 | $this->shipmentFactory = $shipmentFactory; | 63 | $this->shipmentFactory = $shipmentFactory; |
| 63 | $this->invoiceService = $invoiceService; | 64 | $this->invoiceService = $invoiceService; |
| 65 | $this->_product = $product; | ||
| 64 | $this->_stockItemRepository = $stockItemRepository; | 66 | $this->_stockItemRepository = $stockItemRepository; |
| 65 | parent::__construct($context, $registry, $invoiceSender, $shipmentSender, $shipmentFactory, $invoiceService); | 67 | parent::__construct($context, $registry, $invoiceSender, $shipmentSender, $shipmentFactory, $invoiceService); |
| 66 | } | 68 | } |
| ... | @@ -100,7 +102,10 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { | ... | @@ -100,7 +102,10 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { |
| 100 | 102 | ||
| 101 | $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems); | 103 | $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems); |
| 102 | foreach ($invoice->getAllItems() as $item) { | 104 | foreach ($invoice->getAllItems() as $item) { |
| 103 | $_productStock = $this->_stockItemRepository->get($item->getProductId()); | 105 | $sku = $item->getSku(); |
| 106 | //$productId = $item->getProductId(); // parent | ||
| 107 | $productId = $this->_product->getIdBySku($sku); | ||
| 108 | $_productStock = $this->_stockItemRepository->get($productId); | ||
| 104 | $qtySelected = intval($item->getQty()); | 109 | $qtySelected = intval($item->getQty()); |
| 105 | $qtyProduct = $_productStock->getQty(); | 110 | $qtyProduct = $_productStock->getQty(); |
| 106 | if(!$_productStock->getIsInStock() || ($qtySelected > $qtyProduct)) { | 111 | if(!$_productStock->getIsInStock() || ($qtySelected > $qtyProduct)) { |
| ... | @@ -109,7 +114,7 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { | ... | @@ -109,7 +114,7 @@ class Save extends \Magento\Sales\Controller\Adminhtml\Order\Invoice\Save { |
| 109 | return $resultRedirect->setPath('sales/order_invoice/new/*', ['order_id' => $order->getId()]); | 114 | return $resultRedirect->setPath('sales/order_invoice/new/*', ['order_id' => $order->getId()]); |
| 110 | } | 115 | } |
| 111 | } | 116 | } |
| 112 | $invoice = false; | 117 | //$invoice = false; |
| 113 | if (!$invoice) { | 118 | if (!$invoice) { |
| 114 | throw new LocalizedException(__('We can\'t save the invoice right now.')); | 119 | throw new LocalizedException(__('We can\'t save the invoice right now.')); |
| 115 | } | 120 | } | ... | ... |
-
Please register or sign in to post a comment