PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Site/wp-content/plugins/woocommerce/includes/wc-cart-functions.php

https://gitlab.com/rubengrb/ws.vidrialum
PHP | 374 lines | 208 code | 54 blank | 112 comment | 50 complexity | 44fd2164bd333b4e7decb6c3e4bbc5d2 MD5 | raw file
  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.5.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. 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. * Clears the cart session when called.
  32. */
  33. function wc_empty_cart() {
  34. if ( ! isset( WC()->cart ) || '' === WC()->cart ) {
  35. WC()->cart = new WC_Cart();
  36. }
  37. WC()->cart->empty_cart( false );
  38. }
  39. /**
  40. * Load the persistent cart.
  41. *
  42. * @param string $user_login
  43. * @param WP_User $user
  44. * @deprecated 2.3
  45. */
  46. function wc_load_persistent_cart( $user_login, $user ) {
  47. if ( ! $user || ! ( $saved_cart = get_user_meta( $user->ID, '_woocommerce_persistent_cart', true ) ) ) {
  48. return;
  49. }
  50. if ( empty( WC()->session->cart ) || ! is_array( WC()->session->cart ) || 0 === sizeof( WC()->session->cart ) ) {
  51. WC()->session->cart = $saved_cart['cart'];
  52. }
  53. }
  54. /**
  55. * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
  56. *
  57. * Do not use for redirects, use {@see wp_get_referer()} instead.
  58. *
  59. * @since 2.6.1
  60. * @return string|false Referer URL on success, false on failure.
  61. */
  62. function wc_get_raw_referer() {
  63. if ( function_exists( 'wp_get_raw_referer' ) ) {
  64. return wp_get_raw_referer();
  65. }
  66. if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
  67. return wp_unslash( $_REQUEST['_wp_http_referer'] );
  68. } elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
  69. return wp_unslash( $_SERVER['HTTP_REFERER'] );
  70. }
  71. return false;
  72. }
  73. /**
  74. * Add to cart messages.
  75. *
  76. * @access public
  77. * @param int|array $products
  78. * @param bool $show_qty Should qty's be shown? Added in 2.6.0
  79. */
  80. function wc_add_to_cart_message( $products, $show_qty = false ) {
  81. $titles = array();
  82. $count = 0;
  83. if ( ! is_array( $products ) ) {
  84. $products = array( $products );
  85. $show_qty = false;
  86. }
  87. if ( ! $show_qty ) {
  88. $products = array_fill_keys( array_keys( $products ), 1 );
  89. }
  90. foreach ( $products as $product_id => $qty ) {
  91. $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
  92. $count += $qty;
  93. }
  94. $titles = array_filter( $titles );
  95. $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );
  96. // Output success messages
  97. if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
  98. $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
  99. $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
  100. } else {
  101. $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
  102. }
  103. wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
  104. }
  105. /**
  106. * Comma separate a list of item names, and replace final comma with 'and'
  107. * @param array $items
  108. * @return string
  109. */
  110. function wc_format_list_of_items( $items ) {
  111. $item_string = '';
  112. foreach ( $items as $key => $item ) {
  113. $item_string .= $item;
  114. if ( $key + 2 === sizeof( $items ) ) {
  115. $item_string .= ' ' . __( 'and', 'woocommerce' ) . ' ';
  116. } elseif ( $key + 1 !== sizeof( $items ) ) {
  117. $item_string .= ', ';
  118. }
  119. }
  120. return $item_string;
  121. }
  122. /**
  123. * Clear cart after payment.
  124. *
  125. * @access public
  126. */
  127. function wc_clear_cart_after_payment() {
  128. global $wp;
  129. if ( ! empty( $wp->query_vars['order-received'] ) ) {
  130. $order_id = absint( $wp->query_vars['order-received'] );
  131. $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
  132. if ( $order_id > 0 ) {
  133. $order = wc_get_order( $order_id );
  134. if ( $order->order_key === $order_key ) {
  135. WC()->cart->empty_cart();
  136. }
  137. }
  138. }
  139. if ( WC()->session->order_awaiting_payment > 0 ) {
  140. $order = wc_get_order( WC()->session->order_awaiting_payment );
  141. if ( $order && $order->id > 0 ) {
  142. // If the order has not failed, or is not pending, the order must have gone through
  143. if ( ! $order->has_status( array( 'failed', 'pending', 'cancelled' ) ) ) {
  144. WC()->cart->empty_cart();
  145. }
  146. }
  147. }
  148. }
  149. add_action( 'get_header', 'wc_clear_cart_after_payment' );
  150. /**
  151. * Get the subtotal.
  152. *
  153. * @access public
  154. * @return string
  155. */
  156. function wc_cart_totals_subtotal_html() {
  157. echo WC()->cart->get_cart_subtotal();
  158. }
  159. /**
  160. * Get shipping methods.
  161. *
  162. * @access public
  163. */
  164. function wc_cart_totals_shipping_html() {
  165. $packages = WC()->shipping->get_packages();
  166. foreach ( $packages as $i => $package ) {
  167. $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';
  168. $product_names = array();
  169. if ( sizeof( $packages ) > 1 ) {
  170. foreach ( $package['contents'] as $item_id => $values ) {
  171. $product_names[] = $values['data']->get_title() . ' &times;' . $values['quantity'];
  172. }
  173. }
  174. wc_get_template( 'cart/cart-shipping.php', array(
  175. 'package' => $package,
  176. 'available_methods' => $package['rates'],
  177. 'show_package_details' => sizeof( $packages ) > 1,
  178. 'package_details' => implode( ', ', $product_names ),
  179. 'package_name' => apply_filters( 'woocommerce_shipping_package_name', sprintf( _n( 'Shipping', 'Shipping %d', ( $i + 1 ), 'woocommerce' ), ( $i + 1 ) ), $i, $package ),
  180. 'index' => $i,
  181. 'chosen_method' => $chosen_method
  182. ) );
  183. }
  184. }
  185. /**
  186. * Get taxes total.
  187. *
  188. * @access public
  189. */
  190. function wc_cart_totals_taxes_total_html() {
  191. echo apply_filters( 'woocommerce_cart_totals_taxes_total_html', wc_price( WC()->cart->get_taxes_total() ) );
  192. }
  193. /**
  194. * Get a coupon label.
  195. *
  196. * @access public
  197. * @param string $coupon
  198. * @param bool $echo or return
  199. */
  200. function wc_cart_totals_coupon_label( $coupon, $echo = true ) {
  201. if ( is_string( $coupon ) ) {
  202. $coupon = new WC_Coupon( $coupon );
  203. }
  204. $label = apply_filters( 'woocommerce_cart_totals_coupon_label', esc_html( __( 'Coupon:', 'woocommerce' ) . ' ' . $coupon->code ), $coupon );
  205. if ( $echo ) {
  206. echo $label;
  207. } else {
  208. return $label;
  209. }
  210. }
  211. /**
  212. * Get a coupon value.
  213. *
  214. * @access public
  215. * @param string $coupon
  216. */
  217. function wc_cart_totals_coupon_html( $coupon ) {
  218. if ( is_string( $coupon ) ) {
  219. $coupon = new WC_Coupon( $coupon );
  220. }
  221. $value = array();
  222. if ( $amount = WC()->cart->get_coupon_discount_amount( $coupon->code, WC()->cart->display_cart_ex_tax ) ) {
  223. $discount_html = '-' . wc_price( $amount );
  224. } else {
  225. $discount_html = '';
  226. }
  227. $value[] = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_html, $coupon );
  228. if ( $coupon->enable_free_shipping() ) {
  229. $value[] = __( 'Free shipping coupon', 'woocommerce' );
  230. }
  231. // get rid of empty array elements
  232. $value = array_filter( $value );
  233. $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>';
  234. echo apply_filters( 'woocommerce_cart_totals_coupon_html', $value, $coupon );
  235. }
  236. /**
  237. * Get order total html including inc tax if needed.
  238. *
  239. * @access public
  240. */
  241. function wc_cart_totals_order_total_html() {
  242. $value = '<strong>' . WC()->cart->get_total() . '</strong> ';
  243. // If prices are tax inclusive, show taxes here
  244. if ( wc_tax_enabled() && WC()->cart->tax_display_cart == 'incl' ) {
  245. $tax_string_array = array();
  246. if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
  247. foreach ( WC()->cart->get_tax_totals() as $code => $tax )
  248. $tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
  249. } else {
  250. $tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
  251. }
  252. if ( ! empty( $tax_string_array ) ) {
  253. $taxable_address = WC()->customer->get_taxable_address();
  254. $estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
  255. ? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
  256. : '';
  257. $value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
  258. }
  259. }
  260. echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value );
  261. }
  262. /**
  263. * Get the fee value.
  264. *
  265. * @param object $fee
  266. */
  267. function wc_cart_totals_fee_html( $fee ) {
  268. $cart_totals_fee_html = ( 'excl' == WC()->cart->tax_display_cart ) ? wc_price( $fee->amount ) : wc_price( $fee->amount + $fee->tax );
  269. echo apply_filters( 'woocommerce_cart_totals_fee_html', $cart_totals_fee_html, $fee );
  270. }
  271. /**
  272. * Get a shipping methods full label including price.
  273. * @param WC_Shipping_Rate $method
  274. * @return string
  275. */
  276. function wc_cart_totals_shipping_method_label( $method ) {
  277. $label = $method->get_label();
  278. if ( $method->cost > 0 ) {
  279. if ( WC()->cart->tax_display_cart == 'excl' ) {
  280. $label .= ': ' . wc_price( $method->cost );
  281. if ( $method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax ) {
  282. $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
  283. }
  284. } else {
  285. $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
  286. if ( $method->get_shipping_tax() > 0 && ! WC()->cart->prices_include_tax ) {
  287. $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
  288. }
  289. }
  290. }
  291. return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
  292. }
  293. /**
  294. * Round discount.
  295. *
  296. * @param float $value
  297. * @param int $precision
  298. * @return float
  299. */
  300. function wc_cart_round_discount( $value, $precision ) {
  301. if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
  302. return round( $value, $precision, WC_DISCOUNT_ROUNDING_MODE );
  303. } else {
  304. return round( $value, $precision );
  305. }
  306. }
  307. /**
  308. * Gets chosen shipping method IDs from chosen_shipping_methods session, without instance IDs.
  309. * @since 2.6.2
  310. * @return string[]
  311. */
  312. function wc_get_chosen_shipping_method_ids() {
  313. $method_ids = array();
  314. $chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
  315. foreach ( $chosen_methods as $chosen_method ) {
  316. $chosen_method = explode( ':', $chosen_method );
  317. $method_ids[] = current( $chosen_method );
  318. }
  319. return $method_ids;
  320. }