0
<?php


namespace Devbatch\UpdateQuantity\Cron;

/**
 * Class UpdateItem
 *
 * @package DevBatch\UpdateQuantity\Cron
 */
class UpdateProduct
{

    protected $logger;

    /**
     * Constructor
     *
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(\Psr\Log\LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * Execute the cron
     *
     * @return void
     */
    public function execute()
    {
        $this->logger->addInfo("Cronjob UpdateItem is executed.");
        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
        $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
        $collection = $productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $UPC;


        foreach ($collection as $product) { 

            $UPC=$product->getUpc();

            if($product->getUpc() != null ){

                if ($product->getItemNo() == null){

                    try {
                    $baseUrl = isset($wsdls["inspa"]["base"]) ? $wsdls["inspa"]["base"] : 'https://remote.salonservicesnw.com:7247/InSpa/WS/';
                    $uri = isset($wsdls["inspa"]["uri"]["sales_order"]) ? $wsdls["inspa"]["uri"]["sales_order"] : 'Salon%20Service/Page/ItemCrossRef';
                    $login = isset($wsdls["inspa"]["user"]["test"]) ? $wsdls["inspa"]["user"]["test"] : '********';
                    $password = isset($wsdls["inspa"]["pass"]["test"]) ? $wsdls["inspa"]["pass"]["test"] : '*******';    
                    // $arguments = getopt(null, ["sellToCustomer:","item:","quantity:","crossReference:"]);    
                    $context = stream_context_create([
                        'ssl' => [
                            // set some SSL/TLS specific options
                            'verify_peer' => false,
                            'verify_peer_name' => false,
                            'allow_self_signed' => true
                        ]
                    ]);

                    $wsdlOptions = array(
                        "login" => $login, 
                        "password" => $password,
                        "exceptions" => true,
                        "trace" => 1,
                        "cache_wsdl" => WSDL_CACHE_NONE,
                        "features" => SOAP_SINGLE_ELEMENT_ARRAYS, 
                        "stream_context" => $context
                    );

                    // Create new PHP SOAP client

                    $client = new \SoapClient($baseUrl.$uri, $wsdlOptions);
                    $read = new \stdClass();
                    $filter = new stdClass();
                    $filter->Field = "Cross_Reference_No";
                    $filter->Criteria = $UPC;
                    $read->setSize =0;
                    $read->bookmarkKey="";
                    $read->filter = $filter;

//Error here $item_no = $client->ReadMultiple($read)->ReadMultiple_Result->ItemCrossRef[0]->Item_No;

    $product->setItemNo($item_no);
                    $product->save();


                    } 
                    catch (Exception $e) {
                        echo "<h2>Exception Error!</h2>";
                        echo $e->getMessage();
                    }
                }
            }
        }
    }
}
  • I recommend creating magento api via service contracts, you will avoid stuff like this happening :) – Gosu Przmak Mar 6 at 14:42

Your Answer

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

Browse other questions tagged or ask your own question.