woocommerce/tests/legacy/framework/helpers/class-wc-helper-fee.php

110 lines
2.6 KiB
PHP
Raw Normal View History

<?php
use Automattic\Jetpack\Constants;
/**
2015-11-03 13:31:20 +00:00
* Class WC_Helper_Fee.
*
2015-11-03 13:31:20 +00:00
* This helper class should ONLY be used for unit tests!.
*/
class WC_Helper_Fee {
/**
2015-11-03 13:31:20 +00:00
* Create a cart simple fee without taxes.
*
* @since 2.3
*/
public static function create_simple_fee() {
if ( is_admin() && ! Constants::is_defined( 'DOING_AJAX' ) ) {
return;
}
WC()->cart->add_fee( 'Dummy Fee', 10 );
}
/**
2017-08-22 20:26:55 +00:00
* Create a cart fee with taxes.
*
* @since 3.2
*/
public static function create_taxed_fee() {
if ( is_admin() && ! Constants::is_defined( 'DOING_AJAX' ) ) {
2017-08-22 20:26:55 +00:00
return;
}
WC()->cart->add_fee( 'Dummy Taxed Fee', 10, true );
}
/**
* Create a negative cart fee without taxes.
*
* @since 3.2
*/
public static function create_negative_fee() {
if ( is_admin() && ! Constants::is_defined( 'DOING_AJAX' ) ) {
2017-08-22 20:26:55 +00:00
return;
}
WC()->cart->add_fee( 'Dummy Negative Fee', -10 );
}
/**
* Create a negative cart fee with taxes.
*
* @since 3.2
*/
public static function create_negative_taxed_fee() {
if ( is_admin() && ! Constants::is_defined( 'DOING_AJAX' ) ) {
2017-08-22 20:26:55 +00:00
return;
}
WC()->cart->add_fee( 'Dummy Negative Taxed Fee', -10, true );
}
/**
* Add a cart fee.
2015-11-03 13:31:20 +00:00
* Note: need to be added before add any product in the cart.
*
* @since 2.3
2017-08-22 20:26:55 +00:00
* @param string $fee Type of fee to add (Default: simple)
*/
2017-08-22 20:26:55 +00:00
public static function add_cart_fee( $fee = '' ) {
switch ( $fee ) {
case 'taxed':
add_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_taxed_fee' ) );
break;
case 'negative':
add_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_negative_fee' ) );
break;
case 'negative-taxed':
add_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_negative_taxed_fee' ) );
break;
default:
add_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_simple_fee' ) );
}
}
/**
2017-08-22 20:26:55 +00:00
* Remove a cart fee.
*
* @since 2.3
2017-08-22 20:26:55 +00:00
* @param string $fee Type of fee to remove (Default: simple)
*/
2017-08-22 20:26:55 +00:00
public static function remove_cart_fee( $fee = '' ) {
switch ( $fee ) {
case 'taxed':
remove_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_taxed_fee' ) );
break;
case 'negative':
remove_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_negative_fee' ) );
break;
case 'negative-taxed':
remove_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_negative_taxed_fee' ) );
break;
default:
remove_action( 'woocommerce_cart_calculate_fees', array( __CLASS__, 'create_simple_fee' ) );
}
}
}