| 1 |
<?php |
| 2 |
namespace Sellkit\Blocks\Inner_Block; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
use Sellkit\Blocks\Helpers\Checkout\Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Form Shipping class. |
| 10 |
* |
| 11 |
* @since 2.3.0 |
| 12 |
* @package Sellkit\Blocks\Inner_Block |
| 13 |
*/ |
| 14 |
class Checkout_Form_Shipping { |
| 15 |
/** |
| 16 |
* Register block meta. |
| 17 |
* |
| 18 |
* @since 2.3.0 |
| 19 |
*/ |
| 20 |
public function register_block_meta() { |
| 21 |
register_block_type_from_metadata( |
| 22 |
__DIR__ |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Print shipping field in frontend using our customized fields. |
| 28 |
* |
| 29 |
* @param array $fields : shipping fields. |
| 30 |
* @param object $checkout : checkout object. |
| 31 |
* @param array $attributes Block attributes. |
| 32 |
* @since 2.3.0 |
| 33 |
* |
| 34 |
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 35 |
*/ |
| 36 |
public function sellkit_checkout_shipping_field( $fields, $checkout, $attributes = [] ) { |
| 37 |
$default_text = [ |
| 38 |
'shipping_first_name', |
| 39 |
'shipping_last_name', |
| 40 |
'shipping_company', |
| 41 |
'shipping_address_1', |
| 42 |
'shipping_address_2', |
| 43 |
'shipping_postcode', |
| 44 |
'shipping_city', |
| 45 |
]; |
| 46 |
|
| 47 |
$default_select = [ |
| 48 |
'shipping_country', |
| 49 |
'shipping_state', |
| 50 |
]; |
| 51 |
|
| 52 |
$default_tel = [ |
| 53 |
'shipping_phone', |
| 54 |
]; |
| 55 |
|
| 56 |
foreach ( $fields as $key => $details ) { |
| 57 |
if ( in_array( $key, $default_text, true ) ) { |
| 58 |
$fields[ $key ]['type'] = 'text'; |
| 59 |
} |
| 60 |
|
| 61 |
if ( in_array( $key, $default_select, true ) ) { |
| 62 |
$fields[ $key ]['type'] = 'select'; |
| 63 |
} |
| 64 |
|
| 65 |
if ( in_array( $key, $default_tel, true ) ) { |
| 66 |
$fields[ $key ]['type'] = 'tel'; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$block_shipping_fields = $attributes['locations']; |
| 71 |
$default_fields = Helper::assign_settings_per_field( $fields, $block_shipping_fields, 'shipping' ); |
| 72 |
$priority = array_column( $default_fields, 'priority' ); |
| 73 |
|
| 74 |
array_multisort( $priority, SORT_ASC, $default_fields ); |
| 75 |
|
| 76 |
Helper::register_fields(); |
| 77 |
|
| 78 |
foreach ( $default_fields as $key => $details ) { |
| 79 |
if ( ! array_key_exists( 'type', $details ) ) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
$class = $details['type']; |
| 84 |
$class = '\Sellkit\Blocks\Checkout\Fields\\' . ucfirst( $class ); |
| 85 |
$class = new $class(); |
| 86 |
$field = ''; |
| 87 |
|
| 88 |
$field = $class->final_html_structure( $field, $details, $key ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Render form shipping. |
| 94 |
* |
| 95 |
* @param string $content Block content. |
| 96 |
* @param array $attributes Block attributes. |
| 97 |
* @since 2.3.0 |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function checkout_form_shipping( $content, $attributes ) { |
| 101 |
add_action( 'sellkit_block_checkout_shipping_fields', [ $this, 'sellkit_checkout_shipping_field' ], 10, 3 ); |
| 102 |
|
| 103 |
global $woocommerce; |
| 104 |
|
| 105 |
$checkout = $woocommerce->checkout; |
| 106 |
|
| 107 |
$show_title = apply_filters( 'sellkit-checkout-disable-shipping-fields-title', true ); |
| 108 |
|
| 109 |
ob_start(); |
| 110 |
|
| 111 |
if ( ! empty( WC()->cart ) && WC()->cart->needs_shipping() ) : ?> |
| 112 |
<div class="woocommerce-shipping-fields sellkit-one-page-checkout-shipping sellkit-checkout-local-fields <?php echo esc_attr( $attributes['customClassName'] ); ?>"> |
| 113 |
<div class="shipping_address"> |
| 114 |
<?php if ( true === $show_title ) : ?> |
| 115 |
<div id="shipping_text_title" class="header heading" style="width:100%"> |
| 116 |
<?php echo esc_html( $attributes['shippingHeadingText'] ); ?> |
| 117 |
</div> |
| 118 |
<?php endif; ?> |
| 119 |
|
| 120 |
<?php do_action( 'woocommerce_before_checkout_shipping_form', $checkout ); ?> |
| 121 |
|
| 122 |
<div class="woocommerce-shipping-fields__field-wrapper" id="sellkit-checkout-widget-shipping-fields"> |
| 123 |
<?php |
| 124 |
$fields = $checkout->get_checkout_fields( 'shipping' ); |
| 125 |
|
| 126 |
$this->sellkit_checkout_shipping_field( $fields, $checkout, $attributes ); |
| 127 |
?> |
| 128 |
</div> |
| 129 |
|
| 130 |
<?php do_action( 'woocommerce_after_checkout_shipping_form', $checkout ); ?> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
<?php endif; ?> |
| 134 |
<div class="sellkit-woocommerce-additional-fields"> |
| 135 |
<?php do_action( 'woocommerce_before_order_notes', $checkout ); ?> |
| 136 |
<?php do_action( 'woocommerce_after_order_notes', $checkout ); ?> |
| 137 |
</div> |
| 138 |
<?php do_action( 'sellkit_block_checkout_after_shipping_section' ); |
| 139 |
?> |
| 140 |
<section class="sellkit-one-page-shipping-methods"> |
| 141 |
<?php if ( ! empty( WC()->cart ) && WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?> |
| 142 |
<?php do_action( 'woocommerce_review_order_before_shipping' ); ?> |
| 143 |
<?php WC()->cart->calculate_totals(); ?> |
| 144 |
<?php do_action( 'woocommerce_review_order_after_shipping' ); ?> |
| 145 |
<?php endif; ?> |
| 146 |
</section> |
| 147 |
<?php |
| 148 |
$final_content = ob_get_clean(); |
| 149 |
|
| 150 |
return str_replace( '</div>', $final_content . '</div>', $content ); |
| 151 |
} |
| 152 |
} |
| 153 |
|