Last active
August 29, 2015 14:17
-
-
Save AD7six/24ddc287dbf95214f242 to your computer and use it in GitHub Desktop.
Nest less and use more functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function syncProducts() { | |
$db = JFactory::getDbo(); | |
JLog::addLogger( | |
array( | |
'text_file' => 'com_example_product.syncerrors.' . date('d-m-Y') . '.php' | |
), JLog::ALL, array('com_example_product') | |
); | |
// get products that need to be added | |
// UPDATED 1 feb 2015. No more Join, no more upper price limit. We are going all in @ IceCat | |
$query = $db->getQuery(true); | |
$query | |
->select('a.eancode, MIN(a.minimum_price) AS minimum_price, COUNT(shop_id) AS shop_amount') | |
->from($db->quoteName('#__example_product_affiliate_data') . " AS a") | |
->where("LENGTH(a.eancode) >= 13 AND LENGTH(a.eancode) <= 15 AND a.eancode !='0000000000000'") | |
//->where("a.state = 1 AND LENGTH(a.eancode) >= 13 AND LENGTH(a.eancode) <= 15 AND a.eancode !='0000000000000'") | |
//->where("a.eancode IN (5906720027783,8806086111171,8806086111126,8806086124164,8806086073134,8806086111157,8806086096911,8806086070232,8806086021104,8806862218382,8806085407800,8806085490031,8806085407916,8806085407992,8806085408036,8806085408371,8806085497276,8806085408395,8806085496477,8806085408418,8806085496422,8806085413122,8806071494647)") | |
// below is bugged, because there are affiliate shops that don't have categories that fit. | |
//->where("(a.category LIKE '%vaatwa%' OR a.subcategory LIKE '%koelkast%' OR a.category LIKE '%wasdrog%' OR a.subcategory LIKE '%witgoe%')") | |
->group("a.eancode") | |
->having("COUNT( shop_id ) > 2") | |
->order("a.eancode ASC"); | |
$db->setQuery($query); | |
$productsToSync = $db->loadObjectList(); | |
foreach ($productsToSync AS $product) { | |
if ($product->shop_amount < 2) { | |
// The having clause means this is unreachable. | |
continue; | |
} | |
$this->syncProduct($product); | |
} | |
} | |
/** | |
* One function, with one purpose | |
* | |
* Split out any chunks of logic to a separate function. | |
*/ | |
public function syncProduct($product) { | |
$productmeta = new stdClass(); | |
// Get extra info from icecat | |
$icecatArray = $this->getArrayByEancode($product->eancode); | |
// create functions to do specific tasks not public function doesEverything(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment