Merge branch 'master' into commercial-lead-prices

This commit is contained in:
Almira Krdzic
2018-10-15 00:31:24 +02:00
13 changed files with 364 additions and 9 deletions

View File

@@ -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) {
@@ -101,21 +103,46 @@ class Wiaas_Package_Pricing {
return self::_get_package_pricing_commision($package);
}
public static function get_package_max_cost_margin($package){
return self::_get_package_max_cost_margin($package);
}
/**
* Persist payment prices configuration for package
* @param $package
* @param $pricing_rules
*/
public static function set_package_prices($package, $pricing_rules, $commision) {
public static function set_package_prices($package, $pricing_rules, $commision, $max_cost_margin) {
if ( isset( $pricing_rules ) ) {
$package->update_meta_data( '_wiaas_pricing_rules', $pricing_rules );
$package->update_meta_data('_package_pricing_commision', $commision, true);
$package->update_meta_data('_package_max_cost_margin', $max_cost_margin, true);
} else {
$package->delete_meta_data( '_wiaas_pricing_rules' );
}
$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) {
@@ -164,6 +191,26 @@ class Wiaas_Package_Pricing {
return (float) $commision;
}
private static function _get_package_max_cost_margin($package) {
$max_cost_margin = $package->get_meta( '_package_max_cost_margin', true);
if (!isset($max_cost_margin) || $max_cost_margin === '') {
return 0;
}
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();