Created
July 26, 2019 06:02
-
-
Save atapatel/9f58a18bce5522f0586151a6a804c3ff to your computer and use it in GitHub Desktop.
product qty sku.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Report simple running errors | |
error_reporting(E_ERROR | E_WARNING | E_PARSE); | |
use \Magento\Framework\App\Bootstrap; | |
include('app/bootstrap.php'); | |
$bootstrap = Bootstrap::create(BP, $_SERVER); | |
$objectManager = $bootstrap->getObjectManager(); | |
$instance = \Magento\Framework\App\ObjectManager::getInstance(); | |
$storeManager = $instance->get('\Magento\Store\Model\StoreManagerInterface'); | |
$state = $objectManager->get('\Magento\Framework\App\State'); // if you need state then uncomment below line of code | |
$state->setAreaCode('frontend'); | |
$store = $storeManager->getStore(); | |
$storeId = $store->getStoreId(); | |
$websiteId = $storeManager->getWebsite()->getWebsiteId(); | |
$soap_request = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:tem="http://tempuri.org/" | |
xmlns:won="http://schemas.datacontract.org/2004/07/Wondersoft.WCF.Integration"> | |
<soapenv:Header/> | |
<soapenv:Body> | |
<tem:SyncRequest> | |
<tem:CI> | |
<won:ClientID>1</won:ClientID> | |
<won:OutletID>1</won:OutletID> | |
<won:Password>admin</won:Password> | |
<won:TerminalID>1</won:TerminalID> | |
<won:UserName>admin</won:UserName> | |
</tem:CI> | |
<tem:MethodName>GetInventory</tem:MethodName> | |
<tem:XMLData> | |
<![CDATA[ | |
<Params> | |
<Location>2</Location> | |
<DateFilter></DateFilter> | |
</Params> | |
]]> | |
</tem:XMLData> | |
</tem:SyncRequest> | |
</soapenv:Body> | |
</soapenv:Envelope>'; | |
$header = array( | |
"Content-type: text/xml;charset=\"utf-8\"", | |
"Accept: text/xml", | |
"Cache-Control: no-cache", | |
"Pragma: no-cache", | |
"SOAPAction: \"http://tempuri.org/IGenericService/SyncRequest\"", | |
"Content-length: ".strlen($soap_request), | |
); | |
$soap_do = curl_init(); | |
curl_setopt($soap_do, CURLOPT_URL, "http://superkicksecom.eshopaid.com:802/IntegrationService.svc?wsdl" ); | |
//curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); | |
//curl_setopt($soap_do, CURLOPT_TIMEOUT, 10); | |
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($soap_do, CURLOPT_POST, true ); | |
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request); | |
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header); | |
$response = curl_exec($soap_do); | |
// Create array to store SKU and it's QTY | |
$skuArray = array(); | |
if($response === false) { | |
$err = 'Curl error: ' . curl_error($soap_do); | |
curl_close($soap_do); | |
print $err; | |
} else { | |
curl_close($soap_do); | |
$data1 = explode('<SyncRequestResult>', $response); | |
$data2 = explode('</SyncRequestResult>', $data1[1]); | |
$resultData = simplexml_load_string(htmlspecialchars_decode((string)$data2[0])); | |
$parseArry = $resultData->Data->Inventory->Items; | |
foreach ($parseArry as $key1 => $value1) { | |
$i=0; | |
foreach ($value1->Item as $key2 => $value2) { | |
// echo '===============<br>'; | |
// echo $value2->ItemCode; | |
// echo '<br>'; | |
// echo $value2->Stock; | |
// echo '<br>'; | |
$product = $objectManager->get('Magento\Catalog\Model\Product'); | |
if ($product->getIdBySku($value2->ItemCode)) { | |
$product = $objectManager->create('Magento\Catalog\Model\Product'); | |
$product->load($product->getIdBySku($value2->ItemCode)); | |
$stockInfo = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($product->getId()); | |
$stockqty = (int)$stockInfo->getQty(); | |
echo $i.' POS QTY - '.$value2->Stock.' . PRODUCT NAME - '.$product->getName().', SKU - '.$value2->ItemCode.' Before Update QTY - '.$stockqty; | |
echo '<br>'; | |
// Keep Storing the SKU and add it's QTY value. | |
$currentSku = $value2->ItemCode . ''; | |
if ( array_key_exists($currentSku, $skuArray) ) { | |
$skuArray[ ($currentSku) ] += $value2->Stock; | |
} else { | |
$skuArray[ ($currentSku) ] = $value2->Stock; | |
} | |
// $product->setStockData(['qty' => $value2->Stock, 'is_in_stock' => 1]); | |
// $product->setQuantityAndStockStatus(['qty' => $value2->Stock, 'is_in_stock' => 1]); | |
// $product->save(); | |
} | |
//if($i==1){ | |
// break; | |
//} | |
$i++; | |
} | |
} | |
// Now add the SKU and QTY from the array | |
echo "Aumkar's Test Starts::: "; | |
echo "<br> >>> Printing of skuArray begins: "; | |
echo "<table border='1'>"; | |
echo "<thead><tr><td>SKU</td><td>Stock QTY</td></tr></thead><tbody>"; | |
foreach ($skuArray as $itm_sku => $itm_qty) { | |
// echo "SKU: " . $key . ' -- ' . $value . '<br>'; | |
echo "<tr>"; | |
echo "<td style='padding: 5px;'>" . $itm_sku . "</td>"; | |
echo "<td style='padding: 5px;'>" . $itm_qty . "</td>"; | |
echo "</tr>"; | |
$product = $objectManager->create('Magento\Catalog\Model\Product'); | |
$product->load($product->getIdBySku( $itm_sku )); | |
$product->setStockData(['qty' => $itm_qty, 'is_in_stock' => 1]); | |
$product->setQuantityAndStockStatus(['qty' => $itm_qty, 'is_in_stock' => 1]); | |
$product->save(); | |
} | |
echo "</tbody></table>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment