0

I'm using Magento 2 as PIM for other shop solution. I need to export the backorders value once per hour until we have made an API.

The script below works fine, but values for 'backorders' are not exported. How can I fix that?

<?php
require './app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$registry = $_objectManager->get('Magento\Framework\Registry');
$registry->register('isSecureArea', true);

//Store id of exported products, This is useful when we have multiple stores. 
$store_id = 0;

$fp = fopen("export2.csv","w+");
$collection = $_objectManager- 
>create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')
->create()->addStoreFilter($store_id)
->addAttributeToFilter('type_id', 'simple')
->addAttributeToSelect(array('updated_at','sku','status','backorders','price'));

foreach ($collection as $product)
{
$data = array();
$data[] = $product->getUpdated_at();
$data[] = $product->getSku();
$data[] = $product->getStatus();
$data[] = $product->getBackorders(); (doesn´t export value)
$data[] = $product->getPrice();

fputcsv($fp, $data);    
}
fclose($fp);
?>
  • So do you have these as product attributes ? – Abdul Pathan Sep 1 '19 at 6:03
  • Both attributes should be system attributes. I found that "product_online" is instead "status". Do you know what to use for "backorders" with the dropdown values 1) "No Backorders, 2) Allow Qty Below 0, 3) Allow Qty Below 0 and notify customer)? – Thomas Lester Sep 1 '19 at 12:30
1

Try this.

<?php
require './app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$registry = $_objectManager->get('Magento\Framework\Registry');
$registry->register('isSecureArea', true);

//Store id of exported products, This is useful when we have multiple stores. 
$store_id = 0;

$fp = fopen("export2.csv","w+");
$collection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create()->addStoreFilter($store_id)->addAttributeToFilter('type_id', 'simple')->addAttributeToSelect(array('updated_at','sku','product_online','backorders','price'));

foreach ($collection as $prod)
{
$product = $_objectManager->create('Magento\Catalog\Model\Product')->load($prod->getId());
$data = array();
$data[] = $product->getUpdated_at();
$data[] = $product->getSku();
$data[] = $product->getStatus(); // For your product status

$stockRegistry = $_objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
$stockitem = $stockRegistry->getStockItem($product->getId(),$product->getStore()->getWebsiteId());
 // Use this if you need value as 0,1,2
$data[] = $stockitem->getBackorders();
// Use this if you need string value
$data[] = ($stockitem->getBackorders()) ? ($stockitem->getBackorders() == 1) ? "Allow Qty Below 0" : "Allow Qty Below 0 and Notify Customer" : "No Backorders"; 
$data[] = $product->getPrice();
fputcsv($fp, $data);    
}
fclose($fp);
?>
| improve this answer | |
  • Hello Abdul. Thank you for your suggestion. I found that the attribute "status" was what I looked for instead of "product_online" and the values are now exported in both your and my example. But the values for "backorders" are not. Seems that has to be extracted in an other way. stackoverflow.com/questions/44419558/…. Do you know how to get that info in my case.0: "No Backorders" 1: "Allow Qty Below 0 " 2: "Allow Qty Below 0 and Notify Customer" – Thomas Lester Sep 1 '19 at 13:42
  • So you wanna know whats the Backorder setting set for the product from the above three options ? – Abdul Pathan Sep 4 '19 at 4:50
  • @ThomasLester, please check the updated answer. – Abdul Pathan Sep 4 '19 at 5:12

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.