/** * Singleton control */ public static function get_instance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; }
function product_page_pricing_table_data() { $RP_WCDPD = RP_WCDPD::get_instance(); $this_option = $RP_WCDPD->get_options(); if ($this_option['settings']['display_table'] == 'hide' && (!isset($this_option['settings']['display_offers']) || $this_option['settings']['display_offers'] == 'hide')) { return; } global $product; if (!$product) { return; } // Load required classes require_once RP_WCDPD_PLUGIN_PATH . 'includes/classes/Pricing.php'; $selected_rule = null; // Iterate over pricing rules and use the first one that has this product in conditions (or does not have if condition "not in list") if (isset($this_option['pricing']['sets']) && count($this_option['pricing']['sets'])) { foreach ($this_option['pricing']['sets'] as $rule_key => $rule) { if ($rule['method'] == 'quantity' && ($validated_rule = RP_WCDPD_Pricing::validate_rule($rule))) { if ($validated_rule['selection_method'] == 'all' && $RP_WCDPD->user_matches_rule($validated_rule)) { $selected_rule = $validated_rule; break; } if ($validated_rule['selection_method'] == 'categories_include' && count(array_intersect($RP_WCDPD->get_product_categories($product->id), $validated_rule['categories'])) > 0 && $RP_WCDPD->user_matches_rule($validated_rule)) { $selected_rule = $validated_rule; break; } if ($validated_rule['selection_method'] == 'categories_exclude' && count(array_intersect($RP_WCDPD->get_product_categories($product->id), $validated_rule['categories'])) == 0 && $RP_WCDPD->user_matches_rule($validated_rule)) { $selected_rule = $validated_rule; break; } if ($validated_rule['selection_method'] == 'products_include' && in_array($product->id, $validated_rule['products']) && $RP_WCDPD->user_matches_rule($validated_rule)) { $selected_rule = $validated_rule; break; } if ($validated_rule['selection_method'] == 'products_exclude' && !in_array($product->id, $validated_rule['products']) && $RP_WCDPD->user_matches_rule($validated_rule)) { $selected_rule = $validated_rule; break; } } } } if (is_array($selected_rule)) { // Quantity if ($selected_rule['method'] == 'quantity' && in_array($this_option['settings']['display_table'], array('modal', 'inline')) && isset($selected_rule['pricing'])) { if ($product->product_type == 'variable') { $product_variations = $product->get_available_variations(); } // For variable products only - check if prices differ for different variations $multiprice_variable_product = false; if ($product->product_type == 'variable' && !empty($product_variations)) { $last_product_variation = array_slice($product_variations, -1); $last_product_variation_object = new WC_Product_Variable($last_product_variation[0]['variation_id']); $last_product_variation_price = $last_product_variation_object->get_price(); foreach ($product_variations as $variation) { $variation_object = new WC_Product_Variable($variation['variation_id']); if ($variation_object->get_price() != $last_product_variation_price) { $multiprice_variable_product = true; } } } if ($multiprice_variable_product) { $variation_table_data = array(); foreach ($product_variations as $variation) { $variation_product = new WC_Product_Variation($variation['variation_id']); $variation_table_data[$variation['variation_id']] = $RP_WCDPD->pricing_table_calculate_adjusted_prices($selected_rule['pricing'], $variation_product->get_price()); } } else { if ($product->product_type == 'variable' && !empty($product_variations)) { $variation_product = new WC_Product_Variation($last_product_variation[0]['variation_id']); $table_data = $RP_WCDPD->pricing_table_calculate_adjusted_prices($selected_rule['pricing'], $variation_product->get_price()); } else { $table_data = $RP_WCDPD->pricing_table_calculate_adjusted_prices($selected_rule['pricing'], $product->get_price()); } } } return $table_data; } return false; }
/** * Check if conditions of a rule are matched * * @access public * @param array $rule * @return bool */ public function rule_conditions_match($rule) { // Customers if ($rule['user_method'] == 'roles_include') { if (count(array_intersect(RP_WCDPD::current_user_roles(), $rule['roles'])) < 1) { return false; } } if ($rule['user_method'] == 'roles_exclude') { if (count(array_intersect(RP_WCDPD::current_user_roles(), $rule['roles'])) > 0) { return false; } } if ($rule['user_method'] == 'capabilities_include') { if (count(array_intersect(RP_WCDPD::current_user_capabilities(), $rule['capabilities'])) < 1) { return false; } } if ($rule['user_method'] == 'capabilities_exclude') { if (count(array_intersect(RP_WCDPD::current_user_capabilities(), $rule['capabilities'])) > 0) { return false; } } if ($rule['user_method'] == 'users_include') { if (!in_array(get_current_user_id(), $rule['users'])) { return false; } } if ($rule['user_method'] == 'users_exclude') { if (in_array(get_current_user_id(), $rule['users'])) { return false; } } // Get counts of items/products/categories depending on "Quantities based on" setting $quantities = $this->count_in_cart($rule); // Check if quantity discount quantities are matched if ($rule['method'] == 'quantity') { $is_ok = false; foreach ($quantities as $quantity_key => $quantity) { foreach ($rule['pricing'] as $row_key => $row) { if (!isset($row['added']) && $quantity >= $row['min'] && $quantity <= $row['max']) { $is_ok = true; break; } } } if (!$is_ok) { return false; } } else { $is_ok = false; foreach ($quantities as $quantity_key => $quantity) { if ($quantity >= $rule['special_purchase']) { $is_ok = true; break; } } if (!$is_ok) { return false; } } return true; }
/** * Validate, sanitize, check conditions and return single discount rule to be applied * * @access public * @param float $subtotal * @param array $discount_rule * @return mixed */ public function validate_rule($subtotal, $discount_rule) { // Valid from if (isset($discount_rule['valid_from']) && !empty($discount_rule['valid_from']) && strtotime($discount_rule['valid_from'] . ' 00:00:00') > time()) { return false; } // Valid until if (isset($discount_rule['valid_until']) && !empty($discount_rule['valid_until']) && strtotime($discount_rule['valid_until'] . ' 23:59:59') < time()) { return false; } // Only if pricing not adjusted if (isset($discount_rule['only_if_pricing_not_adjusted']) && $discount_rule['only_if_pricing_not_adjusted'] && !empty($this->pricing->applied)) { return false; } // CONDITIONS foreach ($discount_rule['conditions'] as $condition_key => $condition) { switch ($condition['key']) { /** * Total at least */ case 'subtotal_bottom': if ($subtotal < $condition['value']) { return false; } break; /** * Total less than */ /** * Total less than */ case 'subtotal_top': if ($subtotal >= $condition['value']) { return false; } break; /** * At least one product in cart */ /** * At least one product in cart */ case 'products': if (!$this->product_in_cart($condition['products'])) { return false; } break; /** * None of selected products in cart */ /** * None of selected products in cart */ case 'products_not': if ($this->product_in_cart($condition['products'])) { return false; } break; /** * At least one category in cart */ /** * At least one category in cart */ case 'categories': if (!$this->category_in_cart($condition['categories'])) { return false; } break; /** * None of selected categories in cart */ /** * None of selected categories in cart */ case 'categories_not': if ($this->category_in_cart($condition['categories'])) { return false; } break; /** * Count of cart items at least */ /** * Count of cart items at least */ case 'item_count_bottom': if (count($this->items) < $condition['value']) { return false; } break; /** * Count of cart items less than */ /** * Count of cart items less than */ case 'item_count_top': if (count($this->items) >= $condition['value']) { return false; } break; /** * Sum of item quantities at least */ /** * Sum of item quantities at least */ case 'quantity_bottom': if ($this->total_cart_quantity() < $condition['value']) { return false; } break; /** * Sum of item quantities less than */ /** * Sum of item quantities less than */ case 'quantity_top': if ($this->total_cart_quantity() >= $condition['value']) { return false; } break; /** * Specific users */ /** * Specific users */ case 'users': if (get_current_user_id() == 0 || !in_array(get_current_user_id(), $condition['users'])) { return false; } break; /** * Specific user roles */ /** * Specific user roles */ case 'roles': if (get_current_user_id() == 0 || count(array_intersect(RP_WCDPD::current_user_roles(), $condition['roles'])) == 0) { return false; } break; /** * Specific user capabilities */ /** * Specific user capabilities */ case 'capabilities': if (get_current_user_id() == 0 || count(array_intersect(RP_WCDPD::current_user_capabilities(), $condition['capabilities'])) == 0) { return false; } break; /** * Count of orders to date */ /** * Count of orders to date */ case 'history_count': if (get_current_user_id() == 0 || $this->customer_order_count(get_current_user_id()) < $condition['value']) { return false; } break; /** * Total amount of orders to date */ /** * Total amount of orders to date */ case 'history_amount': if (get_current_user_id() == 0 || $this->customer_value(get_current_user_id()) < $condition['value']) { return false; } break; /** * Shipping country */ /** * Shipping country */ case 'shipping_countries': if (get_current_user_id() == 0) { return false; } else { $user_meta = get_user_meta(get_current_user_id()); if (!$user_meta || !isset($user_meta['shipping_country']) || empty($user_meta['shipping_country']) || !in_array($user_meta['shipping_country'][0], $condition['shipping_countries'])) { return false; } } break; default: break; } } return true; }