public function processItemAfterId(&$item, $params = null) { // if no tags column, do nothing if (!isset($item["tags"])) { return; } $hasrel = false; // tags separator is , $taglist = explode(",", $item["tags"]); foreach ($taglist as $tag) { if ($tag[0] == "+" || $tag[0] == "-") { $hasrel = true; break; } } // iterate on tags $addtags = array(); $remtags = array(); foreach ($taglist as $tag) { $tag = trim($tag); $dir = getRelative($tag); $taginfo = $this->parseTag($tag); if ($dir == "+") { $addtags[] = $taginfo; } else { $remtags[] = $taginfo; } } $sids = $this->getItemStoreIds($item); // we need a real store id , not admin so let's find one if (count($sids) == 1 && $sids[0] == 0) { $sql = "SELECT store_id FROM " . $this->tablename("core_store") . " WHERE website_id>0 AND is_active=1 ORDER BY website_id LIMIT 1 "; $sid = $this->selectone($sql, null, "store_id"); if ($sid !== null) { $sids = array($sid); } } $pid = $params["product_id"]; $this->handleItemTags($item, $pid, $sids, $addtags, $remtags, $hasrel); }
/** * assign categories for a given product id from values * categories should already be created & csv values should be as the ones * given in the magento export (ie: comma separated ids, minus 1,2) * @param int $pid : product id * @param array $item : attribute values for product indexed by attribute_code */ public function assignCategories($pid, $item) { $cce = $this->tablename("catalog_category_entity"); $ccpt = $this->tablename("catalog_category_product"); #handle assignment reset if (!isset($item["category_reset"]) || $item["category_reset"] == 1) { $sql = "DELETE {$ccpt}.*\n\t\t\tFROM {$ccpt}\n\t\t\tJOIN {$cce} ON {$cce}.entity_id={$ccpt}.category_id\n\t\t\tWHERE product_id=?"; $this->delete($sql, $pid); } $inserts = array(); $data = array(); $ddata = array(); $catids = csl2arr($item["category_ids"]); foreach ($catids as $catid) { $rel = getRelative($catid); if ($rel == "-") { $ddata[] = $catid; } else { $inserts[] = "(?,?)"; $data[] = $catid; $data[] = $pid; } } #peform deletion of removed category affectation if (count($ddata) > 0) { $sql = "DELETE FROM {$ccpt} WHERE category_id IN (" . $this->arr2values($ddata) . ") AND product_id=?"; $ddata[] = $pid; $this->delete($sql, $ddata); unset($ddata); } #create new category assignment for products, if multi store with repeated ids #ignore duplicates if (count($inserts) > 0) { $sql = "INSERT IGNORE INTO {$ccpt} (`category_id`,`product_id`)\n\t\t\t\t VALUES\t "; $sql .= implode(",", $inserts); $this->insert($sql, $data); unset($data); } unset($deletes); unset($inserts); }
/** * assign categories for a given product id from values * categories should already be created & csv values should be as the ones * given in the magento export (ie: comma separated ids, minus 1,2) * * @param int $pid * : product id * @param array $item * : attribute values for product indexed by attribute_code */ public function assignCategories($pid, $item) { $cce = $this->tablename("catalog_category_entity"); $ccpt = $this->tablename("catalog_category_product"); // andle assignment reset if (!isset($item["category_reset"]) || $item["category_reset"] == 1) { $sql = "DELETE {$ccpt}.*\n\t\t\tFROM {$ccpt}\n\t\t\tJOIN {$cce} ON {$cce}.entity_id={$ccpt}.category_id\n\t\t\tWHERE product_id=?"; $this->delete($sql, $pid); } $inserts = array(); $data = array(); $cdata = array(); $ddata = array(); $cpos = array(); $catids = csl2arr($item["category_ids"]); // find positive category assignments foreach ($catids as $catdef) { $a = explode("::", $catdef); $catid = $a[0]; $catpos = count($a) > 1 ? $a[1] : "0"; $rel = getRelative($catid); if ($rel == "-") { $ddata[] = $catid; } else { $cdata[$catid] = $catpos; } } // get all "real ids" if (count($cdata) > 0) { $scatids = array_keys($cdata); $rcatids = $this->selectAll("SELECT cce.entity_id as id FROM {$cce} as cce WHERE cce.entity_id IN (" . $this->arr2values($scatids) . ")", $scatids); $vcatids = array(); foreach ($rcatids as $rcatrow) { $vcatids[] = $rcatrow['id']; } // now get the diff $diff = array_diff(array_keys($cdata), $vcatids); $cdiff = count($diff); // if there are some, warning if ($cdiff > 0) { $this->log('Invalid category ids found for sku ' . $item['sku'] . ":" . implode(",", $diff), "warning"); // remove invalid category entries for ($i = 0; $i < $cdiff; $i++) { unset($cdata[$diff[$i]]); } } // ow we have verified ids foreach ($cdata as $catid => $catpos) { $inserts[] = "(?,?,?)"; $data[] = $catid; $data[] = $pid; $data[] = $catpos; } } // eform deletion of removed category affectation if (count($ddata) > 0) { $sql = "DELETE FROM {$ccpt} WHERE category_id IN (" . $this->arr2values($ddata) . ") AND product_id=?"; $ddata[] = $pid; $this->delete($sql, $ddata); unset($ddata); } // reate new category assignment for products, if multi store with repeated ids // gnore duplicates if (count($inserts) > 0) { $sql = "INSERT INTO {$ccpt} (`category_id`,`product_id`,`position`)\n\t\t\t\t VALUES\t "; $sql .= implode(",", $inserts); $sql .= " ON DUPLICATE KEY UPDATE position=VALUES(`position`)"; $this->insert($sql, $data); unset($data); } unset($deletes); unset($inserts); }
/** * update product stock * * @param int $pid * : product id * @param array $item * : attribute values for product indexed by attribute_code */ public function updateStock($pid, $item, $isnew) { $scols = $this->getStockCols(); // ake only stock columns that are in item $itstockcols = array_intersect(array_keys($item), $scols); // o stock columns set, item exists, no stock update needed. if (count($itstockcols) == 0 && !$isnew) { return; } $csit = $this->tablename("cataloginventory_stock_item"); $css = $this->tablename("cataloginventory_stock_status"); // alculate is_in_stock flag if (isset($item["qty"])) { if (!isset($item["manage_stock"])) { $item["manage_stock"] = 1; $item["use_config_manage_stock"] = 0; } $mqty = isset($item["min_qty"]) ? $item["min_qty"] : 0; $is_in_stock = isset($item["is_in_stock"]) ? $item["is_in_stock"] : ($item["qty"] > $mqty ? 1 : 0); $item["is_in_stock"] = $is_in_stock; } // ake only stock columns that are in item after item update $common = array_intersect(array_keys($item), $scols); // reate stock item line if needed $stock_id = isset($item["stock_id"]) ? $item["stock_id"] : 1; $sql = "INSERT IGNORE INTO `{$csit}` (product_id,stock_id) VALUES (?,?)"; $this->insert($sql, array($pid, $stock_id)); if (count($common) > 0) { $stockvals = $this->filterkvarr($item, $common); // ill with values $svstr = $this->arr2update($stockvals); if (isset($item["qty"]) && $item["qty"] != "") { $relqty = null; // if magmi_qty_absolute flag is not set, then use standard "relative" qty parsing. if (!isset($item["magmi_qty_absolute"]) || $item["magmi_qty_absolute"] == 0) { // test for relative qty if ($item["qty"][0] == "+" || $item["qty"][0] == "-") { $relqty = getRelative($item["qty"]); } } // if relative qty if ($relqty != null) { // update UPDATE statement value affectation $svstr = preg_replace("/(^|,)qty=\\?/", "\$1qty=qty{$relqty}?", $svstr); $stockvals["qty"] = $item["qty"]; $svstr = str_replace("is_in_stock=?", "is_in_stock=(qty>min_qty)", $svstr); unset($stockvals["is_in_stock"]); } } $sql = "UPDATE `{$csit}` SET {$svstr} WHERE product_id=? AND stock_id=?"; $this->update($sql, array_merge(array_values($stockvals), array($pid, $stock_id))); } $data = array(); $wsids = $this->getItemWebsites($item); $csscols = array("website_id", "product_id", "stock_id", "qty", "stock_status"); $cssvals = $this->filterkvarr($item, $csscols); $stock_id = isset($cssvals["stock_id"]) ? $cssvals["stock_id"] : 1; $stock_status = isset($cssvals["stock_status"]) ? $cssvals["stock_status"] : 1; // new auto synchro on lat inserted stock item values for stock status. // also works for multiple stock ids. // [start] exanto.de - this does not work inside a DB transaction bc cataloginventory_stock_item is not written yet on fresh imports /* * :ORG: $sql="INSERT INTO `$css` SELECT csit.product_id,ws.website_id,cis.stock_id,csit.qty,? as stock_status FROM `$csit` as csit JOIN ".$this->tablename("core_website")." as ws ON ws.website_id IN (".$this->arr2values($wsids).") JOIN ".$this->tablename("cataloginventory_stock")." as cis ON cis.stock_id=? WHERE product_id=? ON DUPLICATE KEY UPDATE stock_status=VALUES(`stock_status`),qty=VALUES(`qty`)"; */ // Fixed version $cpe = $this->tablename("catalog_product_entity"); // Fix , $stockvals is already a mix between item keys & stock table keys. $qty = isset($stockvals['qty']) ? $stockvals['qty'] : 0; if (!$qty) { $qty = 0; } $sql = "INSERT INTO `{$css}` SELECT '{$pid}' as product_id,ws.website_id,cis.stock_id,'{$qty}' as qty,? as stock_status\n FROM `{$cpe}` as cpe\n JOIN " . $this->tablename("core_website") . " as ws ON ws.website_id IN (" . $this->arr2values($wsids) . ")\n JOIN " . $this->tablename("cataloginventory_stock") . " as cis ON cis.stock_id=?\n WHERE cpe.entity_id=?\n ON DUPLICATE KEY UPDATE stock_status=VALUES(`stock_status`),qty=VALUES(`qty`)"; // [ end ] exanto.de - this does not work inside a DB transaction bc cataloginventory_stock_item is not written yet on fresh imports $data[] = $stock_status; $data = array_merge($data, $wsids); $data[] = $stock_id; $data[] = $pid; $this->insert($sql, $data); unset($data); }