_eventManager = $eventManager; $this->sourceCustomerFactory = $sourceCustomerFactory; $this->customerCollection = $customerCollection; $this->sourceCustomerRepository = $sourceCustomerRepository; $this->attributeOption = $attributeOption; $this->logger = $logger; $this->netSuiteSync = $netSuiteSync; $this->sourceItemRepository = $sourceItemRepository; $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory; $this->getDistanceFromSourceToAddress = $getDistanceFromSourceToAddress; $this->addressInterfaceFactory = $addressInterfaceFactory; $this->orderRepository = $orderRepository; $this->sourceRepository = $sourceRepository; $this->csv = $csv; $this->dir = $dir; $this->productRepository = $productRepository; $this->orderCollection = $orderCollection; $this->productResource = $productResource; parent::__construct(); } protected function configure() { $this->setName('customer:code:test'); } protected function importProductNetsuiteId() { $path = $this->dir->getDir('Lts_Sales').'/Data/netsuite-product.csv'; if (!file_exists($path)) { echo sprintf('File %s does not exist', $path).PHP_EOL; } echo 'path: ' . $path."\r\n"; $netsuiteAttributeCode = 'netsuite_id'; try { foreach ($this->csv->getData($path) as $index =>$item) { if ($index === 0) { continue; } echo sprintf('row %d, sku: %s, netsuite id: %s', $index, $item[1], $item[0]).PHP_EOL; try { $product = $this->productRepository->get($item[1]); $product->setData($netsuiteAttributeCode, $item[0]); // 保存产品(使用资源模型直接保存属性,避免完全保存产品) $this->productResource->saveAttribute($product, $netsuiteAttributeCode); echo sprintf('update product %s netsuite id %s success', $item[1], $item[0])."\r\n"; } catch (NoSuchEntityException $e) { echo sprintf('update product %s netsuite id failed: %s', $item[1], $e->getMessage())."\r\n"; } } } catch (\Exception $exception){ echo sprintf('update product %s netsuite id %s success', $item[1], $item[0])."\r\n"; } } /** * @param \Magento\Sales\Api\Data\OrderAddressInterface $address * @return \Magento\InventorySourceSelectionApi\Api\Data\AddressInterface */ protected function addressConverter(\Magento\Sales\Api\Data\OrderAddressInterface $address) { if ($address->getStreet()) { $streets = implode(' ', $address->getStreet()); } return $this->addressInterfaceFactory->create([ 'country' => $address->getCountryId() ?? '', 'postcode' => $address->getPostcode() ?? '', 'street' => $streets ?? '', 'region' => $address->getRegion() ?? '', 'city' => $address->getCity() ?? '' ]); } protected function getInventorySourceFromProduct(string $sku) { $searchCriteria = $this->searchCriteriaBuilderFactory->create() ->addFilter(SourceItemInterface::SKU, $sku) ->addFilter(SourceItemInterface::SOURCE_CODE, 'default', 'neq') ->create(); return $this->sourceItemRepository->getList($searchCriteria)->getItems(); } protected function getWaitSyncOrders() { $criteria = $this->searchCriteriaBuilderFactory->create() ->addFilter('state', Order::STATE_NEW) ->addFilter('is_netsuite_sync', 0) ->create(); return $this->orderRepository->getList($criteria); } /** * @throws LocalizedException */ protected function execute(InputInterface $input, OutputInterface $output) { // $this->importProductNetsuiteId(); $this->netSuiteSync->execute(); // $waitSyncOrders = $this->getWaitSyncOrders(); // foreach ($waitSyncOrders->getItems() as $order) { // /* @var $order \Magento\Sales\Model\Order */ // echo $order->getEntityId().PHP_EOL; //// $order->setId($order->getId())->getExtensionAttributes()->setIsNetsuiteSync(1); //// $this->orderRepository->save($order); // } return 1; } }