Skip to content

Commit

Permalink
Merge pull request #35 from WPprodigy/refactored-1dot3
Browse files Browse the repository at this point in the history
Refactor for 1.3 release
  • Loading branch information
WPprodigy committed Dec 20, 2017
2 parents 5a32332 + 7375b38 commit f93d631
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 313 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# Editors
project.xml
project.properties
/nbproject/private/
.buildpath
.project
.settings*
.idea
.vscode
*.sublime-project
*.sublime-workspace
.sublimelinterrc

# OS X metadata
.DS_Store

# Windows junk
Thumbs.db

# Unit tests
/tmp
/tests/bin/tmp

# Logs
/logs

# Composer
/vendor/
6 changes: 4 additions & 2 deletions classes/admin/class-wcpf-admin-global-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* WooCommerce Product Fees
*
* Creates and saves the global settings.
* Creates global product settings, coupon options, and adds csv import support.
*
* @class WCPF_Admin_Global_Settings
* @author Caleb Burks
Expand Down Expand Up @@ -202,4 +202,6 @@ public function add_export_data_fee_multiplier( $value, $product ) {
return $product->get_meta( 'product-fee-multiplier', true, 'edit' );
}

} // End Class
}

return new WCPF_Admin_Global_Settings();
6 changes: 4 additions & 2 deletions classes/admin/class-wcpf-admin-product-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* WooCommerce Product Fees
*
* Creates and saves the product and variation settings.
* Create the product and variation settings.
*
* @class WCPF_Admin_Product_Settings
* @author Caleb Burks
Expand Down Expand Up @@ -148,4 +148,6 @@ public function admin_css() {
";
}

} // End Class
}

return new WCPF_Admin_Product_Settings();
231 changes: 172 additions & 59 deletions classes/class-woocommerce-product-fees.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,18 @@ class WooCommerce_Product_Fees {

/**
* Constructor for the main product fees class.
*
* @access public
*/
public function __construct() {
if ( is_admin() ) {
// Product Settings
// Product & global settings
require_once 'admin/class-wcpf-admin-product-settings.php';
new WCPF_Admin_Product_Settings();
// Global Settings
require_once 'admin/class-wcpf-admin-global-settings.php';
new WCPF_Admin_Global_Settings();
}

// Fee Classes
require_once( 'fees/class-wcpf-fee.php' );
require_once( 'fees/class-wcpf-product-fee.php' );
require_once( 'fees/class-wcpf-variation-fee.php' );

// Text Domain
// Text domain
add_action( 'plugins_loaded', array( $this, 'text_domain' ) );

// Hook in for fees to be added
// Hook-in for fees to be added
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_fees' ), 15 );
}

Expand All @@ -50,77 +40,200 @@ public function text_domain() {
}

/**
* Add all fees at checkout.
* Check if a product contains fee data.
*
* @access public
* @param int $id Product ID.
* @return bool True or false based on existance of custom meta.
*/
public function add_fees( $cart ) {
public function product_contains_fee_data( $id ) {
$fee_name = get_post_meta( $id, 'product-fee-name', true );
$fee_amount = get_post_meta( $id, 'product-fee-amount', true );

if ( '' !== $fee_name && '' !== $fee_amount && $fee_amount > 0 ) {
return true;
}

return false;
}

/**
* Convert a fee amount from percentage to the actual cost.
*
* @param string $fee_amount Fee amount.
* @param int $item_price Item price.
* @return int $fee_amount The actual cost of the fee.
*/
public function make_percentage_adjustments( $fee_amount, $item_price ) {
// Replace with a standard decimal separator for calculations.
$fee_amount = str_replace( wc_get_price_decimal_separator(), '.', $fee_amount );

if ( strpos( $fee_amount, '%' ) ) {
// Convert to decimal, then multiply by the cart item's price.
$fee_amount = ( str_replace( '%', '', $fee_amount ) / 100 ) * $item_price;
}

return $fee_amount;
}

/**
* Multiply the fee by the cart item quantity if needed.
*
* @param int $amount Fee amount.
* @param string $multiplier Whether the item should be multiplied by qty or not.
* @param int $qty Cart item quantity.
* @return int $amount The actual cost of the fee.
*/
public function maybe_multiply_by_quantity( $amount, $multiplier, $qty ) {
// Multiply the fee by the quantity if needed.
if ( 'yes' === $multiplier ) {
$amount = $qty * $amount;
}

return $amount;
}

/**
* Get the fee data from a product.
*
* @param array $item Cart item data.
* @return array $fee_data Fee data.
*/
public function get_fee_data( $item ) {
$fee_data = false;

// Assign the variation's parent ID if no fee at the variation level.
if ( 0 !== $item['variation_id'] ) {
if ( 0 !== $item['variation_id'] && ! $this->product_contains_fee_data( $item['id'] ) ) {
$item['id'] = $item['parent_id'];
}
}

if ( $this->product_contains_fee_data( $item['id'] ) ) {
$fee_data = array(
'name' => get_post_meta( $item['id'], 'product-fee-name', true ),
'amount' => get_post_meta( $item['id'], 'product-fee-amount', true ),
'multiplier' => get_post_meta( $item['id'], 'product-fee-multiplier', true )
);

$fee_data['amount'] = $this->make_percentage_adjustments( $fee_data['amount'], $item['price'] );
$fee_data['amount'] = $this->maybe_multiply_by_quantity( $fee_data['amount'], $fee_data['multiplier'], $item['qty'] );
}

return $fee_data;
}

/**
* Check if fees should be removed due to a coupon.
*
* @param object $cart WC Cart object.
* @return bool True or false based on existance of coupon meta.
*/
public function maybe_remove_fees_for_coupon( $cart ) {

if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
return false;
}

// Look for a fee-removing coupon.
$cart_coupons = $cart->get_coupons();
if ( ! empty( $cart_coupons ) && version_compare( WC_VERSION, '3.0', '>=' ) ) {
if ( ! empty( $cart_coupons ) ) {
foreach ( $cart_coupons as $coupon ) {
if ( 'yes' === $coupon->get_meta( 'wcpf_coupon_remove_fees' ) ) {
// Exit now. No need to look for fees.
return;
return true;
}
}
}

foreach( $cart->get_cart() as $cart_item => $values ) {
$_product = $values['data'];
$fee = false;
return false;
}

// Data we need from each product in the cart.
$product_data = array(
'id' => $values['product_id'],
'qty' => $values['quantity'],
'price' => $_product->get_price()
);
/**
* Get the fee's tax class.
*
* @param object $product WC Cart item object.
* @return string $fee_tax_class Which tax class to use for the fee.
*/
public function get_fee_tax_class( $product ) {
$fee_tax_class = get_option( 'wcpf_fee_tax_class', '_no_tax' );

// Check first for a variation specific fee, and use that if it exists.
if ( 0 !== $values['variation_id'] ) {
$product_data['variation_id'] = $values['variation_id'];
if ( ! wc_tax_enabled() ) {
return '_no_tax';
}

// Get variation fee.
$fee = new WCPF_Variation_Fee( $product_data, $cart );
// Change fee tax settings to the product's tax settings.
if ( 'inherit_product_tax' === $fee_tax_class ) {
if ( 'taxable' === $product->get_tax_status() ) {
$fee_tax_class = $product->get_tax_class();
} else {
$fee_tax_class = '_no_tax';
}
}

if ( ! $fee ) {
// Get product fee.
$fee = new WCPF_Product_Fee( $product_data, $cart );
}
return $fee_tax_class;
}

if ( $fee && $fee->return_fee() ) {
$fee_data = $fee->return_fee();
$fee_tax_class = get_option( 'wcpf_fee_tax_class', '_no_tax' );
/**
* Get all the fees.
*
* @param object $cart WC Cart object.
* @return array $fees An array of fees to be added.
*/
public function get_fees( $cart ) {
$fees = array();

// Change fee tax settings to the product's tax settings.
if ( 'inherit_product_tax' === $fee_tax_class ) {
$product_tax_status = $_product->get_tax_status();
$product_tax_class = $_product->get_tax_class();
if ( $this->maybe_remove_fees_for_coupon( $cart ) ) {
return $fees;
}

if ( 'taxable' === $product_tax_status ) {
$fee_tax_class = $product_tax_class;
} else {
$fee_tax_class = '_no_tax';
}
}
foreach( $cart->get_cart() as $cart_item => $item ) {

// Get the data we need from each product in the cart.
$item_data = array(
'id' => $item['data']->get_id(),
'variation_id' => $item['variation_id'],
'parent_id' => $item['data']->get_parent_id(),
'qty' => $item['quantity'],
'price' => $item['data']->get_price()
);

$fee = $this->get_fee_data( $item_data );

do_action( 'wcpf_before_fee_is_added', $fee_data, $_product );
if ( $fee ) {
$fee_id = strtolower( $fee['name'] );
$fee_tax_class = $this->get_fee_tax_class( $item['data'] );

// Check if taxes need to be added.
if ( wc_tax_enabled() && '_no_tax' !== $fee_tax_class ) {
// Add fee with taxes.
$cart->add_fee( $fee_data['name'], $fee_data['amount'], true, $fee_tax_class );
if ( array_key_exists( $fee_id, $fees ) && 'combine' === get_option( 'wcpf_name_conflicts', 'combine' ) ) {
$fees[$fee_id]['amount'] += $fee['amount'];
} else {
// Add fee without taxes.
$cart->add_fee( $fee_data['name'], $fee_data['amount'] );
$fees[$fee_id] = apply_filters( 'wcpf_filter_fee_data', array(
'name' => $fee['name'],
'amount' => $fee['amount'],
'taxable' => ( '_no_tax' === $fee_tax_class ) ? false : true,
'tax_class' => $fee_tax_class
), $item_data );
}

do_action( 'wcpf_after_fee_is_added', $fee_data, $_product );
}
}

return $fees;
}

/**
* Add the fees to the cart.
*
* @param object $cart WC Cart object.
* @return null
*/
public function add_fees( $cart ) {
$fees = $this->get_fees( $cart );

if ( empty( $fees ) ) {
return;
}

foreach ( $fees as $fee ) {
$cart->add_fee( $fee['name'], $fee['amount'], $fee['taxable'], $fee['tax_class'] );
}
}

}
46 changes: 0 additions & 46 deletions classes/fees/class-wcpf-fee.php

This file was deleted.

Loading

0 comments on commit f93d631

Please sign in to comment.