857 lines
35 KiB
PHP
Executable File
857 lines
35 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\Prology\Helper;
|
|
|
|
use Magento\Catalog\Model\ProductRepository;
|
|
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
|
|
|
|
class ProductHelper
|
|
{
|
|
const INIT_COMPANIES = array("ING", "DIC");
|
|
|
|
protected $dir;
|
|
protected $productRepository;
|
|
protected $objectManager;
|
|
protected $productUrlPathGenerator;
|
|
protected $productUrlRewriteValidator;
|
|
protected $company_dropdown;
|
|
protected $brands_dropdown;
|
|
protected $types_dropdown;
|
|
|
|
public function __construct(
|
|
\Magento\Framework\Filesystem\DirectoryList $dir,
|
|
ProductRepository $productRepository,
|
|
) {
|
|
$this->dir = $dir;
|
|
$this->productRepository = $productRepository;
|
|
$this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
|
|
$this->productUrlPathGenerator = $this->objectManager->create('\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator');
|
|
$this->productUrlRewriteValidator = $this->objectManager->create('\Magento\CatalogUrlRewrite\Model\Product\Validator');
|
|
//$this->loadCompanyDropdown(self::INIT_COMPANIES);
|
|
$this->loadBrandsDropdown();
|
|
$this->loadTypesDropdown();
|
|
}
|
|
|
|
public function saveImageFromUrl($nameFile, $src) {
|
|
if(@getimagesize($src)){
|
|
file_put_contents($file = $this->dir->getPath('media') .'/images/'. $nameFile, file_get_contents($src));
|
|
//echo $this->dir->getPath('media') .'/images/'. $nameFile.'\n';
|
|
return $this->dir->getPath('media') .'/images/'. $nameFile;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public function getCompanyDropdown(){
|
|
return $this->company_dropdown;
|
|
}
|
|
|
|
public function loadCompanyDropdown($companies){
|
|
$this->company_dropdown = array();
|
|
$product = $this->objectManager->create('\Magento\Catalog\Model\Product');
|
|
$product_resource = $product->getResource();
|
|
$company_attribute = $product_resource->getAttribute('company');
|
|
if ($company_attribute->usesSource()) {
|
|
foreach ($companies as $company) {
|
|
$company_option_id = $company_attribute->getSource()->getOptionId($company);
|
|
|
|
$this->company_dropdown[$company] = $company_option_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getBrandsDropdown(){
|
|
return $this->brands_dropdown;
|
|
}
|
|
|
|
public function loadBrandsDropdown(){
|
|
$this->brands_dropdown = array();
|
|
$product = $this->objectManager->create('\Magento\Catalog\Model\Product');
|
|
$product_resource = $product->getResource();
|
|
$brands_attribute = $product_resource->getAttribute('brands');
|
|
if ($brands_attribute->usesSource()) {
|
|
$options = $brands_attribute->getSource()->getAllOptions();
|
|
foreach ($options as $option) {
|
|
$this->brands_dropdown[trim(strtoupper($option["label"]))] = $option["value"];
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getTypesDropdown(){
|
|
return $this->types_dropdown;
|
|
}
|
|
|
|
public function loadTypesDropdown(){
|
|
$this->types_dropdown = array();
|
|
$product = $this->objectManager->create('\Magento\Catalog\Model\Product');
|
|
$product_resource = $product->getResource();
|
|
$types_attribute = $product_resource->getAttribute('types');
|
|
if ($types_attribute->usesSource()) {
|
|
$options = $types_attribute->getSource()->getAllOptions();
|
|
foreach ($options as $option) {
|
|
$this->types_dropdown[trim(strtoupper($option["label"]))] = $option["value"];
|
|
}
|
|
}
|
|
}
|
|
|
|
public function validateUrlKeyConflicts($product)
|
|
{
|
|
try {
|
|
$this->productUrlRewriteValidator->validateUrlKeyConflicts($product);
|
|
} catch (UrlAlreadyExistsException $e) {
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function isExistProduct($sku)
|
|
{
|
|
$productObject = $this->objectManager->get('Magento\Catalog\Model\Product');
|
|
$product = $productObject->loadByAttribute("sku", $sku);
|
|
if ($product) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getUrlKey($product)
|
|
{
|
|
return $this->productUrlPathGenerator->getUrlKey($product);
|
|
}
|
|
|
|
/*
|
|
typeId = simple|virtual|downloadable|configurable
|
|
visibility = 4|1 0-> show 1->hide
|
|
chooseCondition = "NEW"|"REFURBISHED"
|
|
$imageUrls = ["url"]
|
|
$product = array(
|
|
"sku" => "huynh-test-1",
|
|
"name" => "huynh-test-1",
|
|
"categories" => [1422],
|
|
"description" => "description",
|
|
"shortDescription" => "shortDescription",
|
|
"brands" => "CISCO",
|
|
"types" => "ACCESSPOINT"
|
|
);
|
|
*/
|
|
public function createProductData($product, $price, $qty, $visibility, $typeId, $chooseCondition, $imageUrls){
|
|
if(!empty($chooseCondition)){
|
|
$product["sku"] = $product["sku"]."-".$chooseCondition;
|
|
}
|
|
$is_featured = 0;
|
|
if (isset($product["is_featured"]) && $product["is_featured"] == 1) {
|
|
$is_featured = 1;
|
|
}
|
|
$data = array(
|
|
"is_featured" => $is_featured,
|
|
"sku" => $product["sku"],
|
|
"name" => $product["name"],
|
|
"visibility" => $visibility,//0
|
|
"typeId" => $typeId,//
|
|
"price" => $price,
|
|
"categories" => $product["categories"],
|
|
"qty" => $qty,
|
|
"chooseCondition" => $chooseCondition,
|
|
"description" => $product["description"],
|
|
"shortDescription" => $product["shortDescription"],
|
|
"imageUrls" => $imageUrls,
|
|
"brands" => $product["brands"],
|
|
"types" => $product["types"]
|
|
);
|
|
return $data;
|
|
}
|
|
|
|
/*
|
|
$data = array(
|
|
"sku" => "sku",
|
|
"name" => "name",
|
|
"visibility" => 0,
|
|
"typeId" => "configurable",
|
|
"price" => 0,
|
|
"categories" => [],
|
|
"qty" => 0,
|
|
"chooseCondition" => "",
|
|
"description" => "description",
|
|
"shortDescription" => "shortDescription",
|
|
"imageUrls" => [],
|
|
"brands" => "",
|
|
"types" => ""
|
|
);
|
|
*/
|
|
public function createProduct($data)
|
|
{
|
|
$product = $this->objectManager->create('\Magento\Catalog\Model\Product');
|
|
if ($this->isExistProduct($data['sku'])) {
|
|
$this->deleteImageMedia($data['sku']);
|
|
$product = $product->getCollection()
|
|
->addFieldToSelect("*")
|
|
->addAttributeToFilter('sku', $data['sku'])
|
|
->addMediaGalleryData()
|
|
->getFirstItem();
|
|
}
|
|
$product->setStoreId(0);
|
|
$product->setSku($data['sku']); // Set your sku here
|
|
$product->setName($data["name"]); // Name of Product
|
|
$product->setAttributeSetId(4); // Attribute set id
|
|
$product->setStatus(1); // Status on product enabled/ disabled 1/0
|
|
$product->setWeight(1); // weight of product
|
|
$product->setVisibility($data["visibility"]);
|
|
//$product->setVisibility(1); // visibilty of product (catalog / search / catalog, search / Not visible individually)
|
|
$product->setTaxClassId(0); // Tax class id
|
|
$product->setTypeId($data["typeId"]);
|
|
//$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
|
|
$product->setPrice($data["price"]); // price of product
|
|
$product->setDescription($data["description"]);
|
|
$product->setShortDescription($data["shortDescription"]);
|
|
//$categories = array(4);
|
|
$product->setCategoryIds($data["categories"]);
|
|
if (empty($data["categories"])) {
|
|
$product->setCategoryIds([1936]);
|
|
}
|
|
$product->setWebsiteIds(array(1));
|
|
$product->setUrlKey($data['sku']);
|
|
$is_in_stock = 1;
|
|
if ( empty($data["qty"]) || $data["qty"] <= 0 ) {
|
|
$is_in_stock = 0;
|
|
$data["qty"] = 0;
|
|
}
|
|
|
|
$product->setStockData(
|
|
array(
|
|
'use_config_manage_stock' => 1,
|
|
'manage_stock' => 1,
|
|
'is_in_stock' => $is_in_stock,
|
|
'qty' => $data["qty"]
|
|
)
|
|
);
|
|
|
|
$product->setUrlKey($this->getUrlKey($product));
|
|
|
|
if ($this->validateUrlKeyConflicts($product)) {
|
|
return null;
|
|
}
|
|
|
|
$product->setData('is_featured', $data["is_featured"]);
|
|
|
|
if (!empty($data["brands"]) && isset($this->brands_dropdown[strtoupper($data["brands"])]) && $this->brands_dropdown[strtoupper($data["brands"])] != "") {
|
|
$product->setData('brands', $this->brands_dropdown[strtoupper($data["brands"])]);
|
|
}
|
|
|
|
if (!empty($data["types"]) && isset($this->types_dropdown[strtoupper($data["types"])]) && $this->types_dropdown[strtoupper($data["types"])] != "") {
|
|
$product->setData('types', $this->types_dropdown[strtoupper($data["types"])]);
|
|
}
|
|
|
|
if ( !empty($data["chooseCondition"])) {
|
|
$product_resource = $product->getResource();
|
|
$choose_condition_attribute = $product_resource->getAttribute('choose_condition');
|
|
if ($choose_condition_attribute->usesSource()) {
|
|
$choose_condition_option_id = $choose_condition_attribute->getSource()->getOptionId($data["chooseCondition"]);
|
|
if ($choose_condition_option_id != '') {
|
|
$product->setData('choose_condition', $choose_condition_option_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
for ($i = 0; $i < count($data["imageUrls"]); $i++) {
|
|
$imageUrl = $data["imageUrls"][$i];
|
|
$arr_type = explode(".",$imageUrl);
|
|
$imagePath = $this->saveImageFromUrl($product->getUrlKey()."_".$data["chooseCondition"].$i.".".$arr_type[count($arr_type)-1], $imageUrl);
|
|
if (!empty($imagePath)){
|
|
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
|
|
|
|
}
|
|
}
|
|
$product->save();
|
|
return $product;
|
|
|
|
}
|
|
|
|
public function deleteImageMedia($sku)
|
|
{
|
|
$product = $this->objectManager->create('\Magento\Catalog\Model\Product');
|
|
$product = $product->getCollection()
|
|
->addFieldToSelect("*")
|
|
->addAttributeToFilter('sku', $sku)
|
|
->addMediaGalleryData()
|
|
->getFirstItem();
|
|
$productRepository = $this->objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
|
|
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
|
|
foreach ($existingMediaGalleryEntries as $key => $entry) {
|
|
unset($existingMediaGalleryEntries[$key]);
|
|
}
|
|
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
|
|
$productRepository->save($product);
|
|
|
|
}
|
|
|
|
/*
|
|
$groupDataProduct = array(
|
|
"sku" => "huynh-test",
|
|
"name" => "name",
|
|
"categories" => array(1422),
|
|
"description" => "description",
|
|
"shortDescription" => "short description",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 2,
|
|
"price" => 12.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 3,
|
|
"price" => 13.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
)
|
|
);
|
|
*/
|
|
public function createConfigurableProduct($groupDataProduct)
|
|
{
|
|
$isUpdate = false;
|
|
if ($this->isExistProduct($groupDataProduct["sku"])) {
|
|
$isUpdate = true;
|
|
}
|
|
//create group product
|
|
//createProductData($product, $price, $qty, $visibility, $typeId, $chooseCondition, $imageUrls
|
|
$dataProduct = $this->createProductData($groupDataProduct,
|
|
1,
|
|
1,
|
|
4,
|
|
'configurable',
|
|
"",
|
|
$groupDataProduct["imageUrls"]
|
|
);
|
|
//echo json_encode($dataProduct)."\n";
|
|
$group_product = $this->createProduct($dataProduct);
|
|
if ($group_product == null) {
|
|
return null;
|
|
}
|
|
|
|
//create product condition NEW
|
|
$NEW = null;
|
|
if (!$isUpdate) {
|
|
|
|
$dataProduct = $this->createProductData(
|
|
$groupDataProduct,
|
|
$groupDataProduct["condition"]["new"]["price"],
|
|
1,// default not equal 0
|
|
1,
|
|
'simple',
|
|
"NEW",
|
|
$groupDataProduct["condition"]["new"]["imageUrls"]
|
|
);
|
|
//echo json_encode($dataProduct)."\n";
|
|
$NEW = $this->createProduct($dataProduct);
|
|
} else {
|
|
$dataProduct = $this->createProductData(
|
|
$groupDataProduct,
|
|
$groupDataProduct["condition"]["new"]["price"],
|
|
$groupDataProduct["condition"]["new"]["qty"],
|
|
1,
|
|
'simple',
|
|
"NEW",
|
|
$groupDataProduct["condition"]["new"]["imageUrls"]
|
|
);
|
|
//echo json_encode($dataProduct)."\n";
|
|
$NEW = $this->createProduct($dataProduct);
|
|
}
|
|
|
|
|
|
|
|
//create product condition REFURBISHED
|
|
$dataProduct = $this->createProductData(
|
|
$groupDataProduct,
|
|
$groupDataProduct["condition"]["refurbished"]["price"],
|
|
$groupDataProduct["condition"]["refurbished"]["qty"],
|
|
1,
|
|
'simple',
|
|
"REFURBISHED",
|
|
$groupDataProduct["condition"]["refurbished"]["imageUrls"]
|
|
);
|
|
//echo json_encode($dataProduct)."\n";
|
|
$REFURBISHED = $this->createProduct($dataProduct);
|
|
|
|
//create product condition NEW OTHER
|
|
$dataProduct = $this->createProductData(
|
|
$groupDataProduct,
|
|
$groupDataProduct["condition"]["new_other"]["price"],
|
|
$groupDataProduct["condition"]["new_other"]["qty"],
|
|
1,
|
|
'simple',
|
|
"NEW OTHER",
|
|
$groupDataProduct["condition"]["new_other"]["imageUrls"]
|
|
);
|
|
//echo json_encode($dataProduct)."\n";
|
|
$NEW_OTHER = $this->createProduct($dataProduct);
|
|
|
|
if (!$isUpdate) {
|
|
$attributeModel = $this->objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
|
|
$position = 0;
|
|
$attributes = array(139); // Super Attribute Ids Used To Create Configurable Product
|
|
$associatedProductIds = array($NEW->getId(), $REFURBISHED->getId(), $NEW_OTHER->getId()); //Product Ids Of Associated Products
|
|
foreach ($attributes as $attributeId) {
|
|
$data = array('attribute_id' => $attributeId, 'product_id' => $group_product->getId(), 'position' => $position);
|
|
$position++;
|
|
$attributeModel->setData($data)->save();
|
|
}
|
|
$group_product->setTypeId("configurable"); // Setting Product Type As Configurable
|
|
$group_product->setAffectConfigurableProductAttributes(4);
|
|
$this->objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $group_product);
|
|
$group_product->setNewVariationsAttributeSetId(4); // Setting Attribute Set Id
|
|
$group_product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
|
|
$group_product->setCanSaveConfigurableAttributes(true);
|
|
}
|
|
|
|
$group_product->save();
|
|
if (!$isUpdate) {
|
|
$is_in_stock = 1;
|
|
$productQty = $groupDataProduct["condition"]["new"]["qty"];
|
|
if ( empty($groupDataProduct["condition"]["new"]["qty"]) || $groupDataProduct["condition"]["new"]["qty"] <= 0 ) {
|
|
$is_in_stock = 0;
|
|
$productQty = 0;
|
|
}
|
|
$NEW->setQuantityAndStockStatus(['qty' => $productQty, 'is_in_stock' => $is_in_stock]);
|
|
|
|
$NEW->save();
|
|
|
|
$stockModel = $this->objectManager->get('Magento\CatalogInventory\Model\Stock\ItemFactory')->create();
|
|
$stockResource = $this->objectManager->get('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
|
|
$stockResource->load($stockModel, $NEW->getId(),"product_id");
|
|
$stockModel->setQty($productQty);
|
|
$stockResource->save($stockModel);
|
|
}
|
|
return $group_product;
|
|
}
|
|
|
|
/*
|
|
$groupDataProduct = array(
|
|
"sku" => "huynh-test-01",
|
|
"name" => "huynh-test-01",
|
|
"categories" => array(1422),
|
|
"description" => "description",
|
|
"shortDescription" => "short description",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 10,
|
|
"price" => 20.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 15,
|
|
"price" => 19.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
),
|
|
"relatedProduct" => array(
|
|
array(
|
|
"sku" => "huynh-test-02",
|
|
"name" => "huynh-test-02",
|
|
"categories" => array(1422),
|
|
"description" => "description",
|
|
"shortDescription" => "short description",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 5,
|
|
"price" => 15.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 3,
|
|
"price" => 13.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
)
|
|
)
|
|
),
|
|
"upsellProduct" => array(
|
|
array(
|
|
"sku" => "huynh-test-03",
|
|
"name" => "huynh-test-03",
|
|
"categories" => array(1422),
|
|
"description" => "huynh-test-03",
|
|
"shortDescription" => "short huynh-test-03",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 5,
|
|
"price" => 12.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 3,
|
|
"price" => 13.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
)
|
|
)
|
|
),
|
|
|
|
"crosssellProduct" => array(
|
|
array(
|
|
"sku" => "huynh-test-04",
|
|
"name" => "huynh-test-04",
|
|
"categories" => array(1422),
|
|
"description" => "huynh-test-03",
|
|
"shortDescription" => "short huynh-test-03",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 5,
|
|
"price" => 50.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 3,
|
|
"price" => 55.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
)
|
|
)
|
|
),
|
|
);
|
|
*/
|
|
public function createProductFromErp($groupDataProduct){
|
|
$linkDataAll = [];
|
|
//relatedProduct
|
|
foreach ($groupDataProduct["relatedProduct"] as $relatedProduct) {
|
|
if (!$this->isExistProduct($relatedProduct["sku"])) {
|
|
$this->createConfigurableProduct($relatedProduct);
|
|
}
|
|
$linkDataAll[] = $this->getProductLinks(
|
|
$groupDataProduct["sku"],
|
|
$relatedProduct["sku"],
|
|
"related"
|
|
);
|
|
}
|
|
|
|
//upsellProduct
|
|
foreach ($groupDataProduct["upsellProduct"] as $upsellProduct) {
|
|
if (!$this->isExistProduct($upsellProduct["sku"])) {
|
|
$this->createConfigurableProduct($upsellProduct);
|
|
}
|
|
$linkDataAll[] = $this->getProductLinks(
|
|
$groupDataProduct["sku"],
|
|
$upsellProduct["sku"],
|
|
"upsell"
|
|
);
|
|
}
|
|
|
|
//crosssellProduct
|
|
foreach ($groupDataProduct["crosssellProduct"] as $crosssellProduct) {
|
|
if (!$this->isExistProduct($crosssellProduct["sku"])) {
|
|
$this->createConfigurableProduct($crosssellProduct);
|
|
}
|
|
$linkDataAll[] = $this->getProductLinks(
|
|
$groupDataProduct["sku"],
|
|
$crosssellProduct["sku"],
|
|
"crosssell"
|
|
);
|
|
}
|
|
|
|
//crosssellProduct
|
|
|
|
$groupProduct = $this->createConfigurableProduct($groupDataProduct);
|
|
if ($groupProduct != null){
|
|
$groupProduct->setProductLinks($linkDataAll);
|
|
$groupProduct->save();
|
|
return $groupProduct;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/*
|
|
$groupDataProduct = array(
|
|
"sku" => "huynh-test-01",
|
|
"name" => "huynh-test-01",
|
|
"categories" => array(1422),
|
|
"description" => "description",
|
|
"shortDescription" => "short description",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 10,
|
|
"price" => 20.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 15,
|
|
"price" => 19.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
),
|
|
"relatedProduct" => array(
|
|
array(
|
|
"sku" => "huynh-test-02",
|
|
"name" => "huynh-test-02",
|
|
"categories" => array(1422),
|
|
"description" => "description",
|
|
"shortDescription" => "short description",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 5,
|
|
"price" => 15.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 3,
|
|
"price" => 13.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
)
|
|
)
|
|
),
|
|
"upsellProduct" => array(
|
|
array(
|
|
"sku" => "huynh-test-03",
|
|
"name" => "huynh-test-03",
|
|
"categories" => array(1422),
|
|
"description" => "huynh-test-03",
|
|
"shortDescription" => "short huynh-test-03",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 5,
|
|
"price" => 12.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 3,
|
|
"price" => 13.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
)
|
|
)
|
|
),
|
|
|
|
"crosssellProduct" => array(
|
|
array(
|
|
"sku" => "huynh-test-04",
|
|
"name" => "huynh-test-04",
|
|
"categories" => array(1422),
|
|
"description" => "huynh-test-03",
|
|
"shortDescription" => "short huynh-test-03",
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex9214-base3-ac-1_1.jpg",
|
|
],// images for group product
|
|
"brands" => "CISCO", // one of them ["CISCO","JUPINER","INTEL","HP"]
|
|
"types" => "ACCESSPOINT",//"ACCESSPOINT","ROUTER","SWITCH","SERVER"
|
|
"condition" => array(
|
|
"new" => array(
|
|
"qty" => 5,
|
|
"price" => 50.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/e/x/ex2200-48t-4g-1_1.jpg"
|
|
]//images for group product new
|
|
),
|
|
"refurbished" => array(
|
|
"qty" => 3,
|
|
"price" => 55.5,
|
|
"imageUrls" => [
|
|
"https://ipsupply.com.au/media/catalog/product/cache/10cb21bf02aa6d27ecf18013a400d823/m/a/mag4610-1_1.jpg"
|
|
]
|
|
)
|
|
)
|
|
)
|
|
),
|
|
);
|
|
*/
|
|
public function createProductFromPrology($groupDataProduct, $cats){
|
|
$linkDataAll = [];
|
|
//relatedProduct
|
|
foreach ($groupDataProduct["relatedProduct"] as $relatedProduct) {
|
|
if (!$this->isExistProduct($relatedProduct["sku"])) {
|
|
$relatedProduct["categories"] = $cats;
|
|
$relatedProduct["brands"] = "Cisco";
|
|
$this->createConfigurableProduct($relatedProduct);
|
|
}
|
|
$linkDataAll[] = $this->getProductLinks(
|
|
$groupDataProduct["sku"],
|
|
$relatedProduct["sku"],
|
|
"related"
|
|
);
|
|
}
|
|
|
|
//upsellProduct
|
|
foreach ($groupDataProduct["upsellProduct"] as $upsellProduct) {
|
|
if (!$this->isExistProduct($upsellProduct["sku"])) {
|
|
$upsellProduct["categories"] = $cats;
|
|
$upsellProduct["brands"] = "Cisco";
|
|
$this->createConfigurableProduct($upsellProduct);
|
|
}
|
|
$linkDataAll[] = $this->getProductLinks(
|
|
$groupDataProduct["sku"],
|
|
$upsellProduct["sku"],
|
|
"upsell"
|
|
);
|
|
}
|
|
|
|
//crosssellProduct
|
|
foreach ($groupDataProduct["crosssellProduct"] as $crosssellProduct) {
|
|
if (!$this->isExistProduct($crosssellProduct["sku"])) {
|
|
$crosssellProduct["categories"] = $cats;
|
|
$crosssellProduct["brands"] = "Cisco";
|
|
$this->createConfigurableProduct($crosssellProduct);
|
|
}
|
|
$linkDataAll[] = $this->getProductLinks(
|
|
$groupDataProduct["sku"],
|
|
$crosssellProduct["sku"],
|
|
"crosssell"
|
|
);
|
|
}
|
|
|
|
//crosssellProduct
|
|
$groupDataProduct["categories"] = $cats;
|
|
$groupDataProduct["brands"] = "Cisco";
|
|
$groupProduct = $this->createConfigurableProduct($groupDataProduct);
|
|
if ($groupProduct != null){
|
|
$groupProduct->setProductLinks($linkDataAll);
|
|
$groupProduct->save();
|
|
return $groupProduct;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getProductBySku($sku) {
|
|
$productObject = $this->objectManager->get('Magento\Catalog\Model\Product');
|
|
$product = $productObject->loadByAttribute("sku", $sku);
|
|
return $product;
|
|
}
|
|
|
|
//$data = {"sku": "huynh-test-01", "price" : 1, "condition" : "NEW" , "qty":5 }
|
|
// return Product|null
|
|
public function updatePriceAndQty($data) {
|
|
|
|
if (array_key_exists("sku", $data) && array_key_exists("price", $data)
|
|
&& array_key_exists("condition", $data) && array_key_exists("qty", $data)) {
|
|
$productSku = $data["sku"];
|
|
$productPrice = $data["price"];
|
|
$productCondition = $data["condition"];
|
|
$productQty = $data["qty"];
|
|
$productName = $productSku."-".strtoupper($productCondition);
|
|
if ($this->isExistProduct($productName)) {
|
|
$product = $this->objectManager->create('\Magento\Catalog\Model\Product');
|
|
$productData = $product->getCollection()
|
|
->addFieldToSelect("*")
|
|
->addAttributeToFilter('sku', $productName)
|
|
->getFirstItem();
|
|
// Update product's price
|
|
$productData->setPrice($productPrice);
|
|
|
|
// Update product's quantity
|
|
$is_in_stock = 1;
|
|
if ( empty($productQty) || $productQty <= 0 ) {
|
|
$is_in_stock = 0;
|
|
$productQty = 0;
|
|
}
|
|
$productData->setQuantityAndStockStatus(['qty' => $productQty, 'is_in_stock' => $is_in_stock]);
|
|
|
|
$productData->save();
|
|
|
|
$stockModel = $this->objectManager->get('Magento\CatalogInventory\Model\Stock\ItemFactory')->create();
|
|
$stockResource = $this->objectManager->get('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
|
|
$stockResource->load($stockModel, $productData->getId(),"product_id");
|
|
$stockModel->setQty($productQty);
|
|
$stockResource->save($stockModel);
|
|
|
|
$productObject = $this->objectManager->get('Magento\Catalog\Model\Product');
|
|
$product = $productObject->loadByAttribute("sku", $productSku);
|
|
$stockRegistryInterface = $this->objectManager->create('\Magento\CatalogInventory\Api\StockRegistryInterface');
|
|
$stockItem = $stockRegistryInterface->getStockItem($product->getId());
|
|
$stockItem->setData("is_in_stock", 1)->save();
|
|
$stockModel = $this->objectManager->get('Magento\CatalogInventory\Model\Stock\ItemFactory')->create();
|
|
$stockResource = $this->objectManager->get('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
|
|
$stockResource->load($stockModel, $product->getId(),"product_id");
|
|
$stockModel->setQty($productQty);
|
|
$stockResource->save($stockModel);
|
|
return $productData;
|
|
}
|
|
// Return when product' sku doesn't exist
|
|
return null;
|
|
}
|
|
// Return when parameter is wrong
|
|
return null;
|
|
}
|
|
//$type : related|upsell|crosssell
|
|
public function getProductLinks($sku, $skuLink, $type) {
|
|
$productLinks = $this->objectManager->create('Magento\Catalog\Api\Data\ProductLinkInterface');
|
|
$linkData = $productLinks //Magento\Catalog\Api\Data\ProductLinkInterface
|
|
->setSku($sku)
|
|
->setLinkedProductSku($skuLink)
|
|
->setLinkType($type);
|
|
return $linkData;
|
|
}
|
|
|
|
}
|