Skip to content

Instantly share code, notes, and snippets.

@JosephMaxwell
Created August 4, 2021 13:49
Show Gist options
  • Save JosephMaxwell/f2209021e8ccc51a137fe3fa173d478c to your computer and use it in GitHub Desktop.
Save JosephMaxwell/f2209021e8ccc51a137fe3fa173d478c to your computer and use it in GitHub Desktop.
Fast Simple Product
<?php
declare(strict_types=1);
/**
* @by SwiftOtter, Inc., 2019/10/24
* @website https://swiftotter.com
**/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
$collection->addFieldToFilter('sku', 'simple')
->addAttributeToSelect([
// Attributes to verify
'cost',
]);
$compareProduct = $collection->getFirstItem();
if ($compareProduct->getData('cost') != 0.90
// verify other attributes here
) {
include __DIR__ . '/simple_product_rollback.php';
/** @var $product \Magento\Catalog\Model\Product */
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setId(1)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Simple Product')
->setSku('simple')
->setPrice(10)
->setWeight(1)
->setTaxClassId(0)
->setDescription('Description with <b>html tag</b>')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setStockData(
['use_config_manage_stock' => 0, 'manage_stock' => 0, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 100]
);
$product->setCustomAttribute('cost', 0.90);
// configure needed custom attributes here
$product->save();
return $product;
} else {
return $compareProduct;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment