0

I have a related products section on my product page where I display my upsell products.

The issue I have is where it displays a configurable product, it shows the product as out of stock since it is getting the stock status from the configurable product rather than checking the simple products that the configurable product holds.

The code that gets the stock status is:

<?php if($item->is_in_stock): ?>
   <span class="stock-status stock-status-in">In Stock</span></p>
<?php else: ?>
    <span class="stock-status stock-status-out">Out Of Stock</span></p>
<?php endif; ?>

The $item is populated by the following function:

public function prepareItem($item, $productId=null){

    $productId = is_null($productId) ? $item->getId() : $productId;

    $product = $this->objectManager->create('\Magento\Catalog\Model\ProductFactory')->create()->load($productId);

    $this->reviewFactory->getEntitySummary($product, $this->_storeManager->getStore()->getId());
    $specialPrice = ($product->getPrice() > $product->getFinalPrice()) ? true:false;

    $stockStatus =  $this->stockStateInterface->getStockQty($product->getEntityId()) > 0 ? true:false;

    $tmp = [
        'name'=>$product->getName(),
        'is_in_stock' => $stockStatus,
        'price'=>$this->priceHelper->currency(number_format($product->getPrice(), 2), true, false),
        'final_price'=>$this->priceHelper->currency(number_format($product->getFinalPrice(), 2), true, false),
        'has_special_price' =>$specialPrice,
        'url'=>$this->_storeManager->getStore()->getUrl($product->getProductUrl()),
        'rating_summary' => $product->getRatingSummary()->getRatingSummary(),
        'images' => $this->getImageCollection($product),
        'type_id' => $product->getData('type_id')

    ];

    if($product->getData('type_id') == 'grouped' ){
       $tmp['grouped_price'] = 'Starting at '.$this->getGroupedPrice($product);
    }

    return (object)$tmp;
}

I want to update where the $stockStatus variable so that it checks whether the product is a configurable product and if so, check to see whether any of the simple products for the configurable product are in stock and return true or false.

I would like to create another function that would loop through the configurable product simple products and check whether these are in stock by updating where the $stockStatus is set to the following:

if ($product->getData('type_id') == 'configurable') {
    $stockStatus = $this->getConfigurableStockStatus($product);
} else {
    $stockStatus =  $this->stockStateInterface->getStockQty($product->getEntityId()) > 0 ? true:false;
}

New function:

public function getConfigurableStockStatus($product){

}

I am unsure how I can loop through simple products and if all these are all out of stock return false otherwise return true within the getConfigurableStockStatus function?

Any help would be appreciated.

1 Answer 1

0

Managed to get it working :)

/**
 * @param $product
 * @return boolean
 */
public function getConfigurableStockStatus($product){

     $simpleProducts = $product->getTypeInstance()->getUsedProducts($product);

     $tmp = [];
     foreach ( $simpleProducts as $simpleProduct) {
         var_dump($this->stockStateInterface->getStockQty($simpleProduct->getEntityId()));

         $qty = $this->stockStateInterface->getStockQty($simpleProduct->getEntityId());

         if($qty > 0){
             $tmp[] = $qty;
         }

     }

     return !empty($tmp);

}

Your Answer

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

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