← All changes
|
includes/elementor/modules/checkout/widgets/checkout.php
+104
-13
1.5.0
→
2.7.0
View file →
| @@ -4,12 +4,14 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Sellkit\Elementor\Modules\Checkout\Widgets\{ Settings_Controls, Style_Controls }; |
| 6 | 6 | use Sellkit\Elementor\Modules\Checkout\Classes\{Global_Hooks, Local_Hooks, Multi_Step, Helper }; |
| 7 | 7 | use Elementor\Plugin as Elementor; |
| 8 | +use Sellkit\Global_Checkout\Checkout; | |
| 8 | 9 | |
| 9 | 10 | /** |
| 10 | 11 | * Checkout class. |
| 11 | 12 | * |
| 13 | + * @SuppressWarnings(PHPMD.NPathComplexity) | |
| 12 | 14 | * @since 1.1.0 |
| 13 | 15 | */ |
| 14 | 16 | class Sellkit_Elementor_Checkout_Widget extends Sellkit_Elementor_Base_Widget { |
| 15 | 17 | |
| @@ -68,28 +70,123 @@ | ||
| 68 | 70 | new Local_Hooks( $settings, $this ); |
| 69 | 71 | } |
| 70 | 72 | |
| 71 | 73 | protected function register_controls() { |
| 72 | - require __DIR__ . '/settings-controls.php'; | |
| 73 | - require __DIR__ . '/style-controls.php'; | |
| 74 | + require_once __DIR__ . '/settings-controls.php'; | |
| 75 | + require_once __DIR__ . '/style-controls.php'; | |
| 74 | 76 | |
| 75 | 77 | new Settings_Controls(); |
| 76 | 78 | new Style_Controls(); |
| 77 | 79 | } |
| 78 | 80 | |
| 81 | + /** | |
| 82 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) | |
| 83 | + */ | |
| 79 | 84 | protected function render() { |
| 85 | + if ( ! function_exists( 'wc' ) ) { | |
| 86 | + return; | |
| 87 | + } | |
| 88 | + | |
| 89 | + $settings = $this->get_settings_for_display(); | |
| 90 | + $checkout_id = get_the_ID(); | |
| 91 | + $step_data = get_post_meta( $checkout_id, 'step_data', true ); | |
| 92 | + $global_checkout = apply_filters( 'sellkit_global_checkout_activated', false ); | |
| 93 | + $global_checkout_id = get_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ); | |
| 94 | + $current_funnel_id = ( ! empty( $step_data ) ) ? $step_data['funnel_id'] : -1; | |
| 95 | + $empty_cart_msg = $settings['empty_cart']; // phpcs:ignore | |
| 96 | + | |
| 97 | + $order_key = ! empty( sellkit_htmlspecialchars( INPUT_GET, 'order-key' ) ) ? sellkit_htmlspecialchars( INPUT_GET, 'order-key' ) : sellkit_htmlspecialchars( INPUT_GET, 'key' ); | |
| 98 | + | |
| 99 | + if ( | |
| 100 | + $global_checkout && | |
| 101 | + is_wc_endpoint_url( 'order-received' ) && | |
| 102 | + ! empty( $order_key ) | |
| 103 | + ) { | |
| 104 | + $order_id = wc_get_order_id_by_order_key( $order_key ); | |
| 105 | + | |
| 106 | + if ( empty( $order_id ) ) { | |
| 107 | + /* translators: %s: Empty cart message. */ | |
| 108 | + echo sprintf( | |
| 109 | + '<div class="elementor-alert elementor-alert-info sellkit-checkout-empty-cart-message-box">%s</div>', | |
| 110 | + esc_html( $empty_cart_msg ) | |
| 111 | + ); | |
| 112 | + | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + | |
| 116 | + // Valid order on the order-received endpoint: render WooCommerce's thank-you | |
| 117 | + // template instead of the checkout form. SellKit's redirect_after_purchase | |
| 118 | + // already runs earlier on template_redirect, but express-checkout flows that | |
| 119 | + // skip SellKit's hidden fields (so no sellkit_funnel_next_step_data meta is | |
| 120 | + // saved) fall through to here and must not show a filled-in checkout form. | |
| 121 | + echo '<div class="woocommerce">'; | |
| 122 | + wc_get_template( 'checkout/thankyou.php', [ 'order' => wc_get_order( $order_id ) ] ); | |
| 123 | + echo '</div>'; | |
| 124 | + | |
| 125 | + return; | |
| 126 | + } | |
| 127 | + | |
| 128 | + if ( | |
| 129 | + $global_checkout && | |
| 130 | + 0 === WC()->cart->get_cart_contents_count() && | |
| 131 | + ! is_wc_endpoint_url( 'order-received' ) | |
| 132 | + ) { | |
| 133 | + /* translators: %s: Empty cart message. */ | |
| 134 | + echo sprintf( | |
| 135 | + '<div class="elementor-alert elementor-alert-info sellkit-checkout-empty-cart-message-box">%s</div>', | |
| 136 | + esc_html( $empty_cart_msg ) | |
| 137 | + ); | |
| 138 | + | |
| 139 | + return; | |
| 140 | + } | |
| 141 | + | |
| 142 | + if ( | |
| 143 | + false === $global_checkout && | |
| 144 | + intval( $global_checkout_id ) !== intval( $current_funnel_id ) && | |
| 145 | + is_array( $step_data ) && | |
| 146 | + ( | |
| 147 | + ! array_key_exists( 'data', $step_data ) || | |
| 148 | + ! array_key_exists( 'list', $step_data['data']['products'] ) | |
| 149 | + ) | |
| 150 | + ) { | |
| 151 | + /* translators: %s: Empty cart message. */ | |
| 152 | + echo sprintf( | |
| 153 | + '<div class="elementor-alert elementor-alert-info sellkit-checkout-empty-cart-message-box">%s</div>', | |
| 154 | + esc_html( $empty_cart_msg ) | |
| 155 | + ); | |
| 156 | + return; | |
| 157 | + } | |
| 158 | + | |
| 80 | 159 | add_filter( 'woocommerce_is_checkout', function() { |
| 81 | 160 | return true; |
| 82 | 161 | }, 10 ); |
| 83 | 162 | |
| 84 | - $settings = $this->get_settings_for_display(); | |
| 163 | + $this->clear_sellkit_checkout_custom_hooks( $settings ); | |
| 85 | 164 | |
| 86 | - $this->clear_sellkit_checkout_custom_hooks( $settings ); | |
| 165 | + $shipping_destination = get_option( 'woocommerce_ship_to_destination', true ); | |
| 87 | 166 | |
| 88 | 167 | if ( 'multi-step' === $settings['layout-type'] ) { |
| 89 | 168 | $this->apply_multi_step_design( $settings ); |
| 169 | + | |
| 170 | + remove_action( 'woocommerce_checkout_billing', [ WC()->checkout(), 'checkout_form_billing' ] ); | |
| 171 | + | |
| 172 | + if ( 'billing_only' === $shipping_destination ) { | |
| 173 | + remove_action( 'woocommerce_checkout_shipping', [ WC()->checkout(), 'checkout_form_shipping' ] ); | |
| 174 | + add_action( 'woocommerce_checkout_shipping', [ WC()->checkout(), 'checkout_form_billing' ], 10 ); | |
| 175 | + } | |
| 90 | 176 | } |
| 91 | 177 | |
| 178 | + if ( 'one-page' === $settings['layout-type'] ) { | |
| 179 | + remove_action( 'woocommerce_checkout_billing', [ WC()->checkout(), 'checkout_form_billing' ] ); | |
| 180 | + | |
| 181 | + if ( 'billing_only' === $shipping_destination ) { | |
| 182 | + remove_action( 'woocommerce_checkout_shipping', [ WC()->checkout(), 'checkout_form_shipping' ] ); | |
| 183 | + add_action( 'woocommerce_checkout_shipping', [ WC()->checkout(), 'checkout_form_billing' ], 10 ); | |
| 184 | + } else { | |
| 185 | + add_action( 'woocommerce_checkout_order_review', [ WC()->checkout(), 'checkout_form_billing' ], 10 ); | |
| 186 | + } | |
| 187 | + } | |
| 188 | + | |
| 92 | 189 | $this->apply_local_hooks( $settings ); |
| 93 | 190 | |
| 94 | 191 | if ( 'one-page' === $settings['layout-type'] ) { |
| 95 | 192 | $this->add_render_attribute( |
| @@ -106,12 +203,11 @@ | ||
| 106 | 203 | } |
| 107 | 204 | |
| 108 | 205 | if ( Elementor::$instance->editor->is_edit_mode() ) { |
| 109 | 206 | $current_user = wp_get_current_user()->ID; |
| 110 | - wp_set_current_user( 0 ); | |
| 111 | 207 | } |
| 112 | 208 | |
| 113 | - echo '<div ' . $this->get_render_attribute_string( 'wrapper' ) . ' >'; | |
| 209 | + echo '<div ' . $this->get_render_attribute_string( 'wrapper' ) . ' >'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor-generated attributes. | |
| 114 | 210 | echo do_shortcode( '[woocommerce_checkout]' ); |
| 115 | 211 | echo do_shortcode( '[sellkit_checkout_widget_simulated]' ); |
| 116 | 212 | echo '</div>'; |
| 117 | 213 | |
| @@ -145,18 +241,13 @@ | ||
| 145 | 241 | remove_all_actions( 'sellkit-checkout-multistep-sidebar-begins' ); |
| 146 | 242 | remove_all_actions( 'sellkit-checkout-multistep-sidebar-ends' ); |
| 147 | 243 | remove_all_actions( 'sellkit-checkout-widget-breadcrumb-desktop' ); |
| 148 | 244 | remove_all_actions( 'sellkit-checkout-widget-breadcrumb-mobile' ); |
| 149 | - | |
| 150 | 245 | remove_all_actions( 'sellkit_checkout_shipping_fields' ); |
| 151 | 246 | remove_all_actions( 'sellkit_checkout_billing_fields' ); |
| 152 | - | |
| 153 | 247 | remove_all_actions( 'sellkit_checkout_after_shipping_section' ); |
| 154 | - | |
| 155 | 248 | remove_all_actions( 'sellkit_checkout_one_page_express_methods' ); |
| 156 | - | |
| 157 | 249 | remove_all_actions( 'sellkit-checkout-widget-custom-coupon-form' ); |
| 158 | - | |
| 159 | 250 | remove_all_filters( 'woocommerce_locate_template' ); |
| 160 | 251 | |
| 161 | 252 | // !TODO We can use this method to move / remove billing and shipping fields. |
| 162 | 253 | add_action( 'woocommerce_checkout_shipping', [ WC()->checkout(), 'checkout_form_shipping' ] ); // ! added for theme integration. Astra. |
| @@ -188,9 +279,9 @@ | ||
| 188 | 279 | |
| 189 | 280 | if ( 'yes' !== $settings['show_cart_edit'] ) { |
| 190 | 281 | $this->hide_items_in_editor( 'sellkit-one-page-checkout-product-qty' ); |
| 191 | 282 | |
| 192 | - add_action( 'sellkit-checkout-widget-disable-quantity', [ Helper::instance(), 'checkout_order_hidden_quantity' ], 10, 2 ); | |
| 283 | + add_filter( 'sellkit-checkout-widget-disable-quantity', [ Helper::instance(), 'checkout_order_hidden_quantity' ], 10, 2 ); | |
| 193 | 284 | } |
| 194 | 285 | |
| 195 | 286 | if ( 'yes' !== $settings['show_shipping_method'] ) { |
| 196 | 287 | $this->hide_items_in_editor( 'sellkit-one-page-shipping-methods' ); |
| @@ -212,8 +303,8 @@ | ||
| 212 | 303 | private function hide_items_in_editor( $class ) { |
| 213 | 304 | echo sprintf( |
| 214 | 305 | /** Translators : %s : class name */ |
| 215 | 306 | '<style>.%s { display:none !important; }</style>', |
| 216 | - $class | |
| 307 | + esc_attr( $class ) | |
| 217 | 308 | ); |
| 218 | 309 | } |
| 219 | 310 | } |