<?php
defined('_JEXEC') or die;
class plgHikashopHika_preorder_stock_auto extends JPlugin
{
function onAfterProductCreate(&$product) {
$productClass = hikashop_get('class.product');
$db = JFactory::getDbo();
$categories = self::getCategoriesIdWithChilds($this->params->get('cat', ''));
if(array_intersect($categories, $product->categories)){
if($product->product_quantity == 0){
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('product_quantity').' = '.$db->quote($this->params->get('qty', 0)),
$db->quoteName('product_preorder').' = '.$db->quote($this->params->get('qty', 0))
);
$conditions = array(
$db->quoteName('product_id').' = '.$db->quote($product->product_id)
);
$query->update($db->quoteName('#__hikashop_product'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
else{
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('product_preorder').' = '.$db->quote(0)
);
$conditions = array(
$db->quoteName('product_id').' = '.$db->quote($product->product_id)
);
$query->update($db->quoteName('#__hikashop_product'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
}
}
function onAfterProductUpdate(&$product){
$productClass = hikashop_get('class.product');
$db = JFactory::getDbo();
$categories = self::getCategoriesIdWithChilds($this->params->get('cat', ''));
if(array_intersect($categories, $product->categories)){
if($product->product_quantity == 0){
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('product_quantity').' = '.$db->quote($this->params->get('qty', 0)),
$db->quoteName('product_preorder').' = '.$db->quote($this->params->get('qty', 0))
);
$conditions = array(
$db->quoteName('product_id').' = '.$db->quote($product->product_id)
);
$query->update($db->quoteName('#__hikashop_product'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
else{
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('product_preorder').' = '.$db->quote(0)
);
$conditions = array(
$db->quoteName('product_id').' = '.$db->quote($product->product_id)
);
$query->update($db->quoteName('#__hikashop_product'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
}
}
function onAfterOrderUpdate(&$order,&$send_email) {
$db = JFactory::getDBO();
$config =& hikashop_config();
$confirmed = $config->get('order_confirmed_status');
$cancelled = $config->get('order_cancelled_status');
$orderClass = hikashop_get('class.order');
$productClass = hikashop_get('class.product');
$orderInDb = $orderClass->loadFullOrder($order->order_id);
if(!isset($order->order_status))
return true;
if(!empty($order->order_type) && $order->order_type != 'sale')
return true;
if($order->order_status != $confirmed) {
return true;
}
if(isset($order->old->order_status) && $order->old->order_status == $confirmed) {
return true;
}
if($order->order_status == $confirmed AND $order->old->order_status != $cancelled){
$db->setQuery('SELECT * FROM #__hikashop_order_product WHERE order_id = '.$order->order_id);
$products = $db->loadAssocList();
foreach($products as $product){
$product = json_decode(json_encode($product));
$categories = self::getCategoriesIdWithChilds($this->params->get('cat', ''));
if(array_intersect($categories, $product->categories)){
if($product->product_quantity == 0){
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('product_quantity').' = '.$db->quote($this->params->get('qty', 0)),
$db->quoteName('product_preorder').' = '.$db->quote($this->params->get('qty', 0))
);
$conditions = array(
$db->quoteName('product_id').' = '.$db->quote($product->product_id)
);
$query->update($db->quoteName('#__hikashop_product'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
else{
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('product_preorder').' = '.$db->quote(0)
);
$conditions = array(
$db->quoteName('product_id').' = '.$db->quote($product->product_id)
);
$query->update($db->quoteName('#__hikashop_product'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
}
}
}
}
function getCategoriesId($root){
$db = JFactory::getDBO();
$query = 'SELECT category_id FROM #__hikashop_category WHERE category_published = 1 AND category_parent_id = '.$root.' ORDER BY category_left ASC';
$db->setQuery($query);
$items = $db->loadColumn();
return $items;
}
function getCategoriesIdWithChilds($root = 1, $withRoot = true){
$list = self::getCategoriesId($root);
$catids = array();
if($withRoot == true){
$catids[] = $root;
}
if(!empty($list)){
foreach($list as $item){
if(!in_array($item, $catids)){
$catids[] = $item;
}
if(count(self::getCategoriesId($item))){
$catids = array_merge($catids, self::getCategoriesIdWithChilds($item));
}
}
}
return array_unique($catids);
}
}