2014-10-30 19:58:19 +00:00
|
|
|
<?php
|
2020-06-23 13:10:27 +00:00
|
|
|
/**
|
|
|
|
* Helper class for shipping related unit tests.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tests|Helper
|
|
|
|
*/
|
2014-10-30 19:58:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Class WC_Helper_Shipping.
|
2014-10-30 19:58:19 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* This helper class should ONLY be used for unit tests!.
|
2014-10-30 19:58:19 +00:00
|
|
|
*/
|
|
|
|
class WC_Helper_Shipping {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Create a simple flat rate at the cost of 10.
|
2014-10-30 19:58:19 +00:00
|
|
|
*
|
|
|
|
* @since 2.3
|
2020-06-23 13:10:27 +00:00
|
|
|
*
|
|
|
|
* @param float $cost Optional. Cost of flat rate method.
|
2014-10-30 19:58:19 +00:00
|
|
|
*/
|
2020-06-23 13:10:27 +00:00
|
|
|
public static function create_simple_flat_rate( $cost = 10 ) {
|
2014-10-30 19:58:19 +00:00
|
|
|
$flat_rate_settings = array(
|
2015-07-09 02:40:29 +00:00
|
|
|
'enabled' => 'yes',
|
2016-10-12 10:16:30 +00:00
|
|
|
'title' => 'Flat rate',
|
2015-07-09 02:40:29 +00:00
|
|
|
'availability' => 'all',
|
|
|
|
'countries' => '',
|
|
|
|
'tax_status' => 'taxable',
|
2020-06-23 13:10:27 +00:00
|
|
|
'cost' => $cost,
|
2014-10-30 19:58:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
update_option( 'woocommerce_flat_rate_settings', $flat_rate_settings );
|
|
|
|
update_option( 'woocommerce_flat_rate', array() );
|
2016-05-25 11:36:25 +00:00
|
|
|
WC_Cache_Helper::get_transient_version( 'shipping', true );
|
2018-10-02 15:03:17 +00:00
|
|
|
WC()->shipping()->load_shipping_methods();
|
2014-10-30 19:58:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 13:10:27 +00:00
|
|
|
/**
|
|
|
|
* Helper function to set customer address so that shipping can be calculated.
|
|
|
|
*/
|
|
|
|
public static function force_customer_us_address() {
|
|
|
|
add_filter( 'woocommerce_customer_get_shipping_country', array( self::class, 'force_customer_us_country' ) );
|
|
|
|
add_filter( 'woocommerce_customer_get_shipping_state', array( self::class, 'force_customer_us_state' ) );
|
|
|
|
add_filter( 'woocommerce_customer_get_shipping_postcode', array( self::class, 'force_customer_us_postcode' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper that can be hooked to a filter to force the customer's shipping state to be NY.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @param string $state State code.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function force_customer_us_state( $state ) {
|
|
|
|
return 'NY';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper that can be hooked to a filter to force the customer's shipping country to be US.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @param string $country Country code.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function force_customer_us_country( $country ) {
|
|
|
|
return 'US';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper that can be hooked to a filter to force the customer's shipping postal code to be 12345.
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
* @param string $postcode Postal code.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function force_customer_us_postcode( $postcode ) {
|
|
|
|
return '12345';
|
|
|
|
}
|
|
|
|
|
2014-10-30 19:58:19 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Delete the simple flat rate.
|
2014-10-30 19:58:19 +00:00
|
|
|
*
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
|
|
|
public static function delete_simple_flat_rate() {
|
|
|
|
delete_option( 'woocommerce_flat_rate_settings' );
|
|
|
|
delete_option( 'woocommerce_flat_rate' );
|
2016-05-25 11:36:25 +00:00
|
|
|
WC_Cache_Helper::get_transient_version( 'shipping', true );
|
2018-10-02 15:03:17 +00:00
|
|
|
WC()->shipping()->unregister_shipping_methods();
|
2014-10-30 19:58:19 +00:00
|
|
|
}
|
|
|
|
}
|