Magento 2: ho kenya lihlahisoa ho tsoa mehloling e kantle

Magento ke tharollo ea e-commerce, ke hore. e reretsoe ho rekisa lihlahisoa ho feta ho boloka thepa, thepa kapa tlaleho ea lichelete e tsamaeang le thekiso. Lisebelisoa tse ling (mohlala, litsamaiso tsa ERP) li loketse hamolemo bakeng sa lits'ebetso tse tsamaeang le tsona. Ka hona, hangata ts'ebetsong ea ho sebelisa Magento mosebetsi oa ho kopanya lebenkele le litsamaiso tsena tse ling (mohlala, 1C) o hlaha.

Ka kakaretso, kopanyo e ka fokotsoa hore e be phetiso ea data ka:

  • lethathamo la libuka (lihlahisoa, lihlopha);
  • lintlha tsa thepa (litekanyetso tsa lihlahisoa libakeng tsa polokelo le litheko);
  • bareki;
  • litaelo;

Magento e fana ka sehlopha se arohaneng sa lintho bakeng sa ho qhekella data ho database - bobolokelo. Ka lebaka la litlhaloso tsa Magento, ho kenyelletsa data ho database ka li-repositories ho bonolo ho e ngola, empa ho joalo, ha re re, butle. Khatisong ena, ke nahana ka mekhahlelo ea mantlha ea ho kenyelletsa sehlahisoa ho Magento 2 ka mokhoa oa "classic" - ho sebelisa litlelase tsa repo.

Bareki le litaelo hangata li phetoa ka lehlakoreng le leng - ho tloha Magento ho ea ho litsamaiso tsa ERP tsa kantle. Ka hona, ho bonolo le bona, ka lehlakoreng la Magento o hloka feela ho khetha data e nepahetseng, ebe "likulo tsa tsoa ka lehlakoreng la rōna".

Melao-motheo ea ho rekota data ho database

Hajoale, ho theha lintho tse bolokiloeng sebakeng sa polokelo ea litaba ka mokhoa o hlophisitsoeng ho Magento ho etsoa ka Factory:

function __construct (MagentoCmsModelBlockFactory $blockFactory) {
    $this->blockFactory = $blockFactory;
}

/** @var MagentoCmsModelBlock $block */
$block = $this->blockFactory->create();

'me ho ngolla database ho etsoa ka Repository:

function __construct (MagentoCmsApiBlockRepositoryInterface $blockRepo) {
    $this->blockRepo = $blockRepo;
}

$this->blockRepo->save($block);

Mokhoa oa "Factory" le "Repository" o ka sebelisoa bakeng sa mefuta eohle e meholo sebakeng sa Magento 2.

Boitsebiso ba Sehlahisoa sa Motheo

Ke shebile sebopeho sa data se lumellanang le mofuta oa Magento 2.3. Boitsebiso ba motheo ka sehlahisoa bo teng tafoleng catalog_product_entity (ngoliso ea lihlahisoa):

entity_id
attribute_set_id
type_id
sku
has_options
required_options
created_at
updated_at

Ke lekanyelitsoe ho mofuta o le mong oa sehlahisoa (type_id='simple'), sehlopha sa litšoaneleho tsa kamehla (attribute_set_id=4) le ho hlokomoloha litšobotsi has_options и required_options. Ho tloha ka litšobotsi entity_id, created_at и updated_at li hlahisoa ka boiketsetso, joale, ha e le hantle, ho eketsa sehlahisoa se secha, re hloka feela ho seta sku. Ke etsa sena:

/** @var MagentoCatalogApiDataProductInterfaceFactory $factProd */
/** @var MagentoCatalogApiProductRepositoryInterface $repoProd */
/** @var MagentoCatalogApiDataProductInterface $prod */
$prod = $factProd->create();
$prod->setAttributeSetId(4);
$prod->setTypeId('simple');
$prod->setSku($sku);
$repoProd->save($prod);

'me ke fumana mokhelo:

The "Product Name" attribute value is empty. Set the attribute and try again.

Ke kenya lebitso la sehlahisoa ho kopo mme ke fumana molaetsa oa hore tšobotsi ha e eo Price. Kamora ho eketsa theko, sehlahisoa se eketsoa ho database:

$prod = $factProd->create();
$prod->setAttributeSetId(4);
$prod->setTypeId('simple');
$prod->setSku($sku);
$prod->setName($name);
$prod->setPrice($price);
$repoProd->save($prod);

Lebitso la sehlahisoa le bolokoa tafoleng ea tšobotsi ea varchar (catalog_product_entity_varchar), theko - tafoleng catalog_product_entity_decimal. Pele o kenya sehlahiswa, ho kgothaletswa hore re bontshe ka ho hlaka hore re sebedisa sebaka sa tsamaiso ho kenya boitsebiso:

/** @var MagentoStoreModelStoreManagerInterface $manStore */
$manStore->setCurrentStore(0);

Litšobotsi Tse Eketsehileng

Ho sebetsana le litšobotsi tse ling tsa sehlahisoa u sebelisa Magento hoa thabisa. Mohlala oa data oa EAV bakeng sa mekhatlo ea mantlha (sheba tafole eav_entity_type) ke e 'ngoe ea likarolo tsa bohlokoa tsa sethala sena. Re kenyelletsa feela litšobotsi tse nepahetseng ho mofuta oa sehlahisoa:

$prodEntity->setData('description', $desc);
$prodEntity->setData('short_description', $desc_short);
// или
$prodEntity->setDescription($desc);
$prodEntity->setShortDescription($desc_short);

le ha u boloka mohlala ka ntho ea repo:

$repoProd->save($prod);

Litšobotsi tse ling li tla bolokoa litafoleng tse tsamaellanang tsa database.

Lintlha tsa thepa

Ka mantsoe a bonolo - palo ea sehlahisoa se fumanehang. Ho Magento 2.3, libopeho tsa polokelongtshedimosetso tse hlalosang sebopeho sa ho boloka data ya inventory ke e fapaneng haholo ho tloha se etsahetseng pele. Leha ho le joalo, ho eketsa bongata ba sehlahisoa se fumanehang ka mofuta oa sehlahisoa ha ho thata ho feta ho eketsa litšobotsi tse ling:

/** @var MagentoCatalogModelProduct $prodEntity */
/** @var MagentoCatalogApiProductRepositoryInterface $repoProd */
$inventory = [
    'is_in_stock' => true,
    'qty' => 1234
];
$prodEntity->setData('quantity_and_stock_status', $inventory);
$repoProd->save($prodEntity);

Litaba

E le molao, tšehetso ea mecha ea litaba bakeng sa sehlahisoa bakeng sa moreki lebenkeleng (e-commerce) e fapana le tšehetso ea mecha ea phatlalatso bakeng sa sehlahisoa se tšoanang bakeng sa mosebeletsi oa tsamaiso ea ka hare ea likarabello (ERP). Boemong ba pele, ho bohlokoa ho bonts'a sehlahisoa sefahleho le sefahleho; ea bobeli, ho lekane ho fana ka mohopolo o akaretsang oa sehlahisoa. Leha ho le joalo, ho fetisa bonyane setšoantšo sa mantlha sa sehlahisoa ho tloaelehile haholo. case ha o kenya data.

Ha o eketsa setšoantšo ka phanele ea admin, setšoantšo se bolokoa pele bukeng ea nakoana (./pub/media/tmp/catalog/product) mme feela ha ho boloka sehlahisoa ho isoa bukeng ea media (./pub/media/catalog/product). Hape, ha e kenyelletsoa ka phanele ea admin, setšoantšo se tšoailoe image, small_image, thumbnail, swatch_image.

/** @var MagentoCatalogApiProductRepositoryInterface $repoProd */
/** @var MagentoCatalogModelProductGalleryCreateHandler $hndlGalleryCreate */
/* $imagePath = '/path/to/file.png';  $imagePathRelative = '/f/i/file.png' */
$imagePathRelative = $this->imagePlaceToTmpMedia($imagePath);
/* reload product with gallery data */
$product = $repoProd->get($sku);
/* add image to product's gallery */
$gallery['images'][] = [
    'file' => $imagePathRelative,
    'media_type' => 'image'
    'label' => ''
];
$product->setData('media_gallery', $gallery);
/* set usage areas */
$product->setData('image', $imagePathRelative);
$product->setData('small_image', $imagePathRelative);
$product->setData('thumbnail', $imagePathRelative);
$product->setData('swatch_image', $imagePathRelative);
/* create product's gallery */
$hndlGalleryCreate->execute($product);

Ka lebaka le itseng, mecha ea litaba e hokahane feela ka mor'a ho qala ho boloka sehlahisoa le ho se fumana hape sebakeng sa polokelo. 'Me u lokela ho totobatsa tšobotsi label ha o eketsa keno ho la gallery ea media ea sehlahisoa (ho seng joalo re fumana mokhelo Undefined index: label in .../module-catalog/Model/Product/Gallery/CreateHandler.php on line 516).

Lihlopha

Hangata, sebopeho sa sehlopha sa lebenkele le ts'ebeliso ea morao-rao kapa ho beoa ha lihlahisoa ho tsona ho ka fapana haholo. Maano a ho fallisa data mabapi le mekhahlelo le lihlahisoa tse ka hare ho tsona li itšetlehile ka lintlha tse ngata. Mohlaleng ona ke khomarela tse latelang:

  • mekhahlelo ea backend le mabenkele a bapisoa ka mabitso;
  • haeba sehlopha se romelloa kantle ho naha se seng ka lebenkeleng, se thehiloe tlas'a sehlopha sa metso (Default Category) 'me boemo ba eona bo tsoelang pele lethathamong la mabenkele bo nkoa ka letsoho;
  • sehlahisoa se abeloa sehlopha feela ha se bōptjoa ka lebenkeleng (ho kenngoa ha pele);

Lintlha tsa motheo mabapi le sehlopha li teng tafoleng catalog_category_entity (lethathamo la lihlopha). Ho theha sehlopha ho Magento:

/** @var MagentoCatalogApiDataCategoryInterfaceFactory $factCat */
/** @var MagentoCatalogApiCategoryRepositoryInterface $repoCat */
$cat = $factCat->create();
$cat->setName($name);
$cat->setIsActive(true);
$repoCat->save($cat);

Ho hokahanya sehlahisoa le sehlopha ho etsoa ho sebelisoa mofuta oa ID le sehlahisoa sa SKU:

/** @var MagentoCatalogModelCategoryProductLinkFactory $factCatProdLink */
/** @var MagentoCatalogApiCategoryLinkRepositoryInterface $repoCatLink */
$link = $factCatProdLink->create();
$link->setCategoryId($catMageId);
$link->setSku($prodSku);
$repoCatLink->save($link);

Kakaretso

Khoutu ea ho ngola ho kenya sehlahisoa ka mokhoa o hlophisitsoeng ho Magento 2 ho bonolo haholo. Ke kopantse tsohle tse boletsoeng ka holimo ho module ea demo "flancer32/mage2_ext_demo_import". Ho na le taelo e le 'ngoe feela ea console mojuleng fl32:import:prod, e kenyang lihlahisoa tse hlalositsoeng faeleng ea JSON "./etc/data/products.json":

[
  {
    "sku": "...",
    "name": "...",
    "desc": "...",
    "desc_short": "...",
    "price": ...,
    "qty": ...,
    "categories": ["..."],
    "image_path": "..."
  }
]

Litšoantšo tse tsoang kantle ho naha li lethathamong la batho ./etc/data/img.

Nako ea ho kenya lihlahisoa tse 10 ho sebelisa mokhoa ona ke metsotsoana e ka bang 10 ho laptop ea ka. Haeba re ntšetsa pele mohopolo ona ho ea pele, ho bonolo ho fihlela qeto ea hore lihlahisoa tse ka bang 3600 li ka rekoa ka hora, 'me ho ka nka lihora tse ka bang 100 ho kenya lihlahisoa tsa 30K. Ho fetola laptop ka seva ho u fa monyetla oa ho lokisa boemo ka mokhoa o itseng. Mohlomong le ka makhetlo a 'maloa. Empa eseng ka litaelo tsa boholo. Mohlomong lebelo le ho lieha hona ke ka tekanyo e itseng e 'ngoe ea mabaka a ho hlaha ha morero magento/async-import.

Tharollo e matla ea ho eketsa lebelo la ho kenya thepa e ka ba ho kena ka ho toba sebakeng sa polokelo ea litaba, empa tabeng ena "lintho" tsohle tse mabapi le ho ata ha Magento li lahlehile - u tla tlameha ho etsa ntho e 'ngoe le e' ngoe "e tsoetseng pele". Leha ho le joalo, ho loketse. Haeba e sebetsa, ke tla nahana ka mokhoa ona ka ho ngola ka ho toba ho database sehloohong se latelang.

Source: www.habr.com

Eketsa ka tlhaloso