Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/StoreApi/Utilitie...
File: LocalPickupUtils.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\StoreApi\Utilities;
[1] Fix | Delete
[2] Fix | Delete
/**
[3] Fix | Delete
* Util class for local pickup related functionality, this contains methods that need to be accessed from places besides
[4] Fix | Delete
* the ShippingController, i.e. the OrderController.
[5] Fix | Delete
*/
[6] Fix | Delete
class LocalPickupUtils {
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Gets the local pickup location settings.
[10] Fix | Delete
*
[11] Fix | Delete
* @param string $context The context for the settings. Defaults to 'view'.
[12] Fix | Delete
*/
[13] Fix | Delete
public static function get_local_pickup_settings( $context = 'view' ) {
[14] Fix | Delete
$pickup_location_settings = get_option(
[15] Fix | Delete
'woocommerce_pickup_location_settings',
[16] Fix | Delete
[
[17] Fix | Delete
'enabled' => 'no',
[18] Fix | Delete
'title' => __( 'Pickup', 'woocommerce' ),
[19] Fix | Delete
'cost' => '',
[20] Fix | Delete
'tax_status' => 'taxable',
[21] Fix | Delete
]
[22] Fix | Delete
);
[23] Fix | Delete
[24] Fix | Delete
if ( empty( $pickup_location_settings['title'] ) ) {
[25] Fix | Delete
$pickup_location_settings['title'] = __( 'Pickup', 'woocommerce' );
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
if ( empty( $pickup_location_settings['enabled'] ) ) {
[29] Fix | Delete
$pickup_location_settings['enabled'] = 'no';
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
if ( ! isset( $pickup_location_settings['cost'] ) ) {
[33] Fix | Delete
$pickup_location_settings['cost'] = '';
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
// Return settings as is if we're editing them.
[37] Fix | Delete
if ( 'edit' === $context ) {
[38] Fix | Delete
return $pickup_location_settings;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
// All consumers of this turn it into a bool eventually. Doing it here removes the need for that.
[42] Fix | Delete
$pickup_location_settings['enabled'] = wc_string_to_bool( $pickup_location_settings['enabled'] );
[43] Fix | Delete
$pickup_location_settings['title'] = wc_clean( $pickup_location_settings['title'] );
[44] Fix | Delete
[45] Fix | Delete
return $pickup_location_settings;
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* Checks if WC Blocks local pickup is enabled.
[50] Fix | Delete
*
[51] Fix | Delete
* @return bool True if local pickup is enabled.
[52] Fix | Delete
*/
[53] Fix | Delete
public static function is_local_pickup_enabled() {
[54] Fix | Delete
// Get option directly to avoid early translation function call.
[55] Fix | Delete
// See https://github.com/woocommerce/woocommerce/pull/47113.
[56] Fix | Delete
$pickup_location_settings = get_option(
[57] Fix | Delete
'woocommerce_pickup_location_settings',
[58] Fix | Delete
[
[59] Fix | Delete
'enabled' => 'no',
[60] Fix | Delete
]
[61] Fix | Delete
);
[62] Fix | Delete
[63] Fix | Delete
if ( empty( $pickup_location_settings['enabled'] ) ) {
[64] Fix | Delete
$pickup_location_settings['enabled'] = 'no';
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
return wc_string_to_bool( $pickup_location_settings['enabled'] );
[68] Fix | Delete
}
[69] Fix | Delete
/**
[70] Fix | Delete
* Gets a list of payment method ids that support the 'local-pickup' feature.
[71] Fix | Delete
*
[72] Fix | Delete
* @return string[] List of payment method ids that support the 'local-pickup' feature.
[73] Fix | Delete
*/
[74] Fix | Delete
public static function get_local_pickup_method_ids() {
[75] Fix | Delete
$all_methods_supporting_local_pickup = array_reduce(
[76] Fix | Delete
WC()->shipping()->get_shipping_methods(),
[77] Fix | Delete
function ( $methods, $method ) {
[78] Fix | Delete
if ( $method->supports( 'local-pickup' ) ) {
[79] Fix | Delete
$methods[] = $method->id;
[80] Fix | Delete
}
[81] Fix | Delete
return $methods;
[82] Fix | Delete
},
[83] Fix | Delete
array( 'local_pickup' )
[84] Fix | Delete
);
[85] Fix | Delete
[86] Fix | Delete
// We use array_values because this will be used in JS, so we don't need the (numerical) keys.
[87] Fix | Delete
return array_values(
[88] Fix | Delete
// This array_unique is necessary because WC()->shipping()->get_shipping_methods() can return duplicates.
[89] Fix | Delete
array_unique(
[90] Fix | Delete
$all_methods_supporting_local_pickup
[91] Fix | Delete
)
[92] Fix | Delete
);
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Checks if a method is a local pickup method.
[97] Fix | Delete
*
[98] Fix | Delete
* @param string $method_id The method id to check.
[99] Fix | Delete
* @return bool True if the method is a local pickup method.
[100] Fix | Delete
*/
[101] Fix | Delete
public static function is_local_pickup_method( $method_id ) {
[102] Fix | Delete
return in_array( $method_id, self::get_local_pickup_method_ids(), true );
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Gets local pickup locations for block editor preview, including placeholder
[107] Fix | Delete
* locations for custom shipping methods that support local pickup.
[108] Fix | Delete
*
[109] Fix | Delete
* This method combines the built-in pickup_location locations with placeholder
[110] Fix | Delete
* entries for any other shipping methods that declare 'local-pickup' support.
[111] Fix | Delete
* This allows custom shipping methods to appear in the block editor preview.
[112] Fix | Delete
*
[113] Fix | Delete
* @return array Array of pickup locations with the following structure:
[114] Fix | Delete
* - 'name' (string) The location name.
[115] Fix | Delete
* - 'enabled' (bool) Whether the location is enabled.
[116] Fix | Delete
* - 'address' (array) Address array with keys: address_1, city, state, postcode, country.
[117] Fix | Delete
* - 'details' (string) Additional details about the location.
[118] Fix | Delete
* - 'method_id' (string) The shipping method ID this location belongs to.
[119] Fix | Delete
*
[120] Fix | Delete
* @since 10.5.0
[121] Fix | Delete
*/
[122] Fix | Delete
public static function get_local_pickup_method_locations() {
[123] Fix | Delete
// Get the built-in pickup locations.
[124] Fix | Delete
$builtin_locations = get_option( 'pickup_location_pickup_locations', array() );
[125] Fix | Delete
[126] Fix | Delete
// Add method_id to built-in locations.
[127] Fix | Delete
foreach ( $builtin_locations as $index => $location ) {
[128] Fix | Delete
$builtin_locations[ $index ]['method_id'] = 'pickup_location';
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
// Get all shipping methods that support local-pickup.
[132] Fix | Delete
$shipping_methods = WC()->shipping()->get_shipping_methods();
[133] Fix | Delete
[134] Fix | Delete
// Get store base address for placeholder locations.
[135] Fix | Delete
$base_country = WC()->countries->get_base_country();
[136] Fix | Delete
$base_state = WC()->countries->get_base_state();
[137] Fix | Delete
[138] Fix | Delete
$custom_method_locations = array();
[139] Fix | Delete
[140] Fix | Delete
foreach ( $shipping_methods as $method ) {
[141] Fix | Delete
// Skip if method doesn't support local-pickup.
[142] Fix | Delete
if ( ! $method->supports( 'local-pickup' ) ) {
[143] Fix | Delete
continue;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
// Skip the built-in pickup_location method (already handled above).
[147] Fix | Delete
if ( 'pickup_location' === $method->id ) {
[148] Fix | Delete
continue;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
// Create a placeholder location for this custom method.
[152] Fix | Delete
$custom_method_locations[] = array(
[153] Fix | Delete
'name' => $method->get_method_title(),
[154] Fix | Delete
'enabled' => true,
[155] Fix | Delete
'address' => array(
[156] Fix | Delete
'address_1' => '123 Main Street',
[157] Fix | Delete
'city' => 'Sample City',
[158] Fix | Delete
'state' => $base_state,
[159] Fix | Delete
'postcode' => '12345',
[160] Fix | Delete
'country' => $base_country,
[161] Fix | Delete
),
[162] Fix | Delete
'details' => sprintf(
[163] Fix | Delete
/* translators: %s: shipping method title */
[164] Fix | Delete
__( 'Pickup location for %s', 'woocommerce' ),
[165] Fix | Delete
$method->get_method_title()
[166] Fix | Delete
),
[167] Fix | Delete
'method_id' => $method->id,
[168] Fix | Delete
);
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
return array_merge( $builtin_locations, $custom_method_locations );
[172] Fix | Delete
}
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function