0

I have this code that show error message if QTY requested is smaller than QTY on stock. i have created attributes and assign values into them. how to get those values in those attributes shown in this error message? Thank you.

<?php
namespace Vendor\QtyAddtocartMessage\Model\IsProductSalableForRequestedQtyCondition;

use Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;

class IsSalableWithReservationsCondition extends \Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition\IsSalableWithReservationsCondition
{
    /**
     * @var GetStockItemDataInterface
     */
    private $getStockItemData;

    /**
     * @var GetReservationsQuantityInterface
     */
    private $getReservationsQuantity;

    /**
     * @var GetStockItemConfigurationInterface
     */
    private $getStockItemConfiguration;

    /**
     * @var ProductSalabilityErrorInterfaceFactory
     */
    private $productSalabilityErrorFactory;

    /**
     * @var ProductSalableResultInterfaceFactory
     */
    private $productSalableResultFactory;

    private $productRepository;

    /**
     * @param GetStockItemDataInterface $getStockItemData
     * @param GetReservationsQuantityInterface $getReservationsQuantity
     * @param GetStockItemConfigurationInterface $getStockItemConfiguration
     * @param ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory
     * @param ProductSalableResultInterfaceFactory $productSalableResultFactory
     */

    public function __construct(
        GetStockItemDataInterface $getStockItemData,
        GetReservationsQuantityInterface $getReservationsQuantity,
        GetStockItemConfigurationInterface $getStockItemConfiguration,
        ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory,
        ProductSalableResultInterfaceFactory $productSalableResultFactory,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
            $this->getStockItemData = $getStockItemData;
            $this->getReservationsQuantity = $getReservationsQuantity;
            $this->getStockItemConfiguration = $getStockItemConfiguration;
            $this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
            $this->productSalableResultFactory = $productSalableResultFactory;
            $this->productRepository = $productRepository;
    }

    /**
     * @inheritdoc
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
    {
        $stockItemData = $this->getStockItemData->execute($sku, $stockId);
        if (null === $stockItemData) {
            $errors = [
                $this->productSalabilityErrorFactory->create([
                    'code' => 'is_salable_with_reservations-no_data',
                    'message' => __('The requested sku is not assigned to given stock')
                ])
            ];
            return $this->productSalableResultFactory->create(['errors' => $errors]);
        }

        $availableProductQty = floor($stockItemData['quantity']);
        $product = $this->loadMyProduct($sku);
        $productName = $product->getName();

        /** @var StockItemConfigurationInterface $stockItemConfiguration */
        $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);

        $qtyWithReservation = $stockItemData[GetStockItemDataInterface::QUANTITY] +
            $this->getReservationsQuantity->execute($sku, $stockId);
        $qtyLeftInStock = $qtyWithReservation - $stockItemConfiguration->getMinQty() - $requestedQty;
        $isEnoughQty = (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] && $qtyLeftInStock >= 0;
        if (!$isEnoughQty) {
            $errors = [
                $this->productSalabilityErrorFactory->create([
                    'code' => 'is_salable_with_reservations-not_enough_qty',
                    'message' => __('The requested QTY for "%1" exceeds inventory. only %2 qty available for this product.', $productName, $availableProductQty)
                ])
            ];

            return $this->productSalableResultFactory->create(['errors' => $errors]);
        }
        return $this->productSalableResultFactory->create(['errors' => []]);
    }
    public function loadMyProduct($sku)
    {
        return $this->productRepository->get($sku);
    }
}
2
  • Your question is not clear, please elaborate it more, what you want to achieve Commented Aug 1, 2022 at 5:42
  • @ArunSharma edited my question please check
    – Bayzel
    Commented Aug 1, 2022 at 5:46

2 Answers 2

0

As you defined $product = $this->loadMyProduct($sku); in your code then you just need to getData of it with using attribute_code

$product = $this->loadMyProduct($sku);

// add attribute code here to get value of attribute for the product
$reservationNoData = $product->getData('whiteStore');

You can check the product attribute value.

Hope it works for you!

8
  • this doesn't work. i have attribute code "white-store" which have a value of 35 and this is not working for me
    – Bayzel
    Commented Aug 1, 2022 at 6:53
  • You need to create an attribute white_store instead of white-store user underscore to create attribute Commented Aug 1, 2022 at 7:13
  • actually attribute name is "whiteStore" in one word
    – Bayzel
    Commented Aug 1, 2022 at 7:14
  • Try now updated the answer Commented Aug 1, 2022 at 7:24
  • @Arus this doesn't work still. this time nothing showing earlier it was showing an error
    – Bayzel
    Commented Aug 1, 2022 at 7:46
0

this code worked for me.

$product->getResource()->getAttributeRawValue($product->getId(), 'attribute_code', $product->getStoreId());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.