Merge pull request #9457 from woothemes/9251-wc-cart-functions

Moving some methods from WC_Cart to new functions
This commit is contained in:
Mike Jolley 2015-10-30 13:12:55 +00:00
commit 92310feb98
17 changed files with 198 additions and 160 deletions

View File

@ -330,10 +330,12 @@ class WC_Cart {
/**
* Coupons enabled function. Filterable.
*
* @deprecated 2.5.0 in favor to wc_coupons_enabled()
*
* @return bool
*/
public function coupons_enabled() {
return apply_filters( 'woocommerce_coupons_enabled', get_option( 'woocommerce_enable_coupons' ) == 'yes' );
return wc_coupons_enabled();
}
/**
@ -622,32 +624,29 @@ class WC_Cart {
/**
* Gets the url to the cart page.
*
* @deprecated 2.5.0 in favor to wc_get_cart_url()
*
* @return string url to page
*/
public function get_cart_url() {
return apply_filters( 'woocommerce_get_cart_url', wc_get_page_permalink( 'cart' ) );
return wc_get_cart_url();
}
/**
* Gets the url to the checkout page.
*
* @deprecated 2.5.0 in favor to wc_get_checkout_url()
*
* @return string url to page
*/
public function get_checkout_url() {
$checkout_url = wc_get_page_permalink( 'checkout' );
if ( $checkout_url ) {
// Force SSL if needed
if ( is_ssl() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ) {
$checkout_url = str_replace( 'http:', 'https:', $checkout_url );
}
}
return apply_filters( 'woocommerce_get_checkout_url', $checkout_url );
return wc_get_checkout_url();
}
/**
* Gets the url to remove an item from the cart.
*
* @param string cart_item_key contains the id of the cart item
* @param string cart_item_key contains the id of the cart item
* @return string url to page
*/
public function get_remove_url( $cart_item_key ) {
@ -885,7 +884,7 @@ class WC_Cart {
$in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;
if ( $in_cart_quantity > 0 ) {
throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', $this->get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another &quot;%s&quot; to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another &quot;%s&quot; to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
}
}
@ -919,7 +918,7 @@ class WC_Cart {
if ( ! $product_data->has_enough_stock( $check_qty + $quantity ) ) {
throw new Exception( sprintf(
'<a href="%s" class="button wc-forward">%s</a> %s',
$this->get_cart_url(),
wc_get_cart_url(),
__( 'View Cart', 'woocommerce' ),
sprintf( __( 'You cannot add that amount to the cart &mdash; we have %s in stock and you already have %s in your cart.', 'woocommerce' ), $product_data->get_stock_quantity(), $check_qty )
) );
@ -1493,7 +1492,7 @@ class WC_Cart {
$needs_shipping_address = false;
if ( $this->needs_shipping() === true && ! $this->ship_to_billing_address_only() ) {
if ( $this->needs_shipping() === true && ! wc_ship_to_billing_address_only() ) {
$needs_shipping_address = true;
}
@ -1525,6 +1524,8 @@ class WC_Cart {
/**
* Sees if we need a shipping address.
*
* @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only()
*
* @return bool
*/
public function ship_to_billing_address_only() {
@ -1669,7 +1670,7 @@ class WC_Cart {
*/
public function add_discount( $coupon_code ) {
// Coupons are globally disabled
if ( ! $this->coupons_enabled() ) {
if ( ! wc_coupons_enabled() ) {
return false;
}
@ -1802,7 +1803,7 @@ class WC_Cart {
*/
public function remove_coupon( $coupon_code ) {
// Coupons are globally disabled
if ( ! $this->coupons_enabled() ) {
if ( ! wc_coupons_enabled() ) {
return false;
}

View File

@ -374,7 +374,7 @@ class WC_Checkout {
}
// Ship to billing only option
if ( WC()->cart->ship_to_billing_address_only() ) {
if ( wc_ship_to_billing_address_only() ) {
$this->posted['ship_to_different_address'] = false;
}

View File

@ -409,7 +409,7 @@ class WC_Form_Handler {
}
}
$referer = wp_get_referer() ? remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) : WC()->cart->get_cart_url();
$referer = wp_get_referer() ? remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) : wc_get_cart_url();
wp_safe_redirect( $referer );
exit;
}
@ -420,7 +420,7 @@ class WC_Form_Handler {
WC()->cart->restore_cart_item( $cart_item_key );
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) : WC()->cart->get_cart_url();
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) : wc_get_cart_url();
wp_safe_redirect( $referer );
exit;
}
@ -473,11 +473,11 @@ class WC_Form_Handler {
}
if ( ! empty( $_POST['proceed'] ) ) {
wp_safe_redirect( WC()->cart->get_checkout_url() );
wp_safe_redirect( wc_get_checkout_url() );
exit;
} elseif ( $cart_updated ) {
wc_add_notice( __( 'Cart updated.', 'woocommerce' ) );
$referer = remove_query_arg( 'remove_coupon', ( wp_get_referer() ? wp_get_referer() : WC()->cart->get_cart_url() ) );
$referer = remove_query_arg( 'remove_coupon', ( wp_get_referer() ? wp_get_referer() : wc_get_cart_url() ) );
wp_safe_redirect( $referer );
exit;
}
@ -543,7 +543,7 @@ class WC_Form_Handler {
// Redirect to cart
wc_add_notice( __( 'The cart has been filled with the items from your previous order.', 'woocommerce' ) );
wp_safe_redirect( WC()->cart->get_cart_url() );
wp_safe_redirect( wc_get_cart_url() );
exit;
}
@ -631,7 +631,7 @@ class WC_Form_Handler {
wp_safe_redirect( $url );
exit;
} elseif ( get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' ) {
wp_safe_redirect( WC()->cart->get_cart_url() );
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}

View File

@ -307,7 +307,7 @@ class WC_Frontend_Scripts {
'ajax_url' => WC()->ajax_url(),
'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
'i18n_view_cart' => esc_attr__( 'View Cart', 'woocommerce' ),
'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', WC()->cart->get_cart_url() ),
'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url() ),
'is_cart' => is_cart(),
'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' )
);

View File

@ -4,10 +4,10 @@
*
* Functions for cart specific things.
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @version 2.1.0
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -206,7 +206,7 @@ function wc_cart_totals_coupon_label( $coupon ) {
function wc_cart_totals_coupon_html( $coupon ) {
if ( is_string( $coupon ) ) {
$coupon = new WC_Coupon( $coupon );
}
}
$value = array();
@ -220,11 +220,11 @@ function wc_cart_totals_coupon_html( $coupon ) {
if ( $coupon->enable_free_shipping() ) {
$value[] = __( 'Free shipping coupon', 'woocommerce' );
}
}
// get rid of empty array elements
$value = array_filter( $value );
$value = implode( ', ', $value ) . ' <a href="' . esc_url( add_query_arg( 'remove_coupon', urlencode( $coupon->code ), defined( 'WOOCOMMERCE_CHECKOUT' ) ? WC()->cart->get_checkout_url() : WC()->cart->get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->code ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';
// get rid of empty array elements
$value = array_filter( $value );
$value = implode( ', ', $value ) . ' <a href="' . esc_url( add_query_arg( 'remove_coupon', urlencode( $coupon->code ), defined( 'WOOCOMMERCE_CHECKOUT' ) ? wc_get_checkout_url() : wc_get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->code ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';
echo apply_filters( 'woocommerce_cart_totals_coupon_html', $value, $coupon );
}
@ -308,3 +308,33 @@ function wc_cart_round_discount( $value, $precision ) {
return round( $value, $precision );
}
}
/**
* Gets the url to the cart page
*
* @since 2.5.0
*
* @return string Url to cart page
*/
function wc_get_cart_url() {
return apply_filters( 'woocommerce_get_cart_url', wc_get_page_permalink( 'cart' ) );
}
/**
* Gets the url to the checkout page
*
* @since 2.5.0
*
* @return string Url to checkout page
*/
function wc_get_checkout_url() {
$checkout_url = wc_get_page_permalink( 'checkout' );
if ( $checkout_url ) {
// Force SSL if needed
if ( is_ssl() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ) {
$checkout_url = str_replace( 'http:', 'https:', $checkout_url );
}
}
return apply_filters( 'woocommerce_get_checkout_url', $checkout_url );
}

View File

@ -41,3 +41,15 @@ function wc_get_coupon_type( $type = '' ) {
return '';
}
/**
* Check if coupons are enabled.
* Filterable.
*
* @since 2.5.0
*
* @return bool
*/
function wc_coupons_enabled() {
return apply_filters( 'woocommerce_coupons_enabled', 'yes' == get_option( 'woocommerce_enable_coupons' ) );
}

View File

@ -9,7 +9,7 @@
* as little as possible, but it does happen. When this occurs the version of the template file will
* be bumped and the readme will list any important changes.
*
* @see http://docs.woothemes.com/document/template-structure/
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.3.8
@ -23,7 +23,7 @@ wc_print_notices();
do_action( 'woocommerce_before_cart' ); ?>
<form action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
<form action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
@ -130,7 +130,7 @@ do_action( 'woocommerce_before_cart' ); ?>
<tr>
<td colspan="6" class="actions">
<?php if ( WC()->cart->coupons_enabled() ) { ?>
<?php if ( wc_coupons_enabled() ) { ?>
<div class="coupon">
<label for="coupon_code"><?php _e( 'Coupon', 'woocommerce' ); ?>:</label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <input type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply Coupon', 'woocommerce' ); ?>" />

View File

@ -11,10 +11,10 @@
* as little as possible, but it does happen. When this occurs the version of the template file will
* be bumped and the readme will list any important changes.
*
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.0
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -81,8 +81,8 @@ if ( ! defined( 'ABSPATH' ) ) {
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
<p class="buttons">
<a href="<?php echo WC()->cart->get_cart_url(); ?>" class="button wc-forward"><?php _e( 'View Cart', 'woocommerce' ); ?></a>
<a href="<?php echo WC()->cart->get_checkout_url(); ?>" class="button checkout wc-forward"><?php _e( 'Checkout', 'woocommerce' ); ?></a>
<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="button wc-forward"><?php _e( 'View Cart', 'woocommerce' ); ?></a>
<a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="button checkout wc-forward"><?php _e( 'Checkout', 'woocommerce' ); ?></a>
</p>
<?php endif; ?>

View File

@ -11,7 +11,7 @@
* as little as possible, but it does happen. When this occurs the version of the template file will
* be bumped and the readme will list any important changes.
*
* @see http://docs.woothemes.com/document/template-structure/
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.4.0
@ -21,4 +21,4 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
echo '<a href="' . esc_url( WC()->cart->get_checkout_url() ) . '" class="checkout-button button alt wc-forward">' . __( 'Proceed to Checkout', 'woocommerce' ) . '</a>';
echo '<a href="' . esc_url( wc_get_checkout_url() ) . '" class="checkout-button button alt wc-forward">' . __( 'Proceed to Checkout', 'woocommerce' ) . '</a>';

View File

@ -27,7 +27,7 @@ if ( get_option( 'woocommerce_enable_shipping_calc' ) === 'no' || ! WC()->cart->
<?php do_action( 'woocommerce_before_shipping_calculator' ); ?>
<form class="woocommerce-shipping-calculator" action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
<form class="woocommerce-shipping-calculator" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<p><a href="#" class="shipping-calculator-button"><?php _e( 'Calculate Shipping', 'woocommerce' ); ?></a></p>

View File

@ -9,10 +9,10 @@
* as little as possible, but it does happen. When this occurs the version of the template file will
* be bumped and the readme will list any important changes.
*
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.2
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.1.2
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -23,7 +23,7 @@ if ( ! defined( 'ABSPATH' ) ) {
?>
<div class="woocommerce-billing-fields">
<?php if ( WC()->cart->ship_to_billing_address_only() && WC()->cart->needs_shipping() ) : ?>
<?php if ( wc_ship_to_billing_address_only() && WC()->cart->needs_shipping() ) : ?>
<h3><?php _e( 'Billing &amp; Shipping', 'woocommerce' ); ?></h3>

View File

@ -29,10 +29,9 @@ if ( ! $checkout->enable_signup && ! $checkout->enable_guest_checkout && ! is_us
return;
}
// filter hook for include new pages inside the payment method
$get_checkout_url = apply_filters( 'woocommerce_get_checkout_url', WC()->cart->get_checkout_url() ); ?>
?>
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( $get_checkout_url ); ?>" enctype="multipart/form-data">
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>

View File

@ -9,17 +9,17 @@
* as little as possible, but it does happen. When this occurs the version of the template file will
* be bumped and the readme will list any important changes.
*
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.2
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! WC()->cart->coupons_enabled() ) {
if ( ! wc_coupons_enabled() ) {
return;
}

View File

@ -9,10 +9,10 @@
* as little as possible, but it does happen. When this occurs the version of the template file will
* be bumped and the readme will list any important changes.
*
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.2.0
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -61,7 +61,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<?php if ( apply_filters( 'woocommerce_enable_order_notes_field', get_option( 'woocommerce_enable_order_comments', 'yes' ) === 'yes' ) ) : ?>
<?php if ( ! WC()->cart->needs_shipping() || WC()->cart->ship_to_billing_address_only() ) : ?>
<?php if ( ! WC()->cart->needs_shipping() || wc_ship_to_billing_address_only() ) : ?>
<h3><?php _e( 'Additional Information', 'woocommerce' ); ?></h3>

View File

@ -8,88 +8,6 @@ namespace WooCommerce\Tests\Cart;
*/
class Cart extends \WC_Unit_Test_Case {
/**
* Helper method to get the checkout URL
*
* @since 2.3
* @return string
*/
private function get_checkout_url() {
// Get the checkout URL
$checkout_page_id = wc_get_page_id( 'checkout' );
$checkout_url = '';
// Check if there is a checkout page
if ( $checkout_page_id ) {
// Get the permalink
$checkout_url = get_permalink( $checkout_page_id );
// Force SSL if needed
if ( is_ssl() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ) {
$checkout_url = str_replace( 'http:', 'https:', $checkout_url );
}
// Allow filtering of checkout URL
$checkout_url = apply_filters( 'woocommerce_get_checkout_url', $checkout_url );
}
return $checkout_url;
}
/**
* Test get_checkout_url over HTTP
*
* @since 2.3
*/
public function test_get_checkout_url_regular() {
// Get the original setting
$o_setting = get_option( 'woocommerce_force_ssl_checkout' );
// Force SSL checkout
update_option( 'woocommerce_force_ssl_checkout', 'no' );
$this->assertEquals( $this->get_checkout_url(), WC()->cart->get_checkout_url() );
// Restore option
update_option( 'woocommerce_force_ssl_checkout', $o_setting );
}
/**
* Test get_checkout_url over HTTP
*
* @since 2.3
*/
public function test_get_checkout_url_ssl() {
// Get the original setting
$o_setting = get_option( 'woocommerce_force_ssl_checkout' );
// Force SSL checkout
update_option( 'woocommerce_force_ssl_checkout', 'yes' );
$this->assertEquals( $this->get_checkout_url(), WC()->cart->get_checkout_url() );
// Restore option
update_option( 'woocommerce_force_ssl_checkout', $o_setting );
}
/**
* Test get_cart_url method
*
* @since 2.3
*/
public function test_get_cart_url() {
$cart_page_url = wc_get_page_permalink( 'cart' );
$this->assertEquals( apply_filters( 'woocommerce_get_cart_url', $cart_page_url ? $cart_page_url : '' ), WC()->cart->get_cart_url() );
}
/**
* Test get_remove_url
*
@ -409,27 +327,13 @@ class Cart extends \WC_Unit_Test_Case {
update_option( 'woocommerce_calc_taxes', 'no' );
}
/**
* Test coupons_enabled method
*/
public function test_coupons_enabled() {
$this->assertEquals( apply_filters( 'woocommerce_coupons_enabled', get_option( 'woocommerce_enable_coupons' ) == 'yes' ), WC()->cart->coupons_enabled() );
}
/**
* Test ship_to_billing_address_only method
*/
public function test_ship_to_billing_address_only() {
$this->assertEquals( wc_ship_to_billing_address_only(), WC()->cart->ship_to_billing_address_only() );
}
/**
* Test needs_shipping_address method
*/
public function test_needs_shipping_address() {
$needs_shipping_address = false;
if ( WC()->cart->needs_shipping() === true && ! WC()->cart->ship_to_billing_address_only() ) {
if ( WC()->cart->needs_shipping() === true && ! wc_ship_to_billing_address_only() ) {
$needs_shipping_address = true;
}

View File

@ -8,6 +8,78 @@ namespace WooCommerce\Tests\Cart;
*/
class Functions extends \WC_Unit_Test_Case {
/**
* Helper method to get the checkout URL
*
* @since 2.5.0
*
* @return string
*/
private function get_checkout_url() {
// Get the checkout URL
$checkout_page_id = wc_get_page_id( 'checkout' );
$checkout_url = '';
// Check if there is a checkout page
if ( $checkout_page_id ) {
// Get the permalink
$checkout_url = get_permalink( $checkout_page_id );
// Force SSL if needed
if ( is_ssl() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ) {
$checkout_url = str_replace( 'http:', 'https:', $checkout_url );
}
// Allow filtering of checkout URL
$checkout_url = apply_filters( 'woocommerce_get_checkout_url', $checkout_url );
}
return $checkout_url;
}
/**
* Test get_checkout_url over HTTP
*
* @since 2.5.0
*/
public function test_get_checkout_url_regular() {
// Get the original setting
$o_setting = get_option( 'woocommerce_force_ssl_checkout' );
// Force SSL checkout
update_option( 'woocommerce_force_ssl_checkout', 'no' );
$this->assertEquals( $this->get_checkout_url(), wc_get_checkout_url() );
// Restore option
update_option( 'woocommerce_force_ssl_checkout', $o_setting );
}
/**
* Test get_checkout_url over HTTP
*
* @since 2.5.0
*/
public function test_get_checkout_url_ssl() {
// Get the original setting
$o_setting = get_option( 'woocommerce_force_ssl_checkout' );
// Force SSL checkout
update_option( 'woocommerce_force_ssl_checkout', 'yes' );
$this->assertEquals( $this->get_checkout_url(), wc_get_checkout_url() );
// Restore option
update_option( 'woocommerce_force_ssl_checkout', $o_setting );
}
/**
* Test wc_empty_cart()
*
@ -69,4 +141,15 @@ class Functions extends \WC_Unit_Test_Case {
\WC_Helper_Coupon::delete_coupon( $coupon->id );
}
/**
* Test get_cart_url method
*
* @since 2.5.0
*/
public function test_wc_get_cart_url() {
$cart_page_url = wc_get_page_permalink( 'cart' );
$this->assertEquals( apply_filters( 'woocommerce_get_cart_url', $cart_page_url ? $cart_page_url : '' ), wc_get_cart_url() );
}
}

View File

@ -37,4 +37,13 @@ class Functions extends \WC_Unit_Test_Case {
$this->assertEmpty( wc_get_coupon_type( 'bogus_type' ) );
}
/**
* Test coupons_enabled method
*
* @since 2.5.0
*/
public function test_wc_coupons_enabled() {
$this->assertEquals( apply_filters( 'woocommerce_coupons_enabled', get_option( 'woocommerce_enable_coupons' ) == 'yes' ), wc_coupons_enabled() );
}
}