本文整理匯總了PHP中Magento\CatalogImportExport\Model\Import\Product::setSource方法的典型用法代碼示例。如果您正苦於以下問題:PHP Product::setSource方法的具體用法?PHP Product::setSource怎麽用?PHP Product::setSource使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Magento\CatalogImportExport\Model\Import\Product
的用法示例。
在下文中一共展示了Product::setSource方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: testBundleImport
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testBundleImport()
{
// import data from CSV file
$pathToFile = __DIR__ . '/../../_files/import_bundle.csv';
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData();
$this->assertTrue($errors->getErrorsCount() == 0);
$this->model->importData();
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
$this->assertTrue(is_numeric($productId));
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($productId);
$this->assertFalse($product->isObjectNew());
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
$this->assertEquals(1, $product->getShipmentType());
$optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList);
$bundleOptionCollection = $product->getExtensionAttributes()->getBundleProductOptions();
$this->assertEquals(2, count($bundleOptionCollection));
foreach ($bundleOptionCollection as $optionKey => $option) {
$this->assertEquals('checkbox', $option->getData('type'));
$this->assertEquals('Option ' . ($optionKey + 1), $option->getData('title'));
$this->assertEquals(self::TEST_PRODUCT_NAME, $option->getData('sku'));
$this->assertEquals($optionKey + 1, count($option->getData('product_links')));
foreach ($option->getData('product_links') as $linkKey => $productLink) {
$optionSku = 'Simple ' . ($optionKey + 1 + $linkKey);
$this->assertEquals($optionIdList[$optionSku], $productLink->getData('entity_id'));
$this->assertEquals($optionSku, $productLink->getData('sku'));
}
}
}
示例2: testImportReplace
/**
* @magentoDataFixture Magento/AdvancedPricingImportExport/_files/create_products.php
* @magentoAppArea adminhtml
*/
public function testImportReplace()
{
// import data from CSV file
$pathToFile = __DIR__ . '/_files/import_advanced_pricing.csv';
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE, 'entity' => 'advanced_pricing'])->validateData();
$this->assertEquals(0, $errors->getErrorsCount(), 'Advanced pricing import validation error');
$this->model->importData();
/** @var \Magento\Catalog\Model\ResourceModel\Product $resource */
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
$productIdList = $resource->getProductsIdsBySkus(array_keys($this->expectedTierPrice));
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
foreach ($productIdList as $sku => $productId) {
$product->load($productId);
$tierPriceCollection = $product->getTierPrices();
$this->assertEquals(3, count($tierPriceCollection));
/** @var \Magento\Catalog\Model\Product\TierPrice $tierPrice */
foreach ($tierPriceCollection as $tierPrice) {
$this->assertContains($tierPrice->getData(), $this->expectedTierPrice[$sku]);
}
}
}
示例3: testProductWithLinks
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testProductWithLinks()
{
$linksData = ['upsell' => ['simple1' => '3', 'simple3' => '1'], 'crosssell' => ['simple2' => '1', 'simple3' => '2'], 'related' => ['simple1' => '2', 'simple2' => '1']];
// import data from CSV file
$pathToFile = __DIR__ . '/_files/products_to_import_with_product_links.csv';
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Framework\\Filesystem');
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create('\\Magento\\ImportExport\\Model\\Import\\Source\\Csv', ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->_model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData();
$this->assertTrue($errors->getErrorsCount() == 0);
$this->_model->importData();
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$resource = $objectManager->get('Magento\\Catalog\\Model\\ResourceModel\\Product');
$productId = $resource->getIdBySku('simple4');
/** @var \Magento\Catalog\Model\Product $product */
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
$product->load($productId);
$productLinks = ['upsell' => $product->getUpSellProducts(), 'crosssell' => $product->getCrossSellProducts(), 'related' => $product->getRelatedProducts()];
$importedProductLinks = [];
foreach ($productLinks as $linkType => $linkedProducts) {
foreach ($linkedProducts as $linkedProductData) {
$importedProductLinks[$linkType][$linkedProductData->getSku()] = $linkedProductData->getPosition();
}
}
$this->assertEquals($linksData, $importedProductLinks);
}
示例4: testProductDuplicateCategories
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
* @magentoAppIsolation enabled
* @magentoDataFixture Magento/Catalog/_files/category_duplicates.php
*/
public function testProductDuplicateCategories()
{
$csvFixture = 'products_duplicate_category.csv';
// import data from CSV file
$pathToFile = __DIR__ . '/_files/' . $csvFixture;
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Framework\Filesystem::class);
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->_model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData();
$this->assertTrue($errors->getErrorsCount() === 0);
/** @var \Magento\Catalog\Model\Category $category */
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
$category->setStoreId(1);
$category->load(444);
$this->assertTrue($category !== null);
$category->setName('Category 2-updated')->save();
$this->_model->importData();
$errorProcessor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregator::class);
$errorCount = count($errorProcessor->getAllErrors());
$errorMessage = $errorProcessor->getAllErrors()[0]->getErrorMessage();
$this->assertContains('URL key for specified store already exists', $errorMessage);
$this->assertContains('Default Category/Category 2', $errorMessage);
$this->assertTrue($errorCount === 1, 'Error expected');
$categoryAfter = $this->loadCategoryByName('Category 2');
$this->assertTrue($categoryAfter === null);
/** @var \Magento\Catalog\Model\Product $product */
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
$product->load(1);
$categories = $product->getCategoryIds();
$this->assertTrue(count($categories) == 1);
}
示例5: testProductWithInvalidWeight
/**
* @magentoDbIsolation enabled
*/
public function testProductWithInvalidWeight()
{
// import data from CSV file
$pathToFile = __DIR__ . '/_files/product_to_import_invalid_weight.csv';
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Framework\\Filesystem');
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = new \Magento\ImportExport\Model\Import\Source\Csv($pathToFile, $directory);
$validationResult = $this->_model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND])->isDataValid();
$expectedErrors = ["Product weight is invalid" => [2]];
$this->assertFalse($validationResult);
$this->assertEquals($expectedErrors, $this->_model->getErrorMessages());
}
示例6: testImport
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testImport()
{
// Import data from CSV file
$pathToFile = __DIR__ . '/../../_files/grouped_product.csv';
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData();
$this->assertTrue($errors->getErrorsCount() == 0);
$this->model->importData();
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
$productId = $resource->getIdBySku('Test Grouped');
$this->assertTrue(is_numeric($productId));
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($productId);
$this->assertFalse($product->isObjectNew());
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
$childProductCollection = $product->getTypeInstance()->getAssociatedProducts($product);
foreach ($childProductCollection as $childProduct) {
$this->assertContains($childProduct->getSku(), $this->optionSkuList);
}
}
示例7: testValidateInvalidMultiselectValues
/**
* @magentoDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php
*/
public function testValidateInvalidMultiselectValues()
{
// import data from CSV file
$pathToFile = __DIR__ . '/_files/products_with_invalid_multiselect_values.csv';
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Framework\\App\\Filesystem');
$directory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::ROOT_DIR);
$source = new \Magento\ImportExport\Model\Import\Source\Csv($pathToFile, $directory);
$validationResult = $this->_model->setSource($source)->setParameters(array('behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND))->isDataValid();
$this->assertFalse($validationResult);
$errors = $this->_model->getErrorMessages();
$expectedErrors = array("Please correct the value for 'multiselect_attribute'." => [2], "Orphan rows that will be skipped due default row errors" => [3, 4]);
foreach ($expectedErrors as $message => $invalidRows) {
$this->assertArrayHasKey($message, $errors);
$this->assertEquals($invalidRows, $errors[$message]);
}
}
示例8: testProductCategories
/**
* @magentoAppArea adminhtml
* @dataProvider categoryTestDataProvider
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testProductCategories($fixture, $separator)
{
// import data from CSV file
$pathToFile = __DIR__ . '/_files/' . $fixture;
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Framework\\Filesystem');
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create('\\Magento\\ImportExport\\Model\\Import\\Source\\Csv', ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->_model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product', Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR => $separator])->validateData();
$this->assertTrue($errors->getErrorsCount() == 0);
$this->_model->importData();
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$resource = $objectManager->get('Magento\\Catalog\\Model\\ResourceModel\\Product');
$productId = $resource->getIdBySku('simple1');
$this->assertTrue(is_numeric($productId));
/** @var \Magento\Catalog\Model\Product $product */
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
$product->load($productId);
$this->assertFalse($product->isObjectNew());
$categories = $product->getCategoryIds();
$this->assertTrue(count($categories) == 2);
}
示例9: testDownloadableImport
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testDownloadableImport()
{
// import data from CSV file
$pathToFile = __DIR__ . '/../../_files/import_downloadable.csv';
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData();
$this->assertTrue($errors->getErrorsCount() == 0);
$this->model->importData();
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
$this->assertTrue(is_numeric($productId));
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($productId);
$this->assertFalse($product->isObjectNew());
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
$downloadableProductLinks = $product->getExtensionAttributes()->getDownloadableProductLinks();
$downloadableLinks = $product->getDownloadableLinks();
$downloadableProductSamples = $product->getExtensionAttributes()->getDownloadableProductSamples();
$downloadableSamples = $product->getDownloadableSamples();
//TODO: Track Fields: id, link_id, link_file and sample_file)
$expectedLinks = ['file' => ['title' => 'TEST Import Link Title File', 'sort_order' => '78', 'sample_type' => 'file', 'price' => '123.0000', 'number_of_downloads' => '123', 'is_shareable' => '0', 'link_type' => 'file'], 'url' => ['title' => 'TEST Import Link Title URL', 'sort_order' => '42', 'sample_type' => 'url', 'sample_url' => 'http://www.bing.com', 'price' => '1.0000', 'number_of_downloads' => '0', 'is_shareable' => '1', 'link_type' => 'url', 'link_url' => 'http://www.google.com']];
foreach ($downloadableProductLinks as $link) {
$actualLink = $link->getData();
$this->assertArrayHasKey('link_type', $actualLink);
foreach ($expectedLinks[$actualLink['link_type']] as $expectedKey => $expectedValue) {
$this->assertArrayHasKey($expectedKey, $actualLink);
$this->assertEquals($actualLink[$expectedKey], $expectedValue);
}
}
foreach ($downloadableLinks as $link) {
$actualLink = $link->getData();
$this->assertArrayHasKey('link_type', $actualLink);
$this->assertArrayHasKey('product_id', $actualLink);
$this->assertEquals($actualLink['product_id'], $product->getData($this->productMetadata->getLinkField()));
foreach ($expectedLinks[$actualLink['link_type']] as $expectedKey => $expectedValue) {
$this->assertArrayHasKey($expectedKey, $actualLink);
$this->assertEquals($actualLink[$expectedKey], $expectedValue);
}
}
//TODO: Track Fields: id, sample_id and sample_file)
$expectedSamples = ['file' => ['title' => 'TEST Import Sample File', 'sort_order' => '178', 'sample_type' => 'file'], 'url' => ['title' => 'TEST Import Sample URL', 'sort_order' => '178', 'sample_type' => 'url', 'sample_url' => 'http://www.yahoo.com']];
foreach ($downloadableProductSamples as $sample) {
$actualSample = $sample->getData();
$this->assertArrayHasKey('sample_type', $actualSample);
foreach ($expectedSamples[$actualSample['sample_type']] as $expectedKey => $expectedValue) {
$this->assertArrayHasKey($expectedKey, $actualSample);
$this->assertEquals($actualSample[$expectedKey], $expectedValue);
}
}
foreach ($downloadableSamples as $sample) {
$actualSample = $sample->getData();
$this->assertArrayHasKey('sample_type', $actualSample);
$this->assertArrayHasKey('product_id', $actualSample);
$this->assertEquals($actualSample['product_id'], $product->getData($this->productMetadata->getLinkField()));
foreach ($expectedSamples[$actualSample['sample_type']] as $expectedKey => $expectedValue) {
$this->assertArrayHasKey($expectedKey, $actualSample);
$this->assertEquals($actualSample[$expectedKey], $expectedValue);
}
}
}
示例10: testConfigurableImport
/**
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
* @magentoAppArea adminhtml
*/
public function testConfigurableImport()
{
// import data from CSV file
$pathToFile = __DIR__ . '/../../_files/import_configurable.csv';
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$source = $this->objectManager->create(\Magento\ImportExport\Model\Import\Source\Csv::class, ['file' => $pathToFile, 'directory' => $directory]);
$errors = $this->model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'])->validateData();
$this->assertTrue($errors->getErrorsCount() == 0);
$this->model->importData();
/** @var \Magento\Catalog\Model\ResourceModel\Product $resource */
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
$this->assertTrue(is_numeric($productId));
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->get(ProductRepositoryInterface::class)->getById($productId);
$this->assertFalse($product->isObjectNew());
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
$optionCollection = $product->getTypeInstance()->getConfigurableOptions($product);
foreach ($optionCollection as $option) {
foreach ($option as $optionData) {
$this->assertContains($optionData['sku'], $this->optionSkuList);
}
}
$optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList);
foreach ($optionIdList as $optionId) {
$this->assertArrayHasKey($optionId, $product->getExtensionAttributes()->getConfigurableProductLinks());
}
$configurableOptionCollection = $product->getExtensionAttributes()->getConfigurableProductOptions();
$this->assertEquals(1, count($configurableOptionCollection));
foreach ($configurableOptionCollection as $option) {
$optionData = $option->getData();
$this->assertArrayHasKey('product_super_attribute_id', $optionData);
$this->assertArrayHasKey('product_id', $optionData);
$this->assertEquals($product->getData($this->productMetadata->getLinkField()), $optionData['product_id']);
$this->assertArrayHasKey('attribute_id', $optionData);
$this->assertArrayHasKey('position', $optionData);
$this->assertArrayHasKey('extension_attributes', $optionData);
$this->assertArrayHasKey('product_attribute', $optionData);
$productAttributeData = $optionData['product_attribute']->getData();
$this->assertArrayHasKey('attribute_id', $productAttributeData);
$this->assertArrayHasKey('entity_type_id', $productAttributeData);
$this->assertArrayHasKey('attribute_code', $productAttributeData);
$this->assertEquals('test_configurable', $productAttributeData['attribute_code']);
$this->assertArrayHasKey('frontend_label', $productAttributeData);
$this->assertEquals('Test Configurable', $productAttributeData['frontend_label']);
$this->assertArrayHasKey('label', $optionData);
$this->assertEquals('test_configurable', $optionData['label']);
$this->assertArrayHasKey('use_default', $optionData);
$this->assertArrayHasKey('options', $optionData);
$this->assertEquals('Option 1', $optionData['options'][0]['label']);
$this->assertEquals('Option 1', $optionData['options'][0]['default_label']);
$this->assertEquals('Option 1', $optionData['options'][0]['store_label']);
$this->assertEquals('Option 2', $optionData['options'][1]['label']);
$this->assertEquals('Option 2', $optionData['options'][1]['default_label']);
$this->assertEquals('Option 2', $optionData['options'][1]['store_label']);
$this->assertArrayHasKey('values', $optionData);
$valuesData = $optionData['values'];
$this->assertEquals(2, count($valuesData));
}
}