PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.1.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.1.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / utils / shipping-utils.php

shipping-utils.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.1.0, at includes/utils/shipping-utils.php

130 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Utils;
4
5 use StoreEngine\Classes\Countries;
6 use StoreEngine\Classes\Exceptions\StoreEngineException;
7 use StoreEngine\Shipping\Methods\ShippingMethod;
8 use StoreEngine\Shipping\Shipping;
9 use StoreEngine\Shipping\Shipping as ShippingObj;
10 use StoreEngine\Shipping\ShippingRate;
11 use WP_Error;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 class ShippingUtils {
18 public static function is_shipping_enabled(): bool {
19 return apply_filters( 'storeengine/shipping_enabled', 'disabled' !== Helper::get_settings( 'ship_to_countries' ) );
20 }
21
22 public static function get_shipping_methods_count( $enabled_only = false ): int {
23 global $wpdb;
24
25 $transient_name = 'storeengine_shipping_method_count';
26 $transient_version = Caching::get_transient_version( 'shipping' );
27 $transient_value = get_transient( $transient_name );
28 $counts = array(
29 'enabled' => 0,
30 'disabled' => 0,
31 );
32
33 if ( ! isset( $transient_value['enabled'], $transient_value['disabled'], $transient_value['version'] ) || $transient_value['version'] !== $transient_version ) {
34 $methods = Shipping::init()->get_shipping_methods();
35 $method_ids = array_map( fn( $method ) => $method->get_method_id(), $methods );
36
37 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
38 $counts['enabled'] = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}storeengine_shipping_zone_methods WHERE is_enabled=1 AND method_id IN ('" . implode( "','", array_map( 'esc_sql', $method_ids ) ) . "')" ) );
39 $counts['disabled'] = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}storeengine_shipping_zone_methods WHERE is_enabled=0 AND method_id IN ('" . implode( "','", array_map( 'esc_sql', $method_ids ) ) . "')" ) );
40 // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
41
42 $transient_value = array(
43 'version' => $transient_version,
44 'enabled' => $counts['enabled'],
45 'disabled' => $counts['disabled'],
46 );
47
48 set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
49 } else {
50 $counts = $transient_value;
51 }
52
53 if ( $enabled_only ) {
54 $return = $counts['enabled'];
55 } else {
56 $return = $counts['enabled'] + $counts['disabled'];
57 }
58
59 return $return;
60 }
61
62 public static function cart_totals_shipping_method_label( ShippingRate $method ) {
63 $label = $method->get_label();
64 $has_cost = 0 < $method->get_cost();
65 $hide_cost = ! $has_cost && in_array( $method->get_method_id(), [ 'free_shipping', 'local_pickup' ], true );
66
67 if ( $has_cost && ! $hide_cost ) {
68 if ( Helper::cart()->display_prices_including_tax() ) {
69 $label .= ': ' . Formatting::price( (float) $method->get_cost() + $method->get_shipping_tax() );
70 if ( $method->get_shipping_tax() > 0 && ! Helper::get_settings( 'prices_include_tax' ) ) {
71 $label .= ' <small class="tax_label">' . Countries::init()->inc_tax_or_vat() . '</small>';
72 }
73 } else {
74 $label .= ': ' . Formatting::price( $method->get_cost() );
75 if ( $method->get_shipping_tax() > 0 && Helper::get_settings( 'prices_include_tax' ) ) {
76 $label .= ' <small class="tax_label">' . Countries::init()->ex_tax_or_vat() . '</small>';
77 }
78 }
79 }
80
81 return apply_filters( 'storeengine/shipping/cart_shipping_method_full_label', $label, $method );
82 }
83
84 /**
85 * @param string $method_id
86 * @param int $instance_id
87 *
88 * @return ShippingMethod|WP_Error
89 */
90 public static function get_shipping_method( string $method_id, int $instance_id = 0 ) {
91 $shipping_methods = ShippingObj::init()->get_shipping_method_class_names();
92 if ( ! array_key_exists( $method_id, $shipping_methods ) ) {
93 return new WP_Error( 'invalid-value', __( 'Invalid shipping method Id provided.', 'storeengine' ) );
94 }
95 if ( ! class_exists( $shipping_methods[ $method_id ] ) ) {
96 return new WP_Error( 'invalid-value', __( 'Class not found for shipping method Id!', 'storeengine' ) );
97 }
98
99 try {
100 return new $shipping_methods[ $method_id ]( $instance_id );
101 } catch ( StoreEngineException $e ) {
102 return $e->get_wp_error();
103 }
104 }
105
106 public static function get_chosen_shipping_method_ids(): array {
107 return [];
108 // @TODO implement separate session data manager and then use this,
109 // as this gets called in user object before cart gets initialized.
110 /*$cart = Helper::cart();
111 if ( ! $cart ) {
112 return [];
113 }
114 $chosen_methods = $cart->get_meta( 'chosen_shipping_methods' ) ?? [];
115 $method_ids = [];
116
117 foreach ( $chosen_methods as $chosen_method ) {
118 if ( ! is_string( $chosen_method ) ) {
119 continue;
120 }
121 $chosen_method = explode( ':', $chosen_method );
122 $method_ids[] = current( $chosen_method );
123 }
124
125 return $method_ids;*/
126 }
127 }
128
129 // End of file shipping-utils.php.
130