diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-product.php b/backend/app/plugins/wiaas/includes/class-wiaas-product.php index 0a5fd4d..7197bee 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-product.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-product.php @@ -4,8 +4,7 @@ class Wiaas_Product { public static function init() { require_once dirname( __FILE__ ) . '/product/class-wiaas-product-category.php'; - require_once dirname( __FILE__ ) . '/product/class-wiaas-product-supplier.php'; - require_once dirname( __FILE__ ) . '/product/class-wiaas-product-hooks.php'; + require_once dirname( __FILE__ ) . '/product/class-wiaas-product-supplier.php'; } } diff --git a/backend/app/plugins/wiaas/includes/pricing/class-wiaas-package-pricing.php b/backend/app/plugins/wiaas/includes/pricing/class-wiaas-package-pricing.php index 9270147..802cee8 100644 --- a/backend/app/plugins/wiaas/includes/pricing/class-wiaas-package-pricing.php +++ b/backend/app/plugins/wiaas/includes/pricing/class-wiaas-package-pricing.php @@ -51,6 +51,8 @@ class Wiaas_Package_Pricing { public static function init() { add_filter('woocommerce_bundle_price_html', array( __CLASS__, 'get_package_price_html' ), 10, 2); + + add_action('woocommerce_update_product', array(__CLASS__, 'on_product_update' ), 10, 1 ); } public static function get_package_price_html($price_html, $package) { @@ -121,6 +123,26 @@ class Wiaas_Package_Pricing { $package->save_meta_data(); } + /** + * Executes when woocommerce product is updated + */ + public static function on_product_update($product_id) { + $product = wc_get_product($product_id); + + if ($product->get_type() === WC_Product_Simple::get_type()){ + $product_price = Wiaas_Product_Pricing::get_product_price($product); + + $packages_containing_updated_product = wc_pb_get_bundled_product_map( $product ); + + foreach($packages_containing_updated_product as $index => $package_id){ + $package = new WC_Product_Bundle($package_id); + self::_validate_package($package); + } + }else if ($product->get_type() === WC_Product_Bundle::get_type()){ + self::_validate_package($product); + } + } + // PRIVATE private static function _get_package_prices($package) { @@ -169,6 +191,16 @@ class Wiaas_Package_Pricing { return (float) $max_cost_margin; } + + private static function _validate_package($package){ + $package_total_cost = Wiaas_Pricing::get_package_total_cost($package); + $package_max_cost_margin = Wiaas_Package_Pricing::get_package_max_cost_margin($package); + if (($package_max_cost_margin != 0) && ($package_total_cost > $package_max_cost_margin)){ + Wiaas_Package_Status::set_package_status($package->get_id(), Wiaas_Package_Status::INVALID_MARGIN); + }else{ + Wiaas_Package_Status::set_package_status($package->get_id(), Wiaas_Package_Status::AVAILABLE); + } + } } Wiaas_Package_Pricing::init(); \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/product/class-wiaas-product-hooks.php b/backend/app/plugins/wiaas/includes/product/class-wiaas-product-hooks.php deleted file mode 100644 index 1dd7771..0000000 --- a/backend/app/plugins/wiaas/includes/product/class-wiaas-product-hooks.php +++ /dev/null @@ -1,51 +0,0 @@ -get_type() === WC_Product_Simple::get_type()){ - $product_price = Wiaas_Product_Pricing::get_product_price($product); - - $packages_containing_updated_product = wc_pb_get_bundled_product_map( $product ); - - foreach($packages_containing_updated_product as $index => $package_id){ - $package = new WC_Product_Bundle($package_id); - self::validate_package($package); - } - }else if ($product->get_type() === WC_Product_Bundle::get_type()){ - self::validate_package($product); - } - } - - private static function validate_package($package){ - $package_total_cost = Wiaas_Pricing::get_package_total_cost($package); - $package_max_cost_margin = Wiaas_Package_Pricing::get_package_max_cost_margin($package); - if (($package_max_cost_margin != 0) && ($package_total_cost > $package_max_cost_margin)){ - Wiaas_Package_Status::set_package_status($package->get_id(), Wiaas_Package_Status::INVALID_MARGIN); - }else{ - //TODO: Check if template is valid - Wiaas_Package_Status::set_package_status($package->get_id(), Wiaas_Package_Status::AVAILABLE); - } - } -} - -Wiaas_Product_Hooks::init(); \ No newline at end of file