-- HikaShop version -- : 2.6.1
-- Joomla version -- : 3.5.0
-- PHP version -- : 5.3.24
I have a xml inventory file that gets downloaded from a server once a day. It is formatted like
<?xml version='1.0' standalone='yes'?>
<InventoryDocument 20160407 11:29:36 AM>
<Item>
<ItemNo>035388227</ItemNo>
<item_desc>BRN XBOLT WH ATACS AU 7MM 26B</item_desc>
<msp>854.44</msp>
<retail_price>1029.99</retail_price>
<base_price>804.99</base_price>
<sale_price></sale_price>
<sale_ends></sale_ends>
<quantity>0</quantity>
<upc_code>023614440741</upc_code>
<manufacturer>Browning</manufacturer>
<gun_type>Rifle: Bolt Action</gun_type>
<model_series>X-Bolt</model_series>
<caliber>7MM</caliber>
<action>Bolt Action</action>
<mag_cap>3+1</mag_cap>
<finish>Matte Blue</finish>
<stock>Composite| A-TACS AU Camo</stock>
<sights>No Sights</sights>
<barrle_length>26 Free Floating| Target Crown</barrle_length>
<overall_length>46.75</overall_length>
<features>Sling Swivel Studs| Adjustable Feather Trigger</features>
</Item>
...
</InventoryDocument 20160407 11:29:36 AM>
I'm trying to create a plugin that once the file is downloaded it uses simpleXML to load the file as an object, then loop through all items and create products for those that do not have a quantity of 0. I'm very new to working with the triggers in Hikashop. This is part of what I have so far.
function _updateProducts($path){
//Get a db connection
$db = JFactory::getDbo();
//Create a new query object
$query = $db->getQuery(true);
//select products from categories chosen in plugin
$query
->select($db->quoteName(array('a.category_id','a.category_name','b.category_id','b.product_id','c.product_id','c.product_code','c.product_quantity','c.product_published','c.upc'))
->from($db->quoteName('#__hikashop_category','a'))
->join('LEFT',$db->quoteName('#__hikashop_product_category','b').' ON ('.$db->quoteName('a.category_id').' = '.$db->quoteName('b.category_id').')')
->join('LEFT',$db->quoteName('#__hikashop_product','c').' ON ('.$db->quoteName('b.product_id').' = '.$db->quoteName('c.product_id').')')
->where($db->quoteName('a.category_id').' IN ('.implode(',',$plugin->params['category_name']).')';
//Reset the query using our newly populated object
$db->setQuery($query);
//Load the result as an object
$results = $db->loadObjectList();
//create array to store product code with upc
$product_sku_upc = array();
//store product codes and upcs into array
foreach($results as $result){
$product_sku_upc[$result->product_code]= $result->upc;
}
//convert XML file to object
$xml = simplexml_load_file(JPATH_ROOT.DS.$path);
//loop through items and create product if it doesn't already exist and there is quantity in stock
foreach($xml->Item as $product){
if(array_key_exists($product->ItemNo,$product_sku_upc) || $product->quantity == 0 || $product->quantity == "A*"){
continue;
}else{
//this is what I'm not sure about
onBeforeProductCreate($product,true);
}
}
}
As I said this is all new to me. My main questions are am I correct about calling the onBeforeProductCreate trigger in the code above, how would I use the onBeforeProductCreate trigger to assign the appropriate xml attributes to the appropriate product table column for each product, and does the trigger itself actually save the product?