/magmi/plugins/base/itemprocessors/skufinder/001_skufinder.php
PHP | 95 lines | 83 code | 5 blank | 7 comment | 13 complexity | c8596a6784af2dcb1f4a3dbe644f46aa MD5 | raw file
Possible License(s): LGPL-3.0
- <?php
- class SkuFinderItemProcessor extends Magmi_ItemProcessor
- {
- private $_compchecked=FALSE;
-
- public function getPluginInfo()
- {
- return array(
- "name" => "SKU Finder",
- "author" => "Dweeves",
- "version" => "0.0.1",
- "url"=>"http://sourceforge.net/apps/mediawiki/magmi/index.php?title=SKU_Finder"
- );
- }
-
- public function processItemBeforeId(&$item,$params=null)
- {
-
- $matchfield=trim($this->getParam("SKUF:matchfield"));
- //protection from tricky testers ;)
- if($matchfield=="sku")
- {
- return true;
- }
- $attinfo=$this->getAttrInfo($matchfield);
- if($this->_compchecked==FALSE)
- {
- //Checking attribute compatibility with sku matching
- if($attinfo==NULL)
- {
- $this->log("$matchfield is not a valid attribute","error");
- $item["__MAGMI_LAST__"]=1;
- return false;
- }
- if($attinfo["is_unique"]==0 || $attinfo["is_global"]==0)
- {
- $this->log("sku matching attribute $matchfield must be unique & global scope");
- $item["__MAGMI_LAST__"]=1;
- return false;
- }
- if($attinfo["backend_type"]=="static")
- {
- $this->log("$matchfield is ".$attinfo["backend_type"].", it cannot be used as sku matching field.","error");
- $item["__MAGMI_LAST__"]=1;
- return false;
- }
- if($attinfo["frontend_input"]=="select" || $attinfo["frontend_input"]=="multiselect" )
- {
- $this->log("$matchfield is ".$attinfo["frontend_input"].", it cannot be used as sku matching field.","error");
- $item["__MAGMI_LAST__"]=1;
- return false;
- }
- $this->_compchecked=true;
- }
-
- //no item data for selected matching field, skipping
- if(!isset($item[$matchfield]) && trim($item["matchfield"])!=='')
- {
- $this->log("No value for $matchfield in datasource","error");
- return false;
- }
- //now find sku
- $sql="SELECT sku FROM ".$this->tablename("catalog_product_entity")." as cpe JOIN
- catalog_product_entity_".$attinfo["backend_type"]. "ON value=? AND attribute_id=?";
- $stmt=$this->select($sql,array($item[$matchfield],$attinfo["attribute_id"]));
- $n=0;
- while($result=$stmt->fetch())
- {
- //if more than one result, cannot match single sku
- if($n>1)
- {
- $this->log("Several skus match $matchfield value : ".$item["matchfield"],"error");
- return false;
- }
- else
- {
- $item["sku"]=$result["sku"];
- }
- $n++;
- }
- //if no item found, warning & skip
- if($n==0)
- {
- $this->log("No sku found matching $matchfield value : ".$item["matchfield"],"warning");
- return false;
- }
- //found a single sku ! item sku is in place, continue with processor chain
- return true;
- }
-
- static public function getCategory()
- {
- return "Input Data Preprocessing";
- }
- }