1 <?php
  2 /**
  3  * WooCommerce Cart Functions
  4  *
  5  * Functions for cart specific things.
  6  *
  7  * @author      WooThemes
  8  * @category    Core
  9  * @package     WooCommerce/Functions
 10  * @version     2.1.0
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 14 
 15 /**
 16  * Prevent password protected products being added to the cart
 17  *
 18  * @param  bool $passed
 19  * @param  int $product_id
 20  * @return bool
 21  */
 22 function wc_protected_product_add_to_cart( $passed, $product_id ) {
 23     if ( post_password_required( $product_id ) ) {
 24         $passed = false;
 25         wc_add_notice( __( 'This product is protected and cannot be purchased.', 'woocommerce' ), 'error' );
 26     }
 27     return $passed;
 28 }
 29 add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );
 30 
 31 /**
 32  * Clears the cart session when called
 33  *
 34  * @return void
 35  */
 36 function wc_empty_cart() {
 37     if ( ! isset( WC()->cart ) || WC()->cart == '' )
 38         WC()->cart = new WC_Cart();
 39 
 40     WC()->cart->empty_cart( false );
 41 }
 42 add_action( 'wp_logout', 'wc_empty_cart' );
 43 
 44 
 45 /**
 46  * Load the cart upon login
 47  *
 48  * @param mixed $user_login
 49  * @param mixed $user
 50  * @return void
 51  */
 52 function wc_load_persistent_cart( $user_login, $user = 0 ) {
 53 
 54     if ( ! $user )
 55         return;
 56 
 57     $saved_cart = get_user_meta( $user->ID, '_woocommerce_persistent_cart', true );
 58 
 59     if ( $saved_cart )
 60         if ( empty( WC()->session->cart ) || ! is_array( WC()->session->cart ) || sizeof( WC()->session->cart ) == 0 )
 61             WC()->session->cart = $saved_cart['cart'];
 62 }
 63 add_action( 'wp_login', 'wc_load_persistent_cart', 1, 2 );
 64 
 65 
 66 /**
 67  * Add to cart messages.
 68  *
 69  * @access public
 70  * @param int|array $product_id
 71  * @return void
 72  */
 73 function wc_add_to_cart_message( $product_id ) {
 74 
 75     if ( is_array( $product_id ) ) {
 76 
 77         $titles = array();
 78 
 79         foreach ( $product_id as $id ) {
 80             $titles[] = get_the_title( $id );
 81         }
 82 
 83         $added_text = sprintf( __( 'Added &quot;%s&quot; to your cart.', 'woocommerce' ), join( __( '&quot; and &quot;', 'woocommerce' ), array_filter( array_merge( array( join( '&quot;, &quot;', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );
 84 
 85     } else {
 86         $added_text = sprintf( __( '&quot;%s&quot; was successfully added to your cart.', 'woocommerce' ), get_the_title( $product_id ) );
 87     }
 88 
 89     // Output success messages
 90     if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
 91 
 92         $return_to  = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
 93 
 94         $message    = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
 95 
 96     else :
 97 
 98         $message    = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', get_permalink( wc_get_page_id( 'cart' ) ), __( 'View Cart', 'woocommerce' ), $added_text );
 99 
100     endif;
101 
102     wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
103 }
104 
105 /**
106  * Clear cart after payment.
107  *
108  * @access public
109  * @return void
110  */
111 function wc_clear_cart_after_payment() {
112     global $wp;
113 
114     if ( ! empty( $wp->query_vars['order-received'] ) ) {
115 
116         $order_id = absint( $wp->query_vars['order-received'] );
117 
118         if ( isset( $_GET['key'] ) )
119             $order_key = $_GET['key'];
120         else
121             $order_key = '';
122 
123         if ( $order_id > 0 ) {
124             $order = new WC_Order( $order_id );
125 
126             if ( $order->order_key == $order_key ) {
127                 WC()->cart->empty_cart();
128             }
129         }
130 
131     }
132 
133     if ( WC()->session->order_awaiting_payment > 0 ) {
134 
135         $order = new WC_Order( WC()->session->order_awaiting_payment );
136 
137         if ( $order->id > 0 ) {
138             // If the order has not failed, or is not pending, the order must have gone through
139             if ( $order->status != 'failed' && $order->status != 'pending' )
140                 WC()->cart->empty_cart();
141         }
142     }
143 }
144 add_action( 'get_header', 'wc_clear_cart_after_payment' );
145 
146 /**
147  * Get the subtotal
148  *
149  * @access public
150  * @return string
151  */
152 function wc_cart_totals_subtotal_html() {
153     echo WC()->cart->get_cart_subtotal();
154 }
155 
156 /**
157  * Get shipping methods
158  *
159  * @access public
160  * @return void
161  */
162 function wc_cart_totals_shipping_html() {
163     $packages = WC()->shipping->get_packages();
164 
165     foreach ( $packages as $i => $package ) {
166         $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
167 
168         wc_get_template( 'cart/cart-shipping.php', array( 'package' => $package, 'available_methods' => $package['rates'], 'show_package_details' => ( sizeof( $packages ) > 1 ), 'index' => $i, 'chosen_method' => $chosen_method ) );
169     }
170 }
171 
172 /**
173  * Get taxes total
174  *
175  * @access public
176  * @return void
177  */
178 function wc_cart_totals_taxes_total_html() {
179     echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) );
180 }
181 
182 /**
183  * Get a coupon label
184  *
185  * @access public
186  * @param string $coupon
187  * @return void
188  */
189 function wc_cart_totals_coupon_label( $coupon ) {
190     if ( is_string( $coupon ) )
191         $coupon = new WC_Coupon( $coupon );
192 
193     echo apply_filters( 'woocommerce_cart_totals_coupon_label', esc_html( __( 'Coupon:', 'woocommerce' ) . ' ' . $coupon->code ), $coupon );
194 }
195 
196 /**
197  * Get a coupon value
198  *
199  * @access public
200  * @param string $coupon
201  * @return void
202  */
203 function wc_cart_totals_coupon_html( $coupon ) {
204     if ( is_string( $coupon ) ) {
205         $coupon = new WC_Coupon( $coupon );
206     }
207 
208     $value  = array();
209 
210     if ( ! empty( WC()->cart->coupon_discount_amounts[ $coupon->code ] ) ) {
211         $discount_html = '-' . wc_price( WC()->cart->coupon_discount_amounts[ $coupon->code ] );
212     } else {
213         $discount_html = '';
214     }
215 
216     $value[] = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_html, $coupon );
217 
218     if ( $coupon->enable_free_shipping() ) {
219         $value[] = __( 'Free shipping coupon', 'woocommerce' );
220     }
221 
222     // get rid of empty array elements
223     $value = array_filter( $value );
224 
225     $value = implode( ', ', $value ) . ' <a href="' . add_query_arg( 'remove_coupon', $coupon->code, defined( 'WOOCOMMERCE_CHECKOUT' ) ? WC()->cart->get_checkout_url() : WC()->cart->get_cart_url() ) . '" class="woocommerce-remove-coupon">' . __( '[Remove]', 'woocommerce' ) . '</a>';
226 
227     echo apply_filters( 'woocommerce_cart_totals_coupon_html', $value, $coupon );
228 }
229 
230 /**
231  * Get order total html including inc tax if needed
232  *
233  * @access public
234  * @return void
235  */
236 function wc_cart_totals_order_total_html() {
237     echo '<strong>' . WC()->cart->get_total() . '</strong> ';
238 
239     // If prices are tax inclusive, show taxes here
240     if ( get_option( 'woocommerce_calc_taxes' ) == 'yes' && WC()->cart->tax_display_cart == 'incl' ) {
241         $tax_string_array = array();
242 
243         if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
244             foreach ( WC()->cart->get_tax_totals() as $code => $tax )
245                 $tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
246         } else {
247             $tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
248         }
249 
250         if ( ! empty( $tax_string_array ) )
251             echo '<small class="includes_tax">' . sprintf( __( '(Includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) ) . '</small>';
252     }
253 }
254 
255 /**
256  * Get the fee value
257  *
258  * @param object $fee
259  * @return void
260  */
261 function wc_cart_totals_fee_html( $fee ) {
262     $cart_totals_fee_html = ( 'excl' == WC()->cart->tax_display_cart ) ? wc_price( $fee->amount ) : wc_price( $fee->amount + $fee->tax );
263 
264     echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee );
265 }
266 
267 /**
268  * Get a shipping methods full label including price
269  * @param  object $method
270  * @return string
271  */
272 function wc_cart_totals_shipping_method_label( $method ) {
273     $label = $method->label;
274 
275     if ( $method->cost > 0 ) {
276         if ( WC()->cart->tax_display_cart == 'excl' ) {
277             $label .= ': ' . wc_price( $method->cost );
278             if ( $method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax ) {
279                 $label .= ' <small>' . WC()->countries->ex_tax_or_vat() . '</small>';
280             }
281         } else {
282             $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
283             if ( $method->get_shipping_tax() > 0 && ! WC()->cart->prices_include_tax ) {
284                 $label .= ' <small>' . WC()->countries->inc_tax_or_vat() . '</small>';
285             }
286         }
287     } elseif ( $method->id !== 'free_shipping' ) {
288         $label .= ' (' . __( 'Free', 'woocommerce' ) . ')';
289     }
290 
291     return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
292 }
293 
WooCommerce API documentation generated by ApiGen 2.8.0