first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,207 @@
<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly
/**
* Class FlycartWooDiscountRulesAdvancedHelper
*/
if (!class_exists('FlycartWooDiscountRulesAdvancedHelper')) {
class FlycartWooDiscountRulesAdvancedHelper
{
/**
* Get all hierarchical taxonomy terms
*
* @access public
* @param string $taxonomy
* @param array $ids
* @param string $query
* @return array
*/
public static function get_all_hierarchical_taxonomy_terms($taxonomy, $ids = array(), $query = '')
{
$items = array();
// Get terms
$terms = get_terms(array($taxonomy), array('hide_empty' => 0));
if(!empty($terms)){
// Iterate over terms
foreach ($terms as $term_key => $term) {
if(isset($term->name)){
$term_count = count($terms);
// Get term name
$term_name = $term->name;
$term_slug = $term->slug;
// Term has parent
if ($term->parent) {
$parent_id = $term->parent;
$has_parent = true;
// Make sure we don't have an infinite loop here (happens with some kind of "ghost" terms)
$found = false;
$i = 0;
while ($has_parent && ($i < $term_count || $found)) {
// Reset each time
$found = false;
$i = 0;
// Iterate over terms again
foreach ($terms as $parent_term_key => $parent_term) {
$i++;
if ($parent_term->term_id == $parent_id) {
$term_name = $parent_term->name . ' → ' . $term_name;
$found = true;
if ($parent_term->parent) {
$parent_id = $parent_term->parent;
}
else {
$has_parent = false;
}
break;
}
}
}
}
// Get term id
$term_id = (string) $term->term_id;
// Skip this item if we don't need it
if (!empty($ids) && !in_array($term_id, $ids, true)) {
continue;
}
// Add item
$items[] = array(
'id' => $term_id,
'text' => $term_name,
'slug' => $term_slug
);
}
}
}
return $items;
}
/**
* Get all product attributes based on criteria
*
* @access public
* @param array $ids
* @param string $query
* @return array
*/
public static function get_all_product_attributes($ids = array(), $query = '')
{
global $wc_product_attributes;
$items = array();
// Iterate over product attributes
foreach ($wc_product_attributes as $attribute_key => $attribute) {
// Get attribute name
$attribute_name = !empty($attribute->attribute_label) ? $attribute->attribute_label : $attribute->attribute_name;
// Get terms for this attribute
$terms = self::get_all_hierarchical_taxonomy_terms($attribute_key, $ids, $query);
// Iterate over subitems and make a list of item/subitem pairs
foreach ($terms as $term) {
$items[] = array(
'id' => $term['id'],
'text' => $attribute_name . ': ' . $term['text'],
);
}
}
return $items;
}
/**
* Validate the cart item has the attributes selected
*
* @param array $variations
* @param array $selectedAttributes
* @return array
* */
public static function validateCartItemInSelectedAttributes($variations, $selectedAttributes, $all_attr = false){
$attributeMatches = 0;
if(!empty($variations)){
if($all_attr){
$available_attributes = array();
foreach ($variations as $key => $variation) {
$name = substr($key, 10);//Remove attribute_
$terms = self::get_all_hierarchical_taxonomy_terms($name);
if(!empty($terms)){
foreach ($terms as $term) {
if(strtolower($term['slug']) === strtolower($variation)){
$available_attributes[] = $term['id'];
}
}
}
}
if(!empty($available_attributes)){
foreach ($selectedAttributes as $attribute_list_item){
if(!in_array($attribute_list_item, $available_attributes)){
$attributeMatches = 0;
break;
}
$attributeMatches = 1;
}
}
} else {
foreach ($variations as $key => $variation) {
$name = substr($key, 10);//Remove attribute_
$terms = self::get_all_hierarchical_taxonomy_terms($name);
if(!empty($terms)){
foreach ($terms as $term) {
if(strtolower($term['slug']) === strtolower($variation)){
if(in_array($term['id'], $selectedAttributes)){
$attributeMatches = 1;
break;
}
}
}
if($attributeMatches){
break;
}
}
}
}
}
return $attributeMatches;
}
/**
* Subtotals are costs before discounts.
*
* To prevent rounding issues we need to work with the inclusive price where possible.
* otherwise we'll see errors such as when working with a 9.99 inc price, 20% VAT which would.
* be 8.325 leading to totals being 1p off.
*
* Pre tax coupons come off the price the customer thinks they are paying - tax is calculated.
* afterwards.
*
* e.g. $100 bike with $10 coupon = customer pays $90 and tax worked backwards from that.
*
* @since 3.2.0
*/
public static function get_calculated_item_subtotal() {
$cart_total = new FlycartWooDiscountRulesCartTotals();
$total = $cart_total->calculate_item_subtotals();
return $total;
}
}
}

View File

@@ -0,0 +1,216 @@
<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly
/**
* Class FlycartWooDiscountRulesCartTotals
*/
if (!class_exists('FlycartWooDiscountRulesCartTotals')) {
class FlycartWooDiscountRulesCartTotals
{
protected $items = array();
protected $calculate_tax = true;
/**
* Subtotals are costs before discounts.
*
* To prevent rounding issues we need to work with the inclusive price where possible.
* otherwise we'll see errors such as when working with a 9.99 inc price, 20% VAT which would.
* be 8.325 leading to totals being 1p off.
*
* Pre tax coupons come off the price the customer thinks they are paying - tax is calculated.
* afterwards.
*
* e.g. $100 bike with $10 coupon = customer pays $90 and tax worked backwards from that.
*
* @since 3.2.0
*/
public function calculate_item_subtotals() {
$this->cart = WC()->cart;
if(method_exists($this->cart, 'get_customer')){
$customer = $this->cart->get_customer();
} else {
$customer = WC()->customer;
}
$this->calculate_tax = wc_tax_enabled() && ! $customer->get_is_vat_exempt();
$this->get_items_from_cart();
foreach ( $this->items as $item_key => $item ) {
if ( $item->price_includes_tax ) {
if ( $customer->get_is_vat_exempt() ) {
$item = $this->remove_item_base_taxes( $item );
} elseif ( apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
$item = $this->adjust_non_base_location_price( $item );
}
}
$item->subtotal = $item->price;
$subtotal_taxes = array();
if ($this->calculate_tax && $item->product->is_taxable() ) {
$subtotal_taxes = WC_Tax::calc_tax( $item->subtotal, $item->tax_rates, $item->price_includes_tax );
$item->subtotal_tax = array_sum( array_map( array( $this, 'round_line_tax' ), $subtotal_taxes ) );
if ( $item->price_includes_tax ) {
// Use unrounded taxes so we can re-calculate from the orders screen accurately later.
$item->subtotal = $item->subtotal - array_sum( $subtotal_taxes );
}
}
}
$item_subtotal = array_sum( array_map( 'round', array_values( wp_list_pluck( $this->items, 'subtotal' ) ) ) );
$items_subtotal_tax = array_sum( array_values( wp_list_pluck( $this->items, 'subtotal_tax' ) ) );
$subtotal = $item_subtotal;
if(get_option('woocommerce_tax_display_cart', 'incl') == 'incl'){
$subtotal = $item_subtotal + $items_subtotal_tax;
}
if(function_exists('wc_remove_number_precision_deep')){
return wc_remove_number_precision_deep( $subtotal );
} else {
return $subtotal;
}
}
/**
* Ran to remove all base taxes from an item. Used when prices include tax, and the customer is tax exempt.
*
* @since 3.2.2
* @param object $item Item to adjust the prices of.
* @return object
*/
protected function remove_item_base_taxes( $item ) {
if ( $item->price_includes_tax && $item->taxable ) {
$base_tax_rates = WC_Tax::get_base_tax_rates( $item->product->get_tax_class( 'unfiltered' ) );
// Work out a new base price without the shop's base tax.
$taxes = WC_Tax::calc_tax( $item->price, $base_tax_rates, true );
// Now we have a new item price (excluding TAX).
$item->price = round( $item->price - array_sum( $taxes ) );
$item->price_includes_tax = false;
}
return $item;
}
/**
* Only ran if woocommerce_adjust_non_base_location_prices is true.
*
* If the customer is outside of the base location, this removes the base
* taxes. This is off by default unless the filter is used.
*
* Uses edit context so unfiltered tax class is returned.
*
* @since 3.2.0
* @param object $item Item to adjust the prices of.
* @return object
*/
protected function adjust_non_base_location_price( $item ) {
if ( $item->price_includes_tax && $item->taxable ) {
$base_tax_rates = WC_Tax::get_base_tax_rates( $item->product->get_tax_class( 'unfiltered' ) );
if ( $item->tax_rates !== $base_tax_rates ) {
// Work out a new base price without the shop's base tax.
$taxes = WC_Tax::calc_tax( $item->price, $base_tax_rates, true );
$new_taxes = WC_Tax::calc_tax( $item->price - array_sum( $taxes ), $item->tax_rates, false );
// Now we have a new item price.
$item->price = round( $item->price - array_sum( $taxes ) + array_sum( $new_taxes ) );
}
}
return $item;
}
/**
* Should we round at subtotal level only?
*
* @return bool
*/
protected function round_at_subtotal() {
return 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' );
}
/**
* Apply rounding to an array of taxes before summing. Rounds to store DP setting, ignoring precision.
*
* @since 3.2.6
* @param float $value Tax value.
* @return float
*/
protected function round_line_tax( $value ) {
if ( ! $this->round_at_subtotal() ) {
$value = wc_round_tax_total( $value, 0 );
}
return $value;
}
/**
* Handles a cart or order object passed in for calculation. Normalises data
* into the same format for use by this class.
*
* Each item is made up of the following props, in addition to those returned by get_default_item_props() for totals.
* - key: An identifier for the item (cart item key or line item ID).
* - cart_item: For carts, the cart item from the cart which may include custom data.
* - quantity: The qty for this line.
* - price: The line price in cents.
* - product: The product object this cart item is for.
*
* @since 3.2.0
*/
protected function get_items_from_cart() {
$this->items = array();
foreach ( WC()->cart->cart_contents as $cart_item_key => $cart_item ) {
if(function_exists('wc_add_number_precision_deep')){
$price = wc_add_number_precision_deep( $cart_item['data']->get_price() * $cart_item['quantity'] );
} else {
$price = $cart_item['data']->get_price() * $cart_item['quantity'];
}
$item = $this->get_default_item_props();
$item->key = $cart_item_key;
$item->object = $cart_item;
$item->tax_class = $cart_item['data']->get_tax_class();
$item->taxable = 'taxable' === $cart_item['data']->get_tax_status();
$item->price_includes_tax = wc_prices_include_tax();
$item->quantity = $cart_item['quantity'];
$item->price = $price;
$item->product = $cart_item['data'];
$item->tax_rates = $this->get_item_tax_rates( $item );
$this->items[ $cart_item_key ] = $item;
}
}
/**
* Get default blank set of props used per item.
*
* @since 3.2.0
* @return array
*/
protected function get_default_item_props() {
return (object) array(
'object' => null,
'tax_class' => '',
'taxable' => false,
'quantity' => 0,
'product' => false,
'price_includes_tax' => false,
'subtotal' => 0,
'subtotal_tax' => 0,
'total' => 0,
'total_tax' => 0,
'taxes' => array(),
);
}
/**
* Get tax rates for an item. Caches rates in class to avoid multiple look ups.
*
* @param object $item Item to get tax rates for.
* @return array of taxes
*/
protected function get_item_tax_rates( $item ) {
$tax_class = $item->product->get_tax_class();
if(method_exists($this->cart, 'get_customer')){
return isset( $this->item_tax_rates[ $tax_class ] ) ? $this->item_tax_rates[ $tax_class ] : $this->item_tax_rates[ $tax_class ] = WC_Tax::get_rates( $item->product->get_tax_class(), $this->cart->get_customer() );
} else {
return isset( $this->item_tax_rates[ $tax_class ] ) ? $this->item_tax_rates[ $tax_class ] : $this->item_tax_rates[ $tax_class ] = WC_Tax::get_rates( $item->product->get_tax_class(), WC()->customer );
}
}
}
}

View File

@@ -0,0 +1,155 @@
<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly
/*
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function woodiscountfree_shipping_method() {
if ( ! class_exists( 'WooDiscountFree_Shipping_Method' ) ) {
class WooDiscountFree_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*/
public function __construct() {
$this->id = 'woodiscountfree';
$this->method_title = __( 'WooDiscount Free Shipping', 'woo-discount-rules' );
$this->method_description = __( 'Custom Shipping Method for Woocommerce Discount Rules', 'woo-discount-rules' );
$this->init();
$this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'yes';
global $flycart_woo_discount_rules;
//$this->title = isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'Free Shipping', 'woo-discount-rules' );
$title = $flycart_woo_discount_rules->discountBase->getConfigData('free_shipping_text', 'Free Shipping');
if(empty($title)) $title = 'Free Shipping';
$this->title = __( $title, 'woo-discount-rules' );
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
add_filter( 'woocommerce_shipping_'.$this->id.'_is_available', array($this, 'woodiscount_hide_shipping_when_is_available'), 100 );
}
function woodiscount_hide_shipping_when_is_available(){
$discountBase = new FlycartWooDiscountBase();
$discountBase->handleCartDiscount(1);
if($discountBase->has_free_shipping){
return true;
} else {
return false;
}
}
/**
* Does this method have a settings page?
* @return bool
*/
public function has_settings() {
return false; //$this->instance_id ? $this->supports( 'instance-settings' ) : $this->supports( 'settings' );
}
/**
* Define settings field for this shipping
* @return void
*/
function init_form_fields() {
global $flycart_woo_discount_rules;
$title = $flycart_woo_discount_rules->discountBase->getConfigData('free_shipping_text', __( 'Free Shipping', 'woo-discount-rules' ));
if(empty($title)) $title = 'Free Shipping';
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable', 'woo-discount-rules' ),
'type' => 'checkbox',
'description' => __( 'Enable this shipping.', 'woo-discount-rules' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woo-discount-rules' ),
'type' => 'text',
'description' => __( 'Title to be display on site', 'woo-discount-rules' ),
'default' => __( $title, 'woo-discount-rules' ),
)
);
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package = array() ) {
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => 0,
'taxes' => false,
'package' => $package
);
$this->add_rate( $rate );
}
}
}
}
$enable_free_shipping = $flycart_woo_discount_rules->discountBase->getConfigData('enable_free_shipping', "none");
if($enable_free_shipping == "woodiscountfree"){
add_action( 'woocommerce_shipping_init', 'woodiscountfree_shipping_method' );
function add_woodiscountfree_shipping_method( $methods ) {
$methods[] = 'WooDiscountFree_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_woodiscountfree_shipping_method' );
} else if($enable_free_shipping == "free_shipping"){
function woodiscount_hide_free_shipping_when_is_available(){
$discountBase = new FlycartWooDiscountBase();
$discountBase->handleCartDiscount(1);
if($discountBase->has_free_shipping){
return true;
} else {
return false;
}
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'woodiscount_hide_free_shipping_when_is_available');
}
if($enable_free_shipping != "none"){
function reset_default_shipping_method_woo_discount( $method, $available_methods ) {
if(!empty($available_methods) && is_array($available_methods)) {
$shipping_methods = array_keys($available_methods);
if(!empty($shipping_methods)){
foreach ($shipping_methods as $key => $shipping_method) {
if (strpos($shipping_method, 'free_shipping') === 0) {
$method = $shipping_method;
}
}
if(in_array('woodiscountfree', $shipping_methods)) $method = 'woodiscountfree';
}
}
return $method;
}
add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method_woo_discount', 100, 2);
}
}

View File

@@ -0,0 +1,124 @@
<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly
/**
* Class FlycartWooDiscountRulesPriceProductDependent
*/
if (!class_exists('FlycartWooDiscountRulesPriceProductDependent')) {
class FlycartWooDiscountRulesPriceProductDependent
{
/**
* Get discount in each products
* */
public static function getDiscountInEachProducts($item, $rule, $checkRuleMatches, $productPage = 0, $for_product_id = 0){
$discounts = array();
$product_to_apply_count_option = isset($rule['product_based_condition']['product_to_apply_count_option'])? $rule['product_based_condition']['product_to_apply_count_option']: 'all';
$product_to_apply_count = isset($rule['product_based_condition']['product_to_apply_count'])? $rule['product_based_condition']['product_to_apply_count']: '';
$product_to_apply = isset($rule['product_based_condition']['product_to_apply'])? $rule['product_based_condition']['product_to_apply']: array();
$discount_type = isset($rule['product_based_discount']['discount_type'])? $rule['product_based_discount']['discount_type']: 'percentage_discount';
$discount_value = isset($rule['product_based_discount']['discount_value'])? $rule['product_based_discount']['discount_value']: 0;
$product_discount_details = array();
$product_discount_details['discount_type'] = $discount_type;
$product_discount_details['discount_value'] = $discount_value;
$product_discount_details['discount_quantity'] = 0;
$product_discount_details['discount_price'] = '';
if($discount_type == 'percentage_discount')
$originalDiscount = array('percentage_discount' => $discount_value);
else
$originalDiscount = array('price_discount' => $discount_value);
if($product_to_apply_count_option == 'all'){
foreach ($product_to_apply as $product_id){
$discounts[$product_id] = $originalDiscount;
}
} else if($product_to_apply_count_option == 'skip_first'){
$product_to_apply_count_to_apply = $product_to_apply_count;
$cart = FlycartWoocommerceCart::get_cart();
foreach ($cart as $cart_item_key => $values) {
$_product = $values['data'];
$quantity = $values['quantity'];
if($product_to_apply_count_to_apply > 0){
$product_id = FlycartWoocommerceProduct::get_id($_product);
if($productPage && $product_id == $for_product_id){
$quantity++;
}
if (in_array($product_id, $product_to_apply)){
if($product_to_apply_count_to_apply >= $quantity){
$discounts[$product_id] = array('percentage_discount' => 0);
$product_to_apply_count_to_apply -= $quantity;
} else if($product_to_apply_count_to_apply < $quantity){
$apply_for_only_quantity = $quantity - $product_to_apply_count_to_apply;
$price_discount = self::getDiscountForLimitedProductCount($_product, $discount_type, $discount_value, $quantity, $apply_for_only_quantity);
$price_discount_details = $product_discount_details;
$price_discount_details['discount_price'] = $price_discount['single_product_discount'];
$price_discount_details['discount_quantity'] = $apply_for_only_quantity;
$price_discount = $price_discount['discount'];
$discounts[$product_id] = array('price_discount' => $price_discount, 'product_discount_details' => $price_discount_details);
$product_to_apply_count_to_apply -= $quantity;
}
}
}
}
foreach ($product_to_apply as $product_id){
if(!isset($discounts[$product_id]))
$discounts[$product_id] = $originalDiscount;
}
} else {
$product_to_apply_count_to_apply = $product_to_apply_count;
$cart = FlycartWoocommerceCart::get_cart();
foreach ($cart as $cart_item_key => $values) {
$_product = $values['data'];
$quantity = $values['quantity'];
if($product_to_apply_count_to_apply > 0){
$product_id = FlycartWoocommerceProduct::get_id($_product);
if (in_array($product_id, $product_to_apply)){
if($product_to_apply_count_to_apply > 0){
if($product_to_apply_count_to_apply >= $quantity){
$discounts[$product_id] = $originalDiscount;
$product_to_apply_count_to_apply -= $quantity;
} elseif ($product_to_apply_count_to_apply < $quantity){
$apply_for_only_quantity = $product_to_apply_count_to_apply;
$price_discount = self::getDiscountForLimitedProductCount($_product, $discount_type, $discount_value, $quantity, $apply_for_only_quantity);
$price_discount_details = $product_discount_details;
$price_discount_details['discount_price'] = $price_discount['single_product_discount'];
$price_discount_details['discount_quantity'] = $apply_for_only_quantity;
$price_discount = $price_discount['discount'];
$discounts[$product_id] = array('price_discount' => $price_discount, 'product_discount_details' => $price_discount_details);
$product_to_apply_count_to_apply = 0;
}
}
}
}
}
foreach ($product_to_apply as $product_id){
if(!isset($discounts[$product_id])){
if($product_to_apply_count_to_apply > 0)
$discounts[$product_id] = $originalDiscount;
else
$discounts[$product_id] = array('percentage_discount' => 0);
}
}
}
return $discounts;
}
/**
* Get cheapest product
* */
public static function getDiscountForLimitedProductCount($_product, $discount_type, $discount_value, $quantity, $discount_quantity = 1){
$productPrice = FlycartWoocommerceProduct::get_price($_product);
if($discount_type == 'percentage_discount')
$discountPrice = $productPrice * ($discount_value / 100);
else
$discountPrice = $discount_value;
if($discount_quantity > $quantity)
$discount_price = $discountPrice - (($discountPrice/($quantity)) * ($quantity-$quantity));
else
$discount_price = $discountPrice - (($discountPrice/($quantity)) * ($quantity-$discount_quantity));
return array('discount' => $discount_price, 'single_product_discount' => $discountPrice);
}
}
}