What does this PHP line mean: 'is_in_stock' => $itemColourSizeFlat->getQuantityAvailable() > 0 ? 1 : 0,

magento.stackexchange https://magento.stackexchange.com/questions/94420

  •  20-10-2020
  •  | 
  •  

Question

I need help understanding this line of code and ultimately converting it to one that solves the problem I am facing.

'is_in_stock' => $itemColourSizeFlat->getQuantityAvailable() > 0 ? 1 : 0,

It is an extract from my inventory update process. The problem I have is when I sync my inventory with my external system, products with qty <1 are marked "out of stock". This is a problem because some products have a backorder status of "allow backorders". If the status is allow backorders (or allow backorders and notify customer) I want the stock status to remain "in stock".

for context, this line of code sites together with these lines:

/** * Prepare data for simple products. * * @param $itemColourSizeFlat * @param string $mode * @return mixed */ protected function _prepareDataSimple ($itemColourSizeFlat, $mode = self::MODE_CREATE) { $preparedAttributes = $this->_prepareData($itemColourSizeFlat); $preparedAttributes = $this->_prepareDataSimpleFinalPrice($itemColourSizeFlat, $preparedAttributes); $preparedAttributes['type_id'] = Mage_Catalog_Model_Product_Type::TYPE_SIMPLE; $preparedAttributes['stock_data'] = array( 'is_in_stock' => $itemColourSizeFlat->getQuantityAvailable() > 0 ? 1 : 0, 'qty' => $itemColourSizeFlat->getQuantityAvailable(), );

    foreach ($this->_simpleProductNameUniqueness as $attribute => $originalAttribute)
    {
        if (!array_key_exists($attribute, $preparedAttributes))
        {
            continue;
        }

        // @TODO FIX THIS usingaccumulated data per product group..
        $preparedAttributes['name'] = $preparedAttributes['name'] .
            ' ' .
            $preparedAttributes[$attribute.self::LABEL_DATA_APPEND];
    }</code>

Background. I'm not a developer and my dev is not contactable today. I really need to fix this, so I'm trying to teach myself PHP today to do so. It is very hard so far! Any help would be very much appreciated

Was it helpful?

Solution

Since you're not a PHP programmer I'll start with the solution. If you want to know why it works I'll have an explanation below.

Solution

Since I don't know your code I have to assume you can get the value of "allow backorders" into a variable. For this example I'll call it $allowBackorders.

What you want to do is check if quantity > 0 OR $allowBackorders is true then mark as in stock.

'is_in_stock' => (
                  $itemColourSizeFlat->getQuantityAvailable() > 0
                  || $allowBackorders == true
                 ) ? 1 : 0

Explanation

I'll breakdown your original 'is_in_stock' => $itemColourSizeFlat->getQuantityAvailable() > 0 ? 1 : 0 for you.

First: < expression > ? < value1 > : < value2 > is called a ternary operator. It's a shorthand way of writing "IF < expression > is true then use < value1 > OTHERWISE use < value2 >.

Next we'll look at < expression >. In your case it's $itemColourSizeFlat->getQuantityAvailable() > 0. What this is doing is checking if the value of the $itemColourSizeFlat object's quantity_available data is larger than 0. Since I don't know anything about that object I'm going to assume from it's name this is the number of items you have in stock.

Finally 'is_in_stock' => ... means that whatever you just calculated to the right is being assigned to the is_in_stock array key.

Putting it together your code is checking if quantity available is greater than 0 and if so set is_in_stock to 1 (true, yes). If it's not set it to 0 (false, no).

The change I made tells your ternary operator to check if EITHER quantity > 0 OR $allowBackorders is true in which case use 1, OTHERWISE use 0.

The || is php for the concept of OR and the parenthesis make sure everything happens in the correct order. If you want to know more these are covered in most beginners PHP tutorials.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top