/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Utilities/LocalPickupUtils.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Automattic\WooCommerce\StoreApi\Utilities;

/**
 * Util class for local pickup related functionality, this contains methods that need to be accessed from places besides
 * the ShippingController, i.e. the OrderController.
 */
class LocalPickupUtils {

    
/**
     * Gets the local pickup location settings.
     *
     * @param string $context The context for the settings. Defaults to 'view'.
     */
    
public static function get_local_pickup_settings$context 'view' ) {
        
$pickup_location_settings get_option(
            
'woocommerce_pickup_location_settings',
            [
                
'enabled' => 'no',
                
'title'   => __'Pickup''woocommerce' ),
            ]
        );

        if ( empty( 
$pickup_location_settings['title'] ) ) {
            
$pickup_location_settings['title'] = __'Pickup''woocommerce' );
        }

        if ( empty( 
$pickup_location_settings['enabled'] ) ) {
            
$pickup_location_settings['enabled'] = 'no';
        }

        
// Return settings as is if we're editing them.
        
if ( 'edit' === $context ) {
            return 
$pickup_location_settings;
        }

        
// All consumers of this turn it into a bool eventually. Doing it here removes the need for that.
        
$pickup_location_settings['enabled'] = wc_string_to_bool$pickup_location_settings['enabled'] );
        
$pickup_location_settings['title']   = wc_clean$pickup_location_settings['title'] );

        return 
$pickup_location_settings;
    }

    
/**
     * Checks if WC Blocks local pickup is enabled.
     *
     * @return bool True if local pickup is enabled.
     */
    
public static function is_local_pickup_enabled() {
        
// Get option directly to avoid early translation function call.
        // See https://github.com/woocommerce/woocommerce/pull/47113.
        
$pickup_location_settings get_option(
            
'woocommerce_pickup_location_settings',
            [
                
'enabled' => 'no',
            ]
        );

        if ( empty( 
$pickup_location_settings['enabled'] ) ) {
            
$pickup_location_settings['enabled'] = 'no';
        }

        return 
wc_string_to_bool$pickup_location_settings['enabled'] );
    }
    
/**
     * Gets a list of payment method ids that support the 'local-pickup' feature.
     *
     * @return string[] List of payment method ids that support the 'local-pickup' feature.
     */
    
public static function get_local_pickup_method_ids() {
        
$all_methods_supporting_local_pickup array_reduce(
            
WC()->shipping()->get_shipping_methods(),
            function ( 
$methods$method ) {
                if ( 
$method->supports'local-pickup' ) ) {
                    
$methods[] = $method->id;
                }
                return 
$methods;
            },
            array()
        );

        
// We use array_values because this will be used in JS, so we don't need the (numerical) keys.
        
return array_values(
        
// This array_unique is necessary because WC()->shipping()->get_shipping_methods() can return duplicates.
            
array_unique(
                
$all_methods_supporting_local_pickup
            
)
        );
    }
}