Skip to content

Commit

Permalink
Magento 2.0 capability for main functions
Browse files Browse the repository at this point in the history
  • Loading branch information
neverovsky committed Mar 2, 2016
1 parent b988640 commit a99a3a0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 39 deletions.
6 changes: 4 additions & 2 deletions README.md
@@ -1,4 +1,4 @@
Magmi for Magento 2.0
Magmi 2 for Magento 2.0

=========

Expand All @@ -8,4 +8,6 @@ This is the fork Magento 2.0 of the Official GitHub home for magmi project (Mag
Magento CE 2.x Support
===================================

It is experimental and not fully tested yet
It is experimental and not fully tested yet

This version might not mean what all extensions for Magmi 0.7.x will be working with Magento 2
37 changes: 22 additions & 15 deletions magmi/engines/magmi_productimportengine.php
Expand Up @@ -140,7 +140,7 @@ public function getStoreIdsForWebsiteScope($scodes)
$this->_sid_wsscope[$scodes] = array();
$wscarr = csl2arr($scodes);
$qcolstr = $this->arr2values($wscarr);
$cs = $this->tablename("core_store");
$cs = $this->tablename("store");
$sql = "SELECT csdep.store_id FROM $cs as csmain
JOIN $cs as csdep ON csdep.website_id=csmain.website_id
WHERE csmain.code IN ($qcolstr) ";
Expand All @@ -163,7 +163,7 @@ public function getStoreIdsForStoreScope($scodes)
$this->_sid_sscope[$scodes] = array();
$scarr = csl2arr($scodes);
$qcolstr = $this->arr2values($scarr);
$cs = $this->tablename("core_store");
$cs = $this->tablename("store");
$sql = "SELECT csmain.store_id from $cs as csmain WHERE csmain.code IN ($qcolstr)";
$sidrows = $this->selectAll($sql, $scarr);
foreach ($sidrows as $sidrow) {
Expand Down Expand Up @@ -299,6 +299,7 @@ public function getStockCols()
*/
public function checkRequired($cols)
{

$eav_attr = $this->tablename("eav_attribute");
$sql = "SELECT attribute_code FROM $eav_attr WHERE is_required=1
AND frontend_input!='' AND frontend_label!='' AND entity_type_id=?";
Expand All @@ -318,7 +319,7 @@ public function checkAttributeInfo($attrinf)
$bt=$attrinf['backend_type'];
$user=$attrinf['is_user_defined'];
//checking specific extension custom model for selects that might not respect magento default model
if ($user==1 && $bt=='int' && $finp=='select' && isset($smodel) && $smodel!="eav/entity_attribute_source_table") {
if ($user==1 && $bt=='int' && $finp=='select' && isset($smodel) && $smodel!="Magento\Eav\Model\Entity\Attribute\Source\Table") {
$this->log("Potential assignment problem, specific model found for select attribute => ".$attrinf['attribute_code']."($smodel)", "warning");
}
}
Expand All @@ -332,12 +333,12 @@ public function initAttrSetInfos()
if (isset($this->attribute_set_infos)) {
return; // already initialized
}

$tname = $this->tablename("eav_entity_attribute");
$sql = "SELECT ea.attribute_set_id,ea.attribute_id
FROM $tname AS ea
WHERE ea.entity_type_id = ?";
$result = $this->selectAll($sql, $this->getProductEntityType());

foreach ($result as $row) {
$this->attribute_set_infos[$row["attribute_set_id"]][$row["attribute_id"]] = 1;
}
Expand All @@ -347,6 +348,7 @@ public function initAttrSetInfos()
"SELECT attribute_set_id,attribute_set_name FROM $tname WHERE entity_type_id=?",
array($this->getProductEntityType()));
$this->attribute_sets = array();

foreach ($result as $row) {
$this->attribute_sets[$row['attribute_set_name']] = $row['attribute_set_id'];
}
Expand Down Expand Up @@ -388,9 +390,12 @@ public function initAttrInfos($cols)
} else {
$sql = "SELECT `$tname`.* FROM `$tname` WHERE ($tname.attribute_code IN ($qcolstr)) AND (entity_type_id=?)";
}

$toscan[] = $this->getProductEntityType();
$result = $this->selectAll($sql, $toscan);



$attrinfs = array();
// create an attribute code based array for the wanted columns
foreach ($result as $r) {
Expand Down Expand Up @@ -944,7 +949,7 @@ public function createAttributes($pid, &$item, $attmap, $isnew, $itemids)
//if is global then , global scope applies but if configurable, back to store view scope since
//it's a select
$scope=$attrdesc["is_global"];
if ($attrcode != "price" && $attrdesc["is_configurable"]==1) {
if ($attrcode != "price" && $attrdesc["is_filterable"]==1) {
$scope=0;
}

Expand Down Expand Up @@ -991,15 +996,14 @@ public function createAttributes($pid, &$item, $attmap, $isnew, $itemids)
}
// if we have something to do with this value
if ($ovalue !== false && $ovalue != null) {
$data[] = $this->getProductEntityType();
// $data[] = $this->getProductEntityType();
$data[] = $attid;
$data[] = $store_id;
$data[] = $pid;
$data[] = $ovalue=='__NULL__'?null:$ovalue;
$insstr = "(?,?,?,?,?)";
$insstr = "(?,?,?,?)";
$inserts[] = $insstr;
}

// if one of the store in the list is admin
if ($store_id == 0) {
$sids = $store_ids;
Expand All @@ -1021,14 +1025,17 @@ public function createAttributes($pid, &$item, $attmap, $isnew, $itemids)
if (!empty($inserts)) {
// now perform insert for all values of the the current backend type in one
// single insert

$sql = "INSERT INTO $cpet
(`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`)
(`attribute_id`, `store_id`, `entity_id`, `value`)
VALUES ";
$sql .= implode(",", $inserts);
// this one taken from mysql log analysis of magento import
// smart one :)
$sql .= " ON DUPLICATE KEY UPDATE `value`=VALUES(`value`)";

$this->insert($sql, $data);

}

//if we have values to delete
Expand Down Expand Up @@ -1145,7 +1152,7 @@ public function updateStock($pid, $item, $isnew)
}
$sql = "INSERT INTO `$css` SELECT '$pid' as product_id,ws.website_id,cis.stock_id,'$qty' as qty,? as stock_status
FROM `$cpe` as cpe
JOIN " . $this->tablename("core_website") . " as ws ON ws.website_id IN (" . $this->arr2values($wsids) . ")
JOIN " . $this->tablename("store_website") . " as ws ON ws.website_id IN (" . $this->arr2values($wsids) . ")
JOIN " . $this->tablename("cataloginventory_stock") . " as cis ON cis.stock_id=?
WHERE cpe.entity_id=?
ON DUPLICATE KEY UPDATE stock_status=VALUES(`stock_status`),qty=VALUES(`qty`)";
Expand Down Expand Up @@ -1265,7 +1272,7 @@ public function getItemWebsites($item, $default = false)
if (!isset($this->_wsids[$item["websites"]])) {
$this->_wsids[$item["websites"]] = array();

$cws = $this->tablename("core_website");
$cws = $this->tablename("store_website");
$wscodes = csl2arr($item["websites"]);
$qcolstr = $this->arr2values($wscodes);
$rows = $this->selectAll("SELECT website_id FROM $cws WHERE code IN ($qcolstr)", $wscodes);
Expand All @@ -1283,7 +1290,7 @@ public function getItemWebsites($item, $default = false)

if (!isset($this->_wsids[$k])) {
$this->_wsids[$k] = array();
$cs = $this->tablename("core_store");
$cs = $this->tablename("store");
if (trim($k) != "admin") {
$scodes = csl2arr($k);
$qcolstr = $this->arr2values($scodes);
Expand Down Expand Up @@ -1311,7 +1318,7 @@ public function updateWebSites($pid, $item)
$wsids = $this->getItemWebsites($item);
$qcolstr = $this->arr2values($wsids);
$cpst = $this->tablename("catalog_product_website");
$cws = $this->tablename("core_website");
$cws = $this->tablename("store_website");
// associate product with all websites in a single multi insert (use ignore to avoid duplicates)
$ddata = array($pid);
$sql = "DELETE FROM `$cpst` WHERE product_id=?";
Expand Down Expand Up @@ -1427,7 +1434,7 @@ public function handleIgnore(&$item)
public function findItemStores($pid)
{
$sql = "SELECT cs.code FROM " . $this->tablename("catalog_product_website") . " AS cpw " .
"JOIN " . $this->tablename("core_store") . " as cs ON cs.website_id=cpw.website_id " .
"JOIN " . $this->tablename("store") . " as cs ON cs.website_id=cpw.website_id " .
"WHERE cpw.product_id=?";
$result = $this->selectAll($sql, array($pid));
$scodes = array();
Expand All @@ -1445,7 +1452,7 @@ public function checkItemStores($scodes)

$scarr = explode(",", $scodes);
trimarray($scarr);
$sql = "SELECT code FROM " . $this->tablename("core_store") . " WHERE code IN (" . $this->arr2values($scarr) . ")";
$sql = "SELECT code FROM " . $this->tablename("store") . " WHERE code IN (" . $this->arr2values($scarr) . ")";
$result = $this->selectAll($sql, $scarr);
$rscodes = array();
foreach ($result as $row) {
Expand Down
18 changes: 16 additions & 2 deletions magmi/integration/samples/sample.php
Expand Up @@ -38,12 +38,26 @@ public function log($data, $type)
* ON THE FLY INDEXER IS RECOMMENDED (better endimport performance)
* Reindexer needed also to have products show up on front : select all but "catalog_category_product" & "url_rewrite" (both are handled by on the fly indexer)
*/
$dp->beginImportSession("test_ptj", "create", new TestLogger());
$dp->beginImportSession("default", "create", new TestLogger());

$item=[
'name'=>'name22',
'sku'=>'nnn',
'qty'=>'55',
'is_in_stock'=>'3',
'type'=>'simple',
'categories'=>'Все/Cat1',
'small_image'=>'+http://hotline.ua/img/tx/746/7463963.jpg'
];
$dp->ingest($item);
$dp->endImportSession();
die();


/*
* Create 5000 items , with every 100 : upsell on last 100 even cross sell on last 100 odd related on last 100 every 5 cross sell on last 100 every 10 categories named catX/even or catX/odd with X is thousand of item (using categories plugin)
*/
for ($sku = 0; $sku < 5000; $sku++) {
for ($sku = 0; $sku < 2; $sku++) {
// create item category path array
// catX/even or catX/odd, X being the 1000's of the item
$cats = array("cat" . strval(intval($sku / 1000)));
Expand Down
2 changes: 1 addition & 1 deletion magmi/plugins/5b5/general/attributesetimport/functions.php
Expand Up @@ -89,7 +89,7 @@ function options($self, $title, $prefix, $entityName, $withCsvOptions, $withMagm
}
if ($withEnable) {
checkbox($self, $prefix, 'enable', true, "Enable ${entityName} import");
startDiv($self, $prefix, 'enabled', $self->getParam($prefix.":enable", "on")=="on");
startDiv($self, $prefix, 'enabled', $self->getParam($prefix.":enable", "off")=="on");
}
if ($withCsvOptions) {
csvOptions($self, $prefix);
Expand Down
28 changes: 14 additions & 14 deletions magmi/plugins/extra/itemprocessors/categories/categoryimport.php
Expand Up @@ -29,34 +29,35 @@ public function initCats()
{
// zioigor - 20110426 missing call to tablename method for table_prfix
$t = $this->tablename("catalog_category_entity");
$csg = $this->tablename("core_store_group");
$cs = $this->tablename("core_store");
$csg = $this->tablename("store_group");
$cs = $this->tablename("store");
$ccev = $t . "_varchar";
$ea = $this->tablename("eav_attribute");
$result = $this->selectAll(
"SELECT cs.store_id,csg.website_id,cce.entity_type_id,cce.path,ccev.value as name
"SELECT cs.store_id,csg.website_id,cce.path,ccev.value as name
FROM $cs as cs
JOIN $csg as csg on csg.group_id=cs.group_id
JOIN $t as cce ON cce.entity_id=csg.root_category_id
JOIN $ea as ea ON ea.attribute_code='name' AND ea.entity_type_id=cce.entity_type_id
JOIN $ea as ea ON ea.attribute_code='name'
JOIN $ccev as ccev ON ccev.attribute_id=ea.attribute_id AND ccev.entity_id=cce.entity_id
");
foreach ($result as $row) {
$rootinfo = array("path"=>$row["path"],"etid"=>$row["entity_type_id"],"name"=>$row["name"],
$rootinfo = array("path"=>$row["path"],"name"=>$row["name"],
"rootarr"=>explode("/", $row["path"]));
$this->_catroots[$row["store_id"]] = $rootinfo;
$this->_catrootw[$row["website_id"]][] = $row["store_id"];
if ($this->_cat_eid == null) {
$this->_cat_eid = $row["entity_type_id"];
// $this->_cat_eid = $row["entity_type_id"];
}
}
}

public function getCatAttributeInfos($attcode)
{
$t = $this->tablename("eav_attribute");
$sql = "SELECT * FROM $t WHERE entity_type_id=? AND attribute_code=?";
$info = $this->selectAll($sql, array($this->_cat_eid, $attcode));
$sql = "SELECT * FROM $t WHERE attribute_code=?";

$info = $this->selectAll($sql, array($attcode));
return $info[0];
}

Expand Down Expand Up @@ -111,17 +112,17 @@ public function getCategoryId($parentpath, $cattrs)
$path = implode("/", $parentpath);
$parentid = array_pop($parentpath);
// get child info using parent data
$sql = "SELECT cce.entity_type_id,cce.attribute_set_id,cce.level+1 as level,COALESCE(MAX(eac.position),0)+1 as position
$sql = "SELECT cce.attribute_set_id,cce.level+1 as level,COALESCE(MAX(eac.position),0)+1 as position
FROM $cet as cce
LEFT JOIN $cet as eac ON eac.parent_id=cce.entity_id
WHERE cce.entity_id=?
GROUP BY eac.parent_id";
$info = $this->selectAll($sql, array($parentid));
$info = $info[0];
// insert new category
$sql = "INSERT INTO $cet (entity_type_id,attribute_set_id,parent_id,position,level,path,children_count) VALUES (?,?,?,?,?,?,?)";
$sql = "INSERT INTO $cet (attribute_set_id,parent_id,position,level,path,children_count) VALUES (?,?,?,?,?,?)";
// insert empty path until we get category id
$data = array($info["entity_type_id"],$info["attribute_set_id"],$parentid,$info["position"],$info["level"],"",0);
$data = array($info["attribute_set_id"],$parentid,$info["position"],$info["level"],"",0);
// insert in db,get cat id
$catid = $this->insert($sql, $data);

Expand All @@ -139,16 +140,15 @@ public function getCategoryId($parentpath, $cattrs)

foreach ($attinfo as $attrcode => $attdata) {
if (isset($attdata["attribute_id"])) {
$inserts[] = "(?,?,?,?,?)";
$data[] = $info["entity_type_id"];
$inserts[] = "(?,?,?,?)";
$data[] = $attdata["attribute_id"];
$data[] = 0; // store id 0 for categories
$data[] = $catid;
$data[] = $cattrs[$attrcode];
}
}

$sql = "INSERT INTO $tb (entity_type_id,attribute_id,store_id,entity_id,value) VALUES " .
$sql = "INSERT INTO $tb (attribute_id,store_id,entity_id,value) VALUES " .
implode(",", $inserts) . " ON DUPLICATE KEY UPDATE value=VALUES(`value`)";
$this->insert($sql, $data);
unset($data);
Expand Down
Expand Up @@ -127,7 +127,7 @@ public function processColumnList(&$cols, $params = null)

public function initialize($params)
{
$sql = 'SELECT COUNT(store_id) as cnt FROM ' . $this->tablename('core_store') . ' WHERE store_id != 0';
$sql = 'SELECT COUNT(store_id) as cnt FROM ' . $this->tablename('store') . ' WHERE store_id != 0';
$ns = $this->selectOne($sql, array(), "cnt");
$this->_singleStore = $ns == 1;

Expand Down
8 changes: 4 additions & 4 deletions magmi/plugins/inc/magmi_defaultattributehandler.php
Expand Up @@ -268,26 +268,26 @@ public function handleIntAttribute($pid, &$item, $storeid, $attrcode, $attrdesc,
// we need to identify its type since some have no options
switch ($smodel) {
// if its status, default to 1 (Enabled) if not correcly mapped
case "catalog/product_status":
case 'Magento\Catalog\Model\Product\Attribute\Source\Status':
if (!$this->checkInt($ivalue)) {
$ovalue = 1;
}
break;
// do not create options for boolean values tagged as select ,default to 0 if not correcly mapped
case "eav/entity_attribute_source_boolean":
case 'Magento\Eav\Model\Entity\Attribute\Source\Boolean':
if (!$this->checkInt($ivalue)) {
$ovalue = 0;
}
break;
// if visibility no options either,default to 4 if not correctly mapped
case "catalog/product_visibility":
case 'Magento\Catalog\Model\Product\Visibility':
if (!$this->checkInt($ivalue)) {
$ovalue = 4;
}

break;
// if it's tax_class, get tax class id from item value
case "tax/class_source_product":
case 'Magento\Tax\Model\TaxClass\Source\Product':
$ovalue = $this->getTaxClassId($ivalue);
break;
// otherwise, standard option behavior
Expand Down

0 comments on commit a99a3a0

Please sign in to comment.