Use onBeforeProductCreate with xml inventory file

  • Posts: 128
  • Thank you received: 3
8 years 7 months ago #237369

-- 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?

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13373
  • MODERATOR
8 years 7 months ago #237373

Hi,

Triggers can't be called like that and that trigger won't save your product object and it also won't convert your XML tag names to the necessary names for HikaShop.

I guess that the simplest will be to base your code on the code of the plugin plugins/hikashop/datafeed/ which downloads a CSV file and import it with the import system of HikaShop.

Please Log in or Create an account to join the conversation.

  • Posts: 128
  • Thank you received: 3
8 years 6 months ago #239375

I think I can do as you said by basing my code off of the datafeed plugin because I can get my inventory file in a csv format as well. My one thing is about the "Price fee" parameter in the plugin. Is there anyway to use the datafeed to set multiple prices for each product based on the user group. Example if my "Price fee" is 15% can I assign this price to my "customer" user group and then add another parameter like "Dealer price fee" and say it is 10% and assign that price to my "dealer" user group for each product?

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13373
  • MODERATOR
8 years 6 months ago #239381

Hi,

In that case, instead of having different prices, why not just create discounts restricted to the different user groups so that the dsicount will be applied automatically to the price of the products ? That way, you keep one price and you can easily adjust your fee for special user groups.

Please Log in or Create an account to join the conversation.

Time to create page: 0.057 seconds
Powered by Kunena Forum