Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ class Magmi_ConfigurableItemProcessor extends Magmi_ItemProcessor
private $_use_defaultopc = false;
private $_optpriceinfo = array();
private $_currentsimples = array();
private $baseImageCache = array();
private $addsimpleimages;
private $backImageSupport;

public function initialize($params)
{}
{
$this->addsimpleimages = $this->getParam("CFGR:addsimpleimages", 0);
$this->backImageSupport = $this->getParam("CFGR:backimage", 0);

}
/* Plugin info declaration */
public function getPluginInfo()
{
Expand Down Expand Up @@ -61,27 +68,41 @@ public function dolink($pid, $cond, $conddata = array())
$cpsl = $this->tablename("catalog_product_super_link");
$cpr = $this->tablename("catalog_product_relation");
$cpe = $this->tablename("catalog_product_entity");

$sql = "DELETE cpsl.*,cpsr.* FROM $cpsl as cpsl
JOIN $cpr as cpsr ON cpsr.parent_id=cpsl.parent_id
WHERE cpsl.parent_id=?";
$this->delete($sql, array($pid));
// recreate associations
$sql = "INSERT INTO $cpsl (`parent_id`,`product_id`) SELECT cpec.entity_id as parent_id,cpes.entity_id as product_id
FROM $cpe as cpec
JOIN $cpe as cpes ON cpes.type_id IN ('simple','virtual') AND cpes.sku $cond
WHERE cpec.entity_id=?";
$this->insert($sql, array_merge($conddata, array($pid)));
$sql = "INSERT INTO $cpr (`parent_id`,`child_id`) SELECT cpec.entity_id as parent_id,cpes.entity_id as child_id

//cache select results
$sql = "SELECT cpec.entity_id as parent_id,cpes.entity_id as product_id
FROM $cpe as cpec
JOIN $cpe as cpes ON cpes.type_id IN ('simple','virtual') AND cpes.sku $cond
WHERE cpec.entity_id=?";
$this->insert($sql, array_merge($conddata, array($pid)));
$rows = $this->selectAll($sql, array_merge($conddata, array($pid)));

$ids = array();
//convert result array into a string of values
foreach($rows as $row){
$values .= "(".$row['parent_id'].",".$row['product_id']."),";
$ids[] = $row['product_id'];
}
$values = rtrim($values, ',');

if( ! empty($values) ){
// recreate associations
$sql = "INSERT INTO $cpsl (`parent_id`,`product_id`) VALUES $values";
$this->insert($sql);
$sql = "INSERT INTO $cpr (`parent_id`,`child_id`) VALUES $values";
$this->insert($sql);
}
unset($conddata);
return $ids;
}

public function autoLink($pid)
{
$this->dolink($pid, "LIKE CONCAT(cpec.sku,'%')");
return $this->dolink($pid, "LIKE CONCAT(cpec.sku,'%')");
}

public function updSimpleVisibility($pid)
Expand All @@ -101,7 +122,7 @@ public function updSimpleVisibility($pid)

public function fixedLink($pid, $skulist)
{
$this->dolink($pid, "IN (" . $this->arr2values($skulist) . ")", $skulist);
return $this->dolink($pid, "IN (" . $this->arr2values($skulist) . ")", $skulist);
}

public function buildSAPTable($sapdesc)
Expand Down Expand Up @@ -311,31 +332,38 @@ public function processItemAfterId(&$item, $params = null)
$idx++;
}
unset($confopts);
$ids = array();
switch ($matchmode)
{
case "none":
break;
case "auto":
// destroy old associations
$this->autoLink($pid);
$ids = $this->autoLink($pid);
$this->updSimpleVisibility($pid);
break;
case "cursimples":
$this->fixedLink($pid, $this->_currentsimples);
$ids = $this->fixedLink($pid, $this->_currentsimples);
$this->updSimpleVisibility($pid);

break;
case "fixed":
$sskus = explode(",", $item["simples_skus"]);
trimarray($sskus);
$this->fixedLink($pid, $sskus);
$ids = $this->fixedLink($pid, $sskus);
$this->updSimpleVisibility($pid);
unset($item["simples_skus"]);
unset($sskus);
break;
default:
break;
}

if($this->addsimpleimages >= 1){
// Calling rewriteImageAttributes() here ensures that it runs before handleVarcharAttribute().
$this->rewriteImageAttributes($item, $ids);
}

// always clear current simples
if (count($this->_currentsimples) > 0)
{
Expand All @@ -344,7 +372,123 @@ public function processItemAfterId(&$item, $params = null)
}
return true;
}

/**
Cang Luo 31/10/2014
Overwrite the image attributes with image paths from associated products only if it has associated products
This function must run before the handleVarcharAttribute() function of Image Attribute Processor
@param Array $item
: The item array.
@param Array $ids
: an array of IDs
*/
private function rewriteImageAttributes(&$item, $ids){
$state=0;
if(count($ids) > 0){
if($this->addsimpleimages>=2){
$firstBaseImage = $this->fetchBaseImage($ids[0]);
if( !empty($firstBaseImage) ){
$state += 1;
$item['image'] = $firstBaseImage;
$item['small_image'] = $item['image'];
$item['thumbnail'] = $item['image'];
}
}
$gallery = $this->fetchGalleryImages($item, $ids);
if( !empty($gallery) ){
$state += 2;
$item['media_gallery'] = $gallery;
}
}else{
$this->log("No associated products found for item ".$item['sku'].", fall back to original image values.", 'warning');
}
$item['IMAGES_OVERWRITTEN']=$state;
}

/**
Cang Luo 03/11/2014
select the base image path of given product ID from database
@param string $id
: an integer
@return string/false
: returns the path of base image of given product,
: returns false if $id is not in accepted format or no result found.
*/
public function fetchBaseImage($id){
$id = trim($id);
if(!preg_match('/^\d+$/', $id)){
$this->log("Invalid ID for base image: $id", 'warning');
return false;
}
if(!isset($this->baseImageCache[$id])){
$image_attinfo = $this->getAttrInfo('image');
$attribute_id = $image_attinfo['attribute_id'];
$t = $this->tablename('catalog_product_entity_varchar');
$sql = "SELECT value FROM $t WHERE attribute_id = $attribute_id AND entity_id = ?";
$path = $this->selectone($sql, $id, 'value');
if(is_null($path)){
return false;
}
//caching image path
$this->baseImageCache[$id] = $path;
}
return $this->baseImageCache[$id];
}

/**
Cang Luo 03/11/2014
Select the gallery image paths and labels of given product IDs from database
@param array $ids
: an array of integers
@return string/false
: returns a string of paths and labels, in the format of: path1[::label1]; path2[::label2];...
: returns false if $ids is not in accepted format.
*/
public function fetchGalleryImages($item, $ids){
$idString = $ids;
if(is_array($ids)){
$idString = implode(',' , $ids);
}
if(!preg_match('/^\d+(,\d+)*$/', $idString)){
$this->log("Invalid IDs for gallery images: $idString", 'warning');
return false;
}
$sids = $this->getItemStoreIds($item, 0);
$sids = '('.implode(",", $sids).')';
$tg = $this->tablename('catalog_product_entity_media_gallery');
$tgv = $this->tablename('catalog_product_entity_media_gallery_value');
$sql = "SELECT value, label
FROM $tgv AS emgv
JOIN $tg AS emg ON emg.value_id = emgv.value_id
WHERE emg.entity_id in ($idString) AND emgv.store_id IN $sids AND value IS NOT NULL";
$rows = $this->selectAll($sql);

//back image support
if($this->backImageSupport == 1){
$backImage = false;
if(count($ids) > 1){
//get the base image of the second linked simple product
$backImage = $this->fetchBaseImage($ids[1]);
}
}
$ovalue = '';
foreach($rows as $row){
//back image support
if($this->backImageSupport == 1){
if($row['label'] === 'back'){
$row['label'] = ''; //reset existing 'back' label
}
if($row['value'] === $backImage){
//set the label of base image of the second simple product to 'back'
$row['label'] = 'back';
}
}
$ovalue .= $row['value']. (empty($row['label']) ? '' : '::'.$row['label']) .';';
}
unset($rows);
return rtrim($ovalue, ';');
}

public function processColumnList(&$cols, $params = null)
{
if (!in_array("options_container", $cols))
Expand All @@ -357,7 +501,7 @@ public function processColumnList(&$cols, $params = null)

public function getPluginParamNames()
{
return array("CFGR:simplesbeforeconf","CFGR:updsimplevis","CFGR:nolink");
return array("CFGR:simplesbeforeconf","CFGR:updsimplevis","CFGR:nolink","CFGR:addsimpleimages","CFGR:backimage");
}

static public function getCategory()
Expand Down
23 changes: 23 additions & 0 deletions magmi/plugins/base/itemprocessors/configurables/options_panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@
</select>
</li>
</ul>

<ul class="formline">
<li class="label" style="width: 360px">Auto assign images to configurable product</li>
<?php $v=$this->getParam("CFGR:addsimpleimages",0)?>
<li class="value"><select name="CFGR:addsimpleimages">
<option value="0" <?php if ($v==0){?> selected="selected" <?php }?>>Disable</option>
<option value="1" <?php if ($v==1){?> selected="selected" <?php }?>>Gallery Only</option>
<option value="2" <?php if ($v==2){?> selected="selected" <?php }?>>All</option>
</select></li>
</ul>
<div class="fieldinfo" >If 'All' is selected, it will assign the base image of the first associated product to be the base image, thumbnail and small image of the configurable product, and add all gallery images of associated products to the gallery of configurable product.</div>

<ul class="formline">
<li class="label" style="width:300px; margin-left:60px">Back Image Label support</li>
<?php $v=$this->getParam("CFGR:backimage",0)?>
<li class="value"><select name="CFGR:backimage">
<option value="0" <?php if ($v==0){?> selected="selected" <?php }?>>No</option>
<option value="1" <?php if ($v==1){?> selected="selected" <?php }?>>Yes</option>
</select></li>
<div class="clear"></div>
</ul>
<div class="fieldinfo" style="margin-left:60px">Depends on the Auto Assign Images option. If yes, When auto adding images to the gallery of configurable, it will clear any existing 'back' label and set the label of the base image of the second associated product to 'back'. The labels of simple products will not be affected. </div>

Loading