class-wc-shortcode-cart.php
3 months ago
class-wc-shortcode-checkout.php
3 months ago
class-wc-shortcode-my-account.php
1 month ago
class-wc-shortcode-order-tracking.php
3 years ago
class-wc-shortcode-products.php
3 months ago
class-wc-shortcode-cart.php
105 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Cart Shortcode |
| 4 | * |
| 5 | * Used on the cart page, the cart shortcode displays the cart contents and interface for coupon codes and other cart bits and pieces. |
| 6 | * |
| 7 | * @package WooCommerce\Shortcodes\Cart |
| 8 | * @version 2.3.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Shortcode cart class. |
| 15 | */ |
| 16 | class WC_Shortcode_Cart { |
| 17 | |
| 18 | /** |
| 19 | * Calculate shipping for the cart. |
| 20 | * |
| 21 | * @throws Exception When some data is invalid. |
| 22 | */ |
| 23 | public static function calculate_shipping() { |
| 24 | try { |
| 25 | WC()->shipping()->reset_shipping(); |
| 26 | |
| 27 | $address = array(); |
| 28 | |
| 29 | $address['country'] = isset( $_POST['calc_shipping_country'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_country'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 30 | $address['state'] = isset( $_POST['calc_shipping_state'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_state'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 31 | $address['postcode'] = isset( $_POST['calc_shipping_postcode'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_postcode'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 32 | $address['city'] = isset( $_POST['calc_shipping_city'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_city'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 33 | |
| 34 | if ( $address['postcode'] ) { |
| 35 | $address['postcode'] = wc_format_postcode( $address['postcode'], $address['country'] ); |
| 36 | } |
| 37 | |
| 38 | $address = apply_filters( 'woocommerce_cart_calculate_shipping_address', $address ); |
| 39 | |
| 40 | if ( $address['postcode'] && ! WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ) { |
| 41 | throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ) ); |
| 42 | } |
| 43 | |
| 44 | if ( $address['country'] ) { |
| 45 | if ( ! WC()->customer->get_billing_first_name() ) { |
| 46 | WC()->customer->set_billing_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
| 47 | } |
| 48 | WC()->customer->set_shipping_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
| 49 | } else { |
| 50 | WC()->customer->set_billing_address_to_base(); |
| 51 | WC()->customer->set_shipping_address_to_base(); |
| 52 | } |
| 53 | |
| 54 | WC()->customer->set_calculated_shipping( true ); |
| 55 | WC()->customer->save(); |
| 56 | |
| 57 | wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' ); |
| 58 | |
| 59 | do_action( 'woocommerce_calculated_shipping' ); |
| 60 | |
| 61 | } catch ( Exception $e ) { |
| 62 | if ( ! empty( $e ) ) { |
| 63 | wc_add_notice( $e->getMessage(), 'error' ); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Output the cart shortcode. |
| 70 | * |
| 71 | * @param array $atts Shortcode attributes. |
| 72 | */ |
| 73 | public static function output( $atts ) { |
| 74 | if ( ! apply_filters( 'woocommerce_output_cart_shortcode_content', true ) ) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // Constants. |
| 79 | wc_maybe_define_constant( 'WOOCOMMERCE_CART', true ); |
| 80 | |
| 81 | $atts = shortcode_atts( array(), $atts, 'woocommerce_cart' ); |
| 82 | $nonce_value = wc_get_var( $_REQUEST['woocommerce-shipping-calculator-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine. |
| 83 | |
| 84 | // Update Shipping. Nonce check uses new value and old value (woocommerce-cart). @todo remove in 4.0. |
| 85 | if ( ! empty( $_POST['calc_shipping'] ) && ( wp_verify_nonce( $nonce_value, 'woocommerce-shipping-calculator' ) || wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) ) { // WPCS: input var ok. |
| 86 | self::calculate_shipping(); |
| 87 | |
| 88 | // Also calc totals before we check items so subtotals etc are up to date. |
| 89 | WC()->cart->calculate_totals(); |
| 90 | } |
| 91 | |
| 92 | // Check cart items are valid. |
| 93 | do_action( 'woocommerce_check_cart_items' ); |
| 94 | |
| 95 | // Calc totals. |
| 96 | WC()->cart->calculate_totals(); |
| 97 | |
| 98 | if ( WC()->cart->is_empty() ) { |
| 99 | wc_get_template( 'cart/cart-empty.php' ); |
| 100 | } else { |
| 101 | wc_get_template( 'cart/cart.php' ); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 |