Merge branch 'master' into commercial-lead-prices
This commit is contained in:
@@ -58,6 +58,7 @@ class Wiaas_Admin_Package_Pricing {
|
||||
$package = wc_get_product( $post->ID );
|
||||
$pricing_rules = Wiaas_Package_Pricing::get_package_prices($package);
|
||||
$commission = Wiaas_Package_Pricing::get_package_pricing_commission($package);
|
||||
$max_cost_margin = Wiaas_Package_Pricing::get_package_max_cost_margin($package);
|
||||
|
||||
include 'views/html-package-pricing.php';
|
||||
}
|
||||
@@ -71,7 +72,8 @@ class Wiaas_Admin_Package_Pricing {
|
||||
Wiaas_Package_Pricing::set_package_prices(
|
||||
wc_get_product( $post_id ),
|
||||
$_POST['wiaas_pricing_rules'],
|
||||
$_POST['wiaas_pricing_rules_commision']);
|
||||
$_POST['wiaas_pricing_rules_commision'],
|
||||
$_POST['wiaas_max_cost_margin']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -144,6 +144,20 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="options_group">
|
||||
<?php
|
||||
woocommerce_wp_text_input(
|
||||
array(
|
||||
'id' => '_wiaas_max_cost_margin',
|
||||
'name' => 'wiaas_max_cost_margin',
|
||||
'value' => $max_cost_margin,
|
||||
'label' => __( 'Max cost margin:', 'wiaas' ),
|
||||
'type' => 'number',
|
||||
)
|
||||
);
|
||||
?>
|
||||
</div>
|
||||
|
||||
<div class="options_group">
|
||||
<?php
|
||||
woocommerce_wp_text_input(
|
||||
|
||||
@@ -135,6 +135,12 @@ class Wiaas_Cart {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Check if package is available for adding to cart
|
||||
if (Wiaas_Package_Status::get_package_status($package_id) !== Wiaas_Package_Status::AVAILABLE){
|
||||
wc_add_notice('Package cannot be purchased at the moment', 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Retrieve package country
|
||||
$country = Wiaas_Countries::get_package_country($package);
|
||||
|
||||
|
||||
@@ -2,6 +2,23 @@
|
||||
|
||||
class Wiaas_Checkout {
|
||||
|
||||
public static function init(){
|
||||
add_action( 'woocommerce_check_cart_items', array(__CLASS__, 'validate_wiaas_packages'));
|
||||
}
|
||||
|
||||
public static function validate_wiaas_packages(){
|
||||
//check if any package became invalid
|
||||
$items = WC()->cart->get_cart();
|
||||
foreach($items as $item) {
|
||||
$item_id = $item['data']->get_id();
|
||||
if (wc_pb_is_bundle_container_cart_item($item) &&
|
||||
(Wiaas_Package_Status::get_package_status($item_id) !== Wiaas_Package_Status::AVAILABLE)){
|
||||
wc_add_notice( 'Package ' . $item['data']->get_title() . ' cannot be purchased at the moment', 'error' );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the order checkout.
|
||||
*
|
||||
@@ -87,3 +104,5 @@ class Wiaas_Checkout {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Checkout::init();
|
||||
|
||||
@@ -12,10 +12,38 @@ class Wiaas_Package {
|
||||
require_once dirname( __FILE__ ) . '/package/class-wiaas-package-option-groups.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/package/class-wiaas-package-type.php';
|
||||
require_once dirname( __FILE__ ) . '/package/class-wiaas-package-status.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/package/wiaas-package-functions.php';
|
||||
|
||||
add_filter('woocommerce_rest_prepare_product_object', array(__CLASS__, 'transform_rest_package'), 999, 3);
|
||||
|
||||
add_filter('woocommerce_rest_product_object_query', array( __CLASS__, 'edit_product_query'), 10, 2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change product query to fetch only available packages (available status)
|
||||
* @param $query
|
||||
* @param $instance
|
||||
*/
|
||||
public static function edit_product_query($query, $request){
|
||||
if (isset($request['id'])){
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($query['tax_query'])){
|
||||
$query['tax_query'] = array();
|
||||
}
|
||||
|
||||
$query['tax_query'][] =
|
||||
array(
|
||||
'taxonomy' => 'package_status',
|
||||
'field' => 'name',
|
||||
'terms' => Wiaas_Package_Status::AVAILABLE
|
||||
);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +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-supplier.php';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class Wiaas_Package_Status
|
||||
*/
|
||||
|
||||
|
||||
class Wiaas_Package_Status {
|
||||
|
||||
const AVAILABLE = 'available';
|
||||
const INVALID_MARGIN = 'margin_exceeded';
|
||||
const INVALID_TEMPLATE = 'invalid_template';
|
||||
|
||||
public static function init() {
|
||||
add_action('init', array( __CLASS__, 'register_package_status_taxonomy' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register wiaas package status taxonomy
|
||||
*/
|
||||
public static function register_package_status_taxonomy() {
|
||||
|
||||
$labels = array(
|
||||
'name' => _x( 'Package status', 'taxonomy general name', 'wiaas' ),
|
||||
'singular_name' => _x( 'Package status', 'taxonomy singular name', 'wiaas' ),
|
||||
'search_items' => __( 'Search Package statuses', 'wiaas' ),
|
||||
'all_items' => __( 'All Package statuses', 'wiaas' ),
|
||||
'parent_item' => __( 'Parent Package status', 'wiaas' ),
|
||||
'parent_item_colon' => __( 'Parent Package status:', 'wiaas' ),
|
||||
'edit_item' => __( 'Edit Package status', 'wiaas' ),
|
||||
'update_item' => __( 'Update Package status', 'wiaas' ),
|
||||
'add_new_item' => __( 'Add New Package status', 'wiaas' ),
|
||||
'new_item_name' => __( 'New Package status Name', 'wiaas' ),
|
||||
'menu_name' => __( 'Package status', 'wiaas' ),
|
||||
);
|
||||
|
||||
$args = array(
|
||||
'hierarchical' => false,
|
||||
'labels' => $labels,
|
||||
'show_ui' => false,
|
||||
'show_admin_column' => true,
|
||||
'query_var' => true,
|
||||
'rewrite' => array( 'slug' => 'package_status' ),
|
||||
);
|
||||
|
||||
register_taxonomy( 'package_status', array( 'product' ), $args );
|
||||
|
||||
|
||||
// Register available package statuses
|
||||
|
||||
$statuses = [self::AVAILABLE, self::INVALID_MARGIN, self::INVALID_TEMPLATE];
|
||||
|
||||
foreach ($statuses as $status) {
|
||||
wp_insert_term($status, 'package_status');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve available wiaas package statuses
|
||||
* @return array
|
||||
*/
|
||||
public static function get_available_package_statuses() {
|
||||
$statuses = get_terms( array(
|
||||
'taxonomy' => 'package_status',
|
||||
'hide_empty' => false,
|
||||
) );
|
||||
|
||||
return array_map(function($status) {
|
||||
return $status->name;
|
||||
}, $statuses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve package status for provided package id
|
||||
* @param $package_id
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public static function get_package_status($package_id) {
|
||||
$terms = wp_get_object_terms($package_id, 'package_status');
|
||||
$package_status = isset($terms[0]) ? $terms[0]->name : null;
|
||||
return $package_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set package status for provided package id
|
||||
* @param $package_id
|
||||
* @param $status
|
||||
*/
|
||||
public static function set_package_status($package_id, $status) {
|
||||
|
||||
wp_delete_object_term_relationships( $package_id, 'package_status' );
|
||||
|
||||
if (isset($status)) {
|
||||
wp_set_object_terms($package_id, $status, 'package_status', false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Status::init();
|
||||
@@ -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();
|
||||
@@ -48,8 +48,16 @@ class Wiaas_Product_Supplier {
|
||||
*/
|
||||
public static function on_organization_added($organization_id) {
|
||||
$organization = get_term_by('id', $organization_id, 'wiaas-user-organization');
|
||||
$supplier = wp_insert_term($organization->name, 'supplier');
|
||||
add_term_meta($supplier['term_id'], 'organisation_id', $organization->term_id);
|
||||
|
||||
$supplier = term_exists($organization->slug, 'supplier');
|
||||
|
||||
if (! $supplier) {
|
||||
$supplier = wp_insert_term($organization->name, 'supplier', array(
|
||||
'slug' => $organization->slug
|
||||
));
|
||||
}
|
||||
|
||||
add_term_meta($supplier['term_id'], 'organisation_id', $organization->term_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user