PickupLocation.php
169 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Shipping; |
| 3 | |
| 4 | use WC_Shipping_Method; |
| 5 | |
| 6 | /** |
| 7 | * Local Pickup Shipping Method. |
| 8 | */ |
| 9 | class PickupLocation extends WC_Shipping_Method { |
| 10 | |
| 11 | /** |
| 12 | * Pickup locations. |
| 13 | * |
| 14 | * @var array |
| 15 | */ |
| 16 | protected $pickup_locations = []; |
| 17 | |
| 18 | /** |
| 19 | * Cost |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $cost = ''; |
| 24 | |
| 25 | /** |
| 26 | * Constructor. |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | parent::__construct(); |
| 30 | $this->id = 'pickup_location'; |
| 31 | $this->method_title = __( 'Local pickup', 'woocommerce' ); |
| 32 | $this->method_description = __( 'Allow customers to choose a local pickup location during checkout.', 'woocommerce' ); |
| 33 | $this->init(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Init function. |
| 38 | */ |
| 39 | public function init() { |
| 40 | $this->enabled = $this->get_option( 'enabled' ); |
| 41 | $this->title = $this->get_option( 'title', __( 'Pickup', 'woocommerce' ) ); |
| 42 | $this->tax_status = $this->get_option( 'tax_status' ); |
| 43 | $this->cost = $this->get_option( 'cost' ); |
| 44 | $this->supports = [ 'settings', 'local-pickup' ]; |
| 45 | $this->pickup_locations = get_option( $this->id . '_pickup_locations', [] ); |
| 46 | add_filter( 'woocommerce_attribute_label', array( $this, 'translate_meta_data' ), 10, 3 ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Checks if a given address is complete. |
| 51 | * |
| 52 | * @param array $address Address. |
| 53 | * @return bool |
| 54 | */ |
| 55 | protected function has_valid_pickup_location( $address ) { |
| 56 | // Normalize address. |
| 57 | $address_fields = wp_parse_args( |
| 58 | (array) $address, |
| 59 | array( |
| 60 | 'city' => '', |
| 61 | 'postcode' => '', |
| 62 | 'state' => '', |
| 63 | 'country' => '', |
| 64 | ) |
| 65 | ); |
| 66 | |
| 67 | // Country is always required. |
| 68 | if ( empty( $address_fields['country'] ) ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // If all fields are provided, we can skip further checks. |
| 73 | if ( ! empty( $address_fields['city'] ) && ! empty( $address_fields['postcode'] ) && ! empty( $address_fields['state'] ) ) { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | // Check validity based on requirements for the country. |
| 78 | $country_address_fields = wc()->countries->get_address_fields( $address_fields['country'], 'shipping_' ); |
| 79 | |
| 80 | foreach ( $country_address_fields as $field_name => $field ) { |
| 81 | $key = str_replace( 'shipping_', '', $field_name ); |
| 82 | |
| 83 | if ( isset( $address_fields[ $key ] ) && true === $field['required'] && empty( $address_fields[ $key ] ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Calculate shipping. |
| 93 | * |
| 94 | * @param array $package Package information. |
| 95 | */ |
| 96 | public function calculate_shipping( $package = array() ) { |
| 97 | if ( $this->pickup_locations ) { |
| 98 | foreach ( $this->pickup_locations as $index => $location ) { |
| 99 | if ( ! $location['enabled'] ) { |
| 100 | continue; |
| 101 | } |
| 102 | $this->add_rate( |
| 103 | array( |
| 104 | 'id' => $this->id . ':' . $index, |
| 105 | // This is the label shown in shipping rate/method context e.g. London (Local Pickup). |
| 106 | 'label' => wp_kses_post( $this->title . ' (' . $location['name'] . ')' ), |
| 107 | 'package' => $package, |
| 108 | 'cost' => $this->cost, |
| 109 | 'meta_data' => array( |
| 110 | 'pickup_location' => wp_kses_post( $location['name'] ), |
| 111 | 'pickup_address' => $this->has_valid_pickup_location( $location['address'] ) ? wc()->countries->get_formatted_address( $location['address'], ', ' ) : '', |
| 112 | 'pickup_details' => wp_kses_post( $location['details'] ), |
| 113 | ), |
| 114 | ) |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * See if the method is available. |
| 122 | * |
| 123 | * @param array $package Package information. |
| 124 | * @return bool |
| 125 | */ |
| 126 | public function is_available( $package ) { |
| 127 | // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment |
| 128 | return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', 'yes' === $this->enabled, $package, $this ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Translates meta data for the shipping method. |
| 133 | * |
| 134 | * @param string $label Meta label. |
| 135 | * @param string $name Meta key. |
| 136 | * @param mixed $product Product if applicable. |
| 137 | * @return string |
| 138 | */ |
| 139 | public function translate_meta_data( $label, $name, $product ) { |
| 140 | if ( $product ) { |
| 141 | return $label; |
| 142 | } |
| 143 | switch ( $name ) { |
| 144 | case 'pickup_location': |
| 145 | return __( 'Pickup location', 'woocommerce' ); |
| 146 | case 'pickup_address': |
| 147 | return __( 'Pickup address', 'woocommerce' ); |
| 148 | case 'pickup_details': |
| 149 | return __( 'Pickup details', 'woocommerce' ); |
| 150 | } |
| 151 | return $label; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Admin options screen. |
| 156 | * |
| 157 | * See also WC_Shipping_Method::admin_options(). |
| 158 | */ |
| 159 | public function admin_options() { |
| 160 | global $hide_save_button; |
| 161 | $hide_save_button = true; |
| 162 | |
| 163 | wp_enqueue_script( 'wc-shipping-method-pickup-location' ); |
| 164 | |
| 165 | echo '<h2>' . esc_html__( 'Local pickup', 'woocommerce' ) . '</h2>'; |
| 166 | echo '<div class="wrap"><div id="wc-shipping-method-pickup-location-settings-container"></div></div>'; |
| 167 | } |
| 168 | } |
| 169 |