Skip to content

Commit

Permalink
Merge pull request #29 from claudiosanches/v3.0.0
Browse files Browse the repository at this point in the history
Stop use negative fees in favor of coupons
  • Loading branch information
claudiosanches authored Apr 10, 2017
2 parents 4858e0f + 1dee09a commit 1bde5fa
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 81 deletions.
37 changes: 20 additions & 17 deletions includes/admin/class-wc-payment-discounts-admin.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?php
/**
* Admin settings
*
* @package WC_Payment_Discounts/Classes
* @since 3.0.0
* @version 3.0.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit; // Exit if accessed directly.
}

/**
* WC_Payment_Discounts_Admin class
* WC_Payment_Discounts_Admin class.
*/
class WC_Payment_Discounts_Admin {

Expand Down Expand Up @@ -46,8 +54,7 @@ private function upgrade_to_230() {

foreach ( $old_options as $key => $value ) {
$new_options[ $key ] = array(
'amount' => $value,
'include_tax' => 'yes',
'amount' => $value,
);
}

Expand All @@ -57,8 +64,6 @@ private function upgrade_to_230() {

/**
* Register the administration menu for this plugin into the WordPress Dashboard menu.
*
* @return void
*/
public function add_plugin_admin_menu() {
add_submenu_page(
Expand All @@ -73,8 +78,6 @@ public function add_plugin_admin_menu() {

/**
* Register plugin settings.
*
* @return void
*/
public function register_settings() {
register_setting( 'woocommerce_payment_discounts_group', 'woocommerce_payment_discounts', array( $this, 'validate_settings' ) );
Expand All @@ -85,7 +88,7 @@ public function register_settings() {
*
* @param array $options Submitted values.
*
* @return array Fixed values.
* @return array Sanitized values.
*/
public function validate_settings( $options ) {
$output = array();
Expand All @@ -95,19 +98,19 @@ public function validate_settings( $options ) {
$output[ $key ]['amount'] = 0;
if ( isset( $value['amount'] ) ) {
if ( strstr( $value['amount'], '%' ) ) {
$amount = str_replace( '%', '', floatval( $value['amount'] ) ) . '%';
$amount = str_replace( '%', '', floatval( wc_format_decimal( $value['amount'] ) ) ) . '%';
} else {
$amount = floatval( $value['amount'] );
$amount = floatval( wc_format_decimal( $value['amount'] ) );
}

$output[ $key ]['amount'] = $amount;
}

// Validate include tax.
$output[ $key ]['include_tax'] = 'no';
if ( isset( $value['include_tax'] ) ) {
$output[ $key ]['include_tax'] = 'yes';
}
// Save coupon.
$data = array(
'amount' => $output[ $key ]['amount'],
);
WC_Payment_Discounts_Coupons::update_coupon( $key, $data );
}

return $output;
Expand All @@ -122,7 +125,7 @@ public function display_plugin_admin_page() {
$settings = get_option( 'woocommerce_payment_discounts' );
$payment_gateways = WC()->payment_gateways->payment_gateways();

include_once 'views/html-admin-settings.php';
include dirname( __FILE__ ) . '/views/html-admin-settings.php';
}
}

Expand Down
9 changes: 2 additions & 7 deletions includes/admin/views/html-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
<thead>
<tr>
<th style="width: 20%"><strong><?php esc_html_e( 'Payment method', 'woocommerce-payment-discounts' ); ?></strong></th>
<th style="width: 20%"><strong><?php esc_html_e( 'Discount amount', 'woocommerce-payment-discounts' ); ?></strong></th>
<th><strong><?php esc_html_e( 'Include tax in the discount amount', 'woocommerce-payment-discounts' ); ?></strong></th>
<th><strong><?php esc_html_e( 'Discount amount', 'woocommerce-payment-discounts' ); ?></strong></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $payment_gateways as $gateway ) :
$amount = isset( $settings[ $gateway->id ]['amount'] ) ? $settings[ $gateway->id ]['amount'] : '0';
$include_tax = isset( $settings[ $gateway->id ]['include_tax'] ) ? $settings[ $gateway->id ]['include_tax'] : 'yes';
$amount = isset( $settings[ $gateway->id ]['amount'] ) ? wc_format_localized_price( $settings[ $gateway->id ]['amount'] ) : '0';
?>
<tr>
<td>
Expand All @@ -37,9 +35,6 @@
<td>
<input type="text" class="input-text regular-input" value="<?php echo esc_attr( $amount ); ?>" id="woocommerce_payment_discounts_<?php echo esc_attr( $gateway->id ); ?>_amount" name="woocommerce_payment_discounts[<?php echo esc_attr( $gateway->id ); ?>][amount]" />
</td>
<td>
<label><input type="checkbox" value="yes" id="woocommerce_payment_discounts_<?php echo esc_attr( $gateway->id ); ?>_" name="woocommerce_payment_discounts[<?php echo esc_attr( $gateway->id ); ?>][include_tax]" <?php checked( 'yes', $include_tax, true ); ?>/> <?php esc_html_e( 'Enable/Disable', 'woocommerce-payment-discounts' ) ?></label>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?php
/**
* Apply discounts on checkout
*
* @package WC_Payment_Discounts/Classes
* @since 3.0.0
* @version 3.0.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Add discount.
* WC_Payment_Discounts_Apply_Discount class.
*/
class WC_Payment_Discounts_Add_Discount {
class WC_Payment_Discounts_Apply_Discount {

/**
* Initialize the actions.
Expand All @@ -16,7 +24,7 @@ public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

// Apply the discounts.
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_discount' ), 10 );
add_action( 'woocommerce_calculate_totals', array( $this, 'add_discount' ), 10 );

// Display the discount in payment gateways titles.
add_filter( 'woocommerce_gateway_title', array( $this, 'payment_method_title' ), 10, 2 );
Expand All @@ -34,38 +42,6 @@ public function enqueue_scripts() {
}
}

/**
* Calcule the discount amount.
*
* @param string|int|float $amount Discount value.
* @param float $total Cart subtotal.
*
* @return float Discount amount.
*/
protected function calculate_discount( $amount, $subtotal ) {
if ( strstr( $amount, '%' ) ) {
$amount = ( $subtotal / 100 ) * str_replace( '%', '', $amount );
}

return $amount;
}

/**
* Generate the discount name.
*
* @param mixed $amount Discount amount
* @param object $gateway Gateway data.
*
* @return string Discount name.
*/
protected function discount_name( $amount, $gateway ) {
if ( strstr( $amount, '%' ) ) {
return sprintf( __( 'Discount for %s (%s off)', 'woocommerce-payment-discounts' ), esc_attr( $gateway->title ), $amount );
}

return sprintf( __( 'Discount for %s', 'woocommerce-payment-discounts' ), esc_attr( $gateway->title ) );
}

/**
* Display the discount in payment method title.
*
Expand Down Expand Up @@ -96,6 +72,26 @@ public function payment_method_title( $title, $id ) {
return $title;
}

/**
* Remove payment coupons.
*
* @param WC_Cart $cart Cart object.
* @param string $skip Payment method ID to skip during remotion.
* @return bool
*/
protected function remove_payment_coupons( $cart, $skip = '' ) {
$removed = false;

foreach ( $cart->get_applied_coupons() as $code ) {
if ( 'wcpd_' === substr( $code, 0, 5 ) && $code !== $skip ) {
$cart->remove_coupon( $code );
$removed = true;
}
}

return $removed;
}

/**
* Add discount.
*
Expand All @@ -109,23 +105,26 @@ public function add_discount( $cart ) {
// Gets the settings.
$settings = get_option( 'woocommerce_payment_discounts' );

// Always set coupons calculation as enabled.
add_filter( 'woocommerce_coupons_enabled', '__return_true' );

if ( isset( $settings[ WC()->session->chosen_payment_method ]['amount'] ) ) {
// Gets the gateway discount.
$amount = $settings[ WC()->session->chosen_payment_method ]['amount'];
$include_tax = 'yes' === $settings[ WC()->session->chosen_payment_method ]['include_tax'];
$amount = $settings[ WC()->session->chosen_payment_method ]['amount'];

if ( apply_filters( 'wc_payment_discounts_apply_discount', 0 < $amount, $cart ) ) {

// Gets the gateway data.
$payment_gateways = WC()->payment_gateways->payment_gateways();
$gateway = $payment_gateways[ WC()->session->chosen_payment_method ];

// Generate the discount amount and title.
$discount_name = $this->discount_name( $amount, $gateway );
$cart_discount = $this->calculate_discount( $amount, $cart->cart_contents_total ) * -1;

// Apply the discount.
$cart->add_fee( $discount_name, $cart_discount, $include_tax );
$methods = WC()->payment_gateways->payment_gateways();
$gateway = $methods[ WC()->session->chosen_payment_method ];
$coupon = WC_Payment_Discounts_Coupons::get_coupon( $gateway->id, array( 'amount' => $amount ) );

// Remove other coupons and apply method coupon.
$this->remove_payment_coupons( $cart, $coupon->get_code() );
if ( ! $cart->has_discount( $coupon->get_code() ) ) {
$cart->add_discount( $coupon->get_code() );
}
} else {
$this->remove_payment_coupons( $cart );
}
}
}
Expand All @@ -144,4 +143,4 @@ public function update_order_data( $order_id ) {
}
}

new WC_Payment_Discounts_Add_Discount();
new WC_Payment_Discounts_Apply_Discount();
Loading

0 comments on commit 1bde5fa

Please sign in to comment.