| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit\Elementor\Modules\Checkout\Widgets\{ Settings_Controls, Style_Controls }; |
| 6 |
use Sellkit\Elementor\Modules\Checkout\Classes\{Global_Hooks, Local_Hooks, Multi_Step, Helper }; |
| 7 |
use Elementor\Plugin as Elementor; |
| 8 |
|
| 9 |
/** |
| 10 |
* Checkout class. |
| 11 |
* |
| 12 |
* @since 1.1.0 |
| 13 |
*/ |
| 14 |
class Sellkit_Elementor_Checkout_Widget extends Sellkit_Elementor_Base_Widget { |
| 15 |
|
| 16 |
public function __construct( $data = [], $args = null ) { |
| 17 |
parent::__construct( $data, $args ); |
| 18 |
|
| 19 |
add_action( 'elementor/frontend/after_enqueue_scripts', [ $this, 'frontend_enqueue_scripts' ], 10 ); |
| 20 |
add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'editor_enqueue_scripts' ], 10 ); |
| 21 |
} |
| 22 |
|
| 23 |
public function get_name() { |
| 24 |
return 'sellkit-checkout'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_title() { |
| 28 |
return __( 'Checkout Form', 'sellkit' ); |
| 29 |
} |
| 30 |
|
| 31 |
public function get_icon() { |
| 32 |
return 'sellkit-element-icon sellkit-checkout-icon'; |
| 33 |
} |
| 34 |
|
| 35 |
public function frontend_enqueue_scripts() { |
| 36 |
wp_enqueue_script( 'wc-checkout' ); |
| 37 |
} |
| 38 |
|
| 39 |
public function editor_enqueue_scripts() { |
| 40 |
wp_enqueue_script( 'wc-checkout' ); |
| 41 |
} |
| 42 |
|
| 43 |
public function is_reload_preview_required() { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
public function get_style_depends() { |
| 48 |
return [ 'font-awesome' ]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Apply multi-step functionality. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
* @since 1.1.0 |
| 56 |
*/ |
| 57 |
private function apply_multi_step_design( $settings ) { |
| 58 |
new Multi_Step( $settings ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Apply local hooks. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
* @since 1.1.0 |
| 66 |
*/ |
| 67 |
private function apply_local_hooks( $settings ) { |
| 68 |
new Local_Hooks( $settings, $this ); |
| 69 |
} |
| 70 |
|
| 71 |
protected function register_controls() { |
| 72 |
require __DIR__ . '/settings-controls.php'; |
| 73 |
require __DIR__ . '/style-controls.php'; |
| 74 |
|
| 75 |
new Settings_Controls(); |
| 76 |
new Style_Controls(); |
| 77 |
} |
| 78 |
|
| 79 |
protected function render() { |
| 80 |
add_filter( 'woocommerce_is_checkout', function() { |
| 81 |
return true; |
| 82 |
}, 10 ); |
| 83 |
|
| 84 |
$settings = $this->get_settings_for_display(); |
| 85 |
|
| 86 |
$this->clear_sellkit_checkout_custom_hooks( $settings ); |
| 87 |
|
| 88 |
if ( 'multi-step' === $settings['layout-type'] ) { |
| 89 |
$this->apply_multi_step_design( $settings ); |
| 90 |
} |
| 91 |
|
| 92 |
$this->apply_local_hooks( $settings ); |
| 93 |
|
| 94 |
if ( 'one-page' === $settings['layout-type'] ) { |
| 95 |
$this->add_render_attribute( |
| 96 |
'wrapper', |
| 97 |
'class', |
| 98 |
'sellkit-checkout-widget-one-page-build sellkit-checkout-widget-main-wrapper' |
| 99 |
); |
| 100 |
} else { |
| 101 |
$this->add_render_attribute( |
| 102 |
'wrapper', |
| 103 |
'class', |
| 104 |
'sellkit-checkout-widget-multi-page-build sellkit-checkout-widget-main-wrapper' |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
if ( Elementor::$instance->editor->is_edit_mode() ) { |
| 109 |
$current_user = wp_get_current_user()->ID; |
| 110 |
wp_set_current_user( 0 ); |
| 111 |
} |
| 112 |
|
| 113 |
echo '<div ' . $this->get_render_attribute_string( 'wrapper' ) . ' >'; |
| 114 |
echo do_shortcode( '[woocommerce_checkout]' ); |
| 115 |
echo do_shortcode( '[sellkit_checkout_widget_simulated]' ); |
| 116 |
echo '</div>'; |
| 117 |
|
| 118 |
do_action( 'sellkit-after-checkout-content', $settings ); |
| 119 |
|
| 120 |
if ( Elementor::$instance->editor->is_edit_mode() ) { |
| 121 |
wp_set_current_user( $current_user ); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Remove every function attached to sellkit checkout custom hooks. |
| 128 |
* |
| 129 |
* @param array $settings widget settings. |
| 130 |
* @since 1.1.0 |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
private function clear_sellkit_checkout_custom_hooks( $settings ) { |
| 134 |
// Removed function that attached to custom hooks to prevent any error. |
| 135 |
remove_all_actions( 'sellkit-checkout-cart-item' ); |
| 136 |
remove_all_actions( 'sellkit-checkout-step-a-begins' ); |
| 137 |
remove_all_actions( 'sellkit-checkout-step-a-ends' ); |
| 138 |
remove_all_actions( 'sellkit-checkout-step-b-begins' ); |
| 139 |
remove_all_actions( 'sellkit-checkout-step-b-ends' ); |
| 140 |
remove_all_actions( 'sellkit-checkout-widget-step-two-header' ); |
| 141 |
remove_all_actions( 'sellkit-checkout-step-c-begins' ); |
| 142 |
remove_all_actions( 'sellkit-checkout-multistep-third-step-back-btn' ); |
| 143 |
remove_all_actions( 'sellkit-checkout-step-c-ends' ); |
| 144 |
remove_all_actions( 'sellkit-checkout-widget-step-three-header' ); |
| 145 |
remove_all_actions( 'sellkit-checkout-multistep-sidebar-begins' ); |
| 146 |
remove_all_actions( 'sellkit-checkout-multistep-sidebar-ends' ); |
| 147 |
remove_all_actions( 'sellkit-checkout-widget-breadcrumb-desktop' ); |
| 148 |
remove_all_actions( 'sellkit-checkout-widget-breadcrumb-mobile' ); |
| 149 |
|
| 150 |
remove_all_actions( 'sellkit_checkout_shipping_fields' ); |
| 151 |
remove_all_actions( 'sellkit_checkout_billing_fields' ); |
| 152 |
|
| 153 |
remove_all_actions( 'sellkit_checkout_after_shipping_section' ); |
| 154 |
|
| 155 |
remove_all_actions( 'sellkit_checkout_one_page_express_methods' ); |
| 156 |
|
| 157 |
remove_all_actions( 'sellkit-checkout-widget-custom-coupon-form' ); |
| 158 |
|
| 159 |
remove_all_filters( 'woocommerce_locate_template' ); |
| 160 |
|
| 161 |
// !TODO We can use this method to move / remove billing and shipping fields. |
| 162 |
add_action( 'woocommerce_checkout_shipping', [ WC()->checkout(), 'checkout_form_shipping' ] ); // ! added for theme integration. Astra. |
| 163 |
remove_action( 'woocommerce_checkout_billing', [ WC()->checkout(), 'checkout_form_shipping' ] ); // ! added for theme integration. Astra. |
| 164 |
|
| 165 |
if ( Elementor::$instance->editor->is_edit_mode() ) { |
| 166 |
$this->editor_mode( $settings ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Editor mode, Render widget content on clicking it's wrapper. |
| 172 |
* |
| 173 |
* @since 1.1.0 |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
private function editor_mode( $settings ) { |
| 177 |
add_action( 'sellkit_checkout_required_hidden_fields', function() { |
| 178 |
echo '<input type="hidden" name="sellkit-elementor-editor-mode" value="true" >'; |
| 179 |
} ); |
| 180 |
|
| 181 |
// Some frontend Hooks doesn't work in editor mode. |
| 182 |
// So we just hide them in editor mode, BUT we use real hooks in frontend. |
| 183 |
// Purpose is to increase user experience in editor mode. |
| 184 |
|
| 185 |
if ( 'yes' !== $settings['show_cart_items'] ) { |
| 186 |
$this->hide_items_in_editor( 'cart_item' ); |
| 187 |
} |
| 188 |
|
| 189 |
if ( 'yes' !== $settings['show_cart_edit'] ) { |
| 190 |
$this->hide_items_in_editor( 'sellkit-one-page-checkout-product-qty' ); |
| 191 |
|
| 192 |
add_action( 'sellkit-checkout-widget-disable-quantity', [ Helper::instance(), 'checkout_order_hidden_quantity' ], 10, 2 ); |
| 193 |
} |
| 194 |
|
| 195 |
if ( 'yes' !== $settings['show_shipping_method'] ) { |
| 196 |
$this->hide_items_in_editor( 'sellkit-one-page-shipping-methods' ); |
| 197 |
} |
| 198 |
|
| 199 |
add_action( 'sellkit-checkout-widget-custom-coupon-form', function() use ( $settings ) { |
| 200 |
Local_Hooks::coupon_form( $settings ); |
| 201 |
} ); |
| 202 |
|
| 203 |
if ( 'yes' !== $settings['show_coupon_field'] ) { |
| 204 |
$this->hide_items_in_editor( 'coupon-form' ); |
| 205 |
} |
| 206 |
|
| 207 |
if ( 'yes' !== $settings['order_summary_show_image'] ) { |
| 208 |
$this->hide_items_in_editor( 'sellkit-checkout-widget-item-image' ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
private function hide_items_in_editor( $class ) { |
| 213 |
echo sprintf( |
| 214 |
/** Translators : %s : class name */ |
| 215 |
'<style>.%s { display:none !important; }</style>', |
| 216 |
$class |
| 217 |
); |
| 218 |
} |
| 219 |
} |
| 220 |
|