Source of file ProductCSVBulkLoader.php
Size: 3,262 Bytes - Last Modified: 2021-12-24T05:16:32+00:00
/var/www/docs.ssmods.com/process/src/code/import/ProductCSVBulkLoader.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | <?php /** * Allow slightly more complex product imports from a CSV file * * @package commerce * @author i-lateral (http://www.i-lateral.com) */ class ProductCSVBulkLoader extends CsvBulkLoader { public $duplicateChecks = array( 'ID' => 'ID', 'SKU' => 'SKU' ); public function __construct($objectClass = null) { if(!$objectClass) $objectClass = 'Product'; parent::__construct($objectClass); } /** * Perform more complex imports of generic columns * */ public function processRecord($record, $columnMap, &$results, $preview = false) { // Get Current Object $objID = parent::processRecord($record, $columnMap, $results, $preview); $object = DataObject::get_by_id($this->objectClass, $objID); $this->extend("onBeforeProcess", $record, $object); // Loop through all fields and setup associations foreach($record as $key => $value) { // Find any categories (denoted by a 'CategoryXX' column) if(strpos($key,'Category') !== false) { $category = ProductCategory::get() ->filter("Title", $value) ->first(); if($category) $object->Categories()->add($category); } if($key == 'Categories') { $parts = explode(',', $value); if(!count($parts)) return false; // First remove all categories foreach($object->Categories() as $category) { $object->Categories()->remove($category); } // Now re-add categories foreach($parts as $part) { $category = ProductCategory::get() ->filter("Title", trim($part)) ->first(); if($category) $object->Categories()->add($category); } } // Find any Images (denoted by a 'ImageXX' column) if(strpos($key,'Image') !== false && $key != "Images") { $image = Image::get() ->filter("Name", $value) ->first(); if($image) $object->Images()->add($image); } // Alternativley look for the 'Images' field as a CSV if($key == "Images") { $parts = explode(',', $value); if(count($parts)) { // First remove all Images foreach($object->Images() as $image) { $object->Images()->remove($image); } // Now re-add categories foreach($parts as $part) { $image = Image::get() ->filter("Name", trim($part)) ->first(); if($image) $object->Images()->add($image); } } } } $this->extend("onAfterProcess", $record, $object); $object->destroy(); unset($object); return $objID; } } |