0

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

2

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.

  • thank you. I'll try this now. I really appreciate you taking the time and breaking it down. Makes sense now. The backorders functionality is default magento, so I will now try to get the value of "allow backorders" into a variable as you say. Thanks again. – Wes Dec 20 '15 at 2:05
  • this link/answer looks like it might explain how to create the variable. Would it belong the the line somewhere in blank space? – Wes Dec 20 '15 at 2:19
  • Most likely. Obviously I can't be positive without a lot more of your code. If you use the two lines from that answer just before $preparedAttributes['stock_data'] = array( you'll have the best chance. Of course you want to capture what getBackorders() returns by using $allowBackorders = $inventory->getBackorders(); Do you know what to use for $_product from the answer you found?? – tbernard Dec 20 '15 at 2:24
  • thanks so much. I don't know what the equivalent of $_product is for my code so I think I need to give up and wait for my dev to come back into contact next week. I really appreciate you taking the time though. – Wes Dec 20 '15 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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