Changeset 3441322
- Timestamp:
- 01/17/2026 12:24:23 AM (7 weeks ago)
- Location:
- percentage-fee-shipping/trunk
- Files:
-
- 2 added
- 3 edited
-
includes/class-aispfs-shipping-free.php (added)
-
includes/class-aispfs-shipping-percentage.php (modified) (5 diffs)
-
includes/functions.php (added)
-
percentage-fee-shipping.php (modified) (2 diffs)
-
readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
percentage-fee-shipping/trunk/includes/class-aispfs-shipping-percentage.php
r3412961 r3441322 1 1 <?php 2 if ( !defined('ABSPATH')) {2 if ( ! defined( 'ABSPATH' ) ) { 3 3 exit; 4 4 } 5 5 6 if ( !class_exists('WC_Shipping_Method')) {6 if ( ! class_exists( 'WC_Shipping_Method' ) ) { 7 7 return; 8 8 } 9 10 function aispfs_skip_min_for_admin() {11 // Doit être admin12 if ( ! ( is_user_logged_in() && current_user_can( 'manage_options' ) ) ) {13 return false;14 }15 16 // Doit avoir Order Tools activé17 if ( ! function_exists( 'is_plugin_active' ) ) {18 require_once ABSPATH . 'wp-admin/includes/plugin.php';19 }20 21 return is_plugin_active( 'aisp-order-tools/aisp-order-tools.php' );22 }23 24 25 /**26 * Méthode 1 : Shipping avec pourcentage + minimum par zone27 */28 if (!class_exists('AISPFS_Shipping_Percentage')) :29 9 30 10 class AISPFS_Shipping_Percentage extends WC_Shipping_Method { … … 34 14 public $min_cost; 35 15 36 public function __construct( $instance_id = 0) {16 public function __construct( $instance_id = 0 ) { 37 17 38 $this->id = 'aispfs_percentage_fee_shipping'; 39 $this->instance_id = absint( $instance_id);40 $this->method_title = __( 'AISPFS Percentage Shipping', 'percentage-fee-shipping');41 $this->method_description = __( 'Shipping cost based on cart percentage with optional minimum per zone.', 'percentage-fee-shipping');18 $this->id = 'aispfs_percentage_fee_shipping'; 19 $this->instance_id = absint( $instance_id ); 20 $this->method_title = __( 'AISPFS Percentage Shipping', 'percentage-fee-shipping' ); 21 $this->method_description = __( 'Shipping cost based on cart percentage with optional minimum per zone.', 'percentage-fee-shipping' ); 42 22 43 23 $this->supports = array( 44 24 'shipping-zones', 45 25 'instance-settings', 46 'instance-settings-modal' 26 'instance-settings-modal', 47 27 ); 48 28 … … 55 35 $this->init_settings(); 56 36 57 $this->title = $this->get_instance_option('title'); 58 $this->percentage = floatval( $this->get_instance_option('percentage', '5') ); 59 $this->enabled = $this->get_instance_option('enabled'); 60 61 // ✅ NEW OPTIONS 62 $this->min_enabled = $this->get_instance_option('min_enabled', 'no'); 63 $this->min_cost = floatval( $this->get_instance_option('min_cost', 25) ); 37 $this->title = $this->get_instance_option( 'title' ); 38 $this->percentage = floatval( $this->get_instance_option( 'percentage', '5' ) ); 39 $this->enabled = $this->get_instance_option( 'enabled' ); 40 $this->min_enabled = $this->get_instance_option( 'min_enabled', 'no' ); 41 $this->min_cost = floatval( $this->get_instance_option( 'min_cost', 25 ) ); 64 42 65 43 add_action( … … 74 52 75 53 'enabled' => array( 76 'title' => __( 'Enable/Disable', 'percentage-fee-shipping'),54 'title' => __( 'Enable/Disable', 'percentage-fee-shipping' ), 77 55 'type' => 'checkbox', 78 'label' => __( 'Enable this shipping method', 'percentage-fee-shipping'),79 'default' => 'yes' 56 'label' => __( 'Enable this shipping method', 'percentage-fee-shipping' ), 57 'default' => 'yes', 80 58 ), 81 59 82 60 'title' => array( 83 'title' => __( 'Method Title', 'percentage-fee-shipping'),61 'title' => __( 'Method Title', 'percentage-fee-shipping' ), 84 62 'type' => 'text', 85 'description' => __('This controls the title displayed at checkout.', 'percentage-fee-shipping'), 86 'default' => __('Percentage Shipping', 'percentage-fee-shipping'), 87 'desc_tip' => true 63 'default' => __( 'Percentage Shipping', 'percentage-fee-shipping' ), 64 'desc_tip' => true, 88 65 ), 89 66 90 67 'percentage' => array( 91 'title' => __( 'Shipping Percentage (%)', 'percentage-fee-shipping'),68 'title' => __( 'Shipping Percentage (%)', 'percentage-fee-shipping' ), 92 69 'type' => 'number', 93 'css' => 'width: 80px;', 94 'description' => __('Percentage of cart subtotal used for shipping.', 'percentage-fee-shipping'), 70 'css' => 'width:80px;', 95 71 'default' => '9', 96 'custom_attributes' => array(97 'min' => '0',98 'step' => '1'99 )100 72 ), 101 73 102 74 'min_enabled' => array( 103 'title' => __( 'Enable Minimum Shipping', 'percentage-fee-shipping'),75 'title' => __( 'Enable Minimum Shipping', 'percentage-fee-shipping' ), 104 76 'type' => 'checkbox', 105 'label' => __('Apply a minimum shipping cost for this zone', 'percentage-fee-shipping'),106 77 'default' => 'no', 107 78 ), 108 79 109 80 'min_cost' => array( 110 'title' => __('Minimum Shipping Cost', 'percentage-fee-shipping'), 111 'type' => 'number', 112 'css' => 'width: 80px;', 113 'description' => __('Minimum shipping fee for this zone.', 'percentage-fee-shipping'), 114 'default' => '25', 115 'custom_attributes' => array( 116 'min' => '0', 117 'step' => '0.01' 118 ) 119 ) 120 81 'title' => __( 'Minimum Shipping Cost', 'percentage-fee-shipping' ), 82 'type' => 'number', 83 'css' => 'width:80px;', 84 'default' => '25', 85 ), 121 86 ); 122 87 } 123 88 124 public function calculate_shipping( $package = array()) {89 public function calculate_shipping( $package = array() ) { 125 90 126 91 if ( 'no' === $this->enabled ) { … … 128 93 } 129 94 130 $ cart_subtotal = WC()->cart->get_subtotal();131 $cost = ($cart_subtotal * $this->percentage) / 100;95 $subtotal = WC()->cart->get_subtotal(); 96 $cost = ( $subtotal * $this->percentage ) / 100; 132 97 133 // ✅ APPLY MINIMUM PER ZONE (skip for admin when Order Tools active)134 98 if ( 135 $this->min_enabled === 'yes'136 && $this->min_cost > 0137 && $cost < $this->min_cost138 &&! aispfs_skip_min_for_admin()99 'yes' === $this->min_enabled && 100 $this->min_cost > 0 && 101 $cost < $this->min_cost && 102 ! aispfs_skip_min_for_admin() 139 103 ) { 140 104 $cost = $this->min_cost; 141 105 } 142 106 143 // ✅ MISSING PART (NOW FIXED) 144 $rate = array( 107 $this->add_rate( array( 145 108 'id' => $this->get_rate_id(), 146 109 'label' => $this->title, 147 'cost' => round($cost, 2) 148 ); 149 150 $this->add_rate( $rate ); 110 'cost' => round( $cost, 2 ), 111 ) ); 151 112 } 152 113 } 153 154 endif;155 156 157 /**158 * Méthode 2 : Free Shipping159 */160 if (!class_exists('AISPFS_Shipping_Free')) :161 162 class AISPFS_Shipping_Free extends WC_Shipping_Method {163 164 public $min_amount;165 166 public function __construct($instance_id = 0) {167 168 $this->id = 'aispfs_free_shipping';169 $this->instance_id = absint($instance_id);170 $this->method_title = __('AISPFS Free Shipping', 'percentage-fee-shipping');171 172 $this->supports = array(173 'shipping-zones',174 'instance-settings',175 'instance-settings-modal'176 );177 178 $this->init();179 }180 181 public function init() {182 183 $this->init_form_fields();184 $this->init_settings();185 186 $this->title = $this->get_instance_option('title', __('Free Shipping', 'percentage-fee-shipping'));187 $this->min_amount = floatval( $this->get_instance_option('min_amount', 100) );188 $this->enabled = $this->get_instance_option('enabled');189 190 add_action(191 'woocommerce_update_options_shipping_' . $this->id,192 array( $this, 'process_admin_options' )193 );194 }195 196 public function init_form_fields() {197 198 $this->instance_form_fields = array(199 200 'enabled' => array(201 'title' => __('Enable/Disable', 'percentage-fee-shipping'),202 'type' => 'checkbox',203 'label' => __('Enable Free Shipping', 'percentage-fee-shipping'),204 'default' => 'yes'205 ),206 207 'title' => array(208 'title' => __('Method Title', 'percentage-fee-shipping'),209 'type' => 'text',210 'default' => __('Free Shipping', 'percentage-fee-shipping'),211 'desc_tip' => true212 ),213 214 'min_amount' => array(215 'title' => __('Minimum Amount', 'percentage-fee-shipping'),216 'type' => 'number',217 'description' => __('Cart subtotal required to offer free shipping.', 'percentage-fee-shipping'),218 'default' => 100,219 'desc_tip' => true,220 )221 222 );223 }224 225 public function calculate_shipping($package = array()) {226 227 if ('no' === $this->enabled) {228 return;229 }230 231 $cart_subtotal = WC()->cart->get_subtotal();232 233 if ($cart_subtotal >= $this->min_amount) {234 235 $rate = array(236 'id' => $this->get_rate_id(),237 'label' => $this->title,238 'cost' => 0239 );240 241 $this->add_rate($rate);242 }243 }244 }245 246 endif;247 248 249 /**250 * Register both shipping methods251 */252 add_filter('woocommerce_shipping_methods', function ($methods) {253 254 $methods['aispfs_percentage_fee_shipping'] = 'AISPFS_Shipping_Percentage';255 $methods['aispfs_free_shipping'] = 'AISPFS_Shipping_Free';256 257 return $methods;258 });259 260 261 /**262 * Remove percentage method when free shipping is active263 */264 add_filter( 'woocommerce_package_rates', function( $rates ) {265 266 $has_free = false;267 268 foreach ( $rates as $rate_id => $rate ) {269 if ( strpos( $rate_id, 'aispfs_free_shipping' ) !== false ) {270 $has_free = true;271 break;272 }273 }274 275 if ( $has_free ) {276 foreach ( $rates as $rate_id => $rate ) {277 if ( strpos( $rate_id, 'aispfs_percentage_fee_shipping' ) !== false ) {278 unset( $rates[$rate_id] );279 }280 }281 }282 283 return $rates;284 285 }, 200 ); -
percentage-fee-shipping/trunk/percentage-fee-shipping.php
r3412961 r3441322 5 5 * Author URI: https://aispdev.com/ 6 6 * Description: Adds a shipping method where the cost is calculated as a percentage of the cart subtotal. 7 * Version: 1. 2.18 * Requires at least: 5.0 9 * Requires PHP: 7.2 7 * Version: 1.3.0 8 * Requires at least: 5.0 9 * Requires PHP: 7.2 10 10 * Author: AISP Dev 11 11 * License: GPL-2.0+ … … 15 15 */ 16 16 17 if ( !defined('ABSPATH')) {18 exit; // Exit if accessed directly17 if ( ! defined( 'ABSPATH' ) ) { 18 exit; 19 19 } 20 20 21 // Check if WooCommerce is active 22 if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')), true)) { 23 add_action('admin_notices', function () { 21 if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) { 22 add_action( 'admin_notices', function () { 24 23 ?> 25 24 <div class="notice notice-error"> 26 <p><?php esc_html_e( 'Percentage Fee Shipping requires WooCommerce to be installed and active.', 'percentage-fee-shipping'); ?></p>25 <p><?php esc_html_e( 'Percentage Fee Shipping requires WooCommerce to be installed and active.', 'percentage-fee-shipping' ); ?></p> 27 26 </div> 28 27 <?php 29 } );28 } ); 30 29 return; 31 30 } 32 31 33 // Load delivery method only when needed34 add_action('woocommerce_shipping_init', 'aispfs_load_shipping_method');35 32 36 function aispfs_load_shipping_method() { 37 require_once plugin_dir_path(__FILE__) . 'includes/class-aispfs-shipping-percentage.php'; 33 add_action( 'woocommerce_shipping_init', 'aispfs_load_shipping_methods' ); 38 34 39 } 35 function aispfs_load_shipping_methods() { 40 36 41 // Save the delivery method 42 add_filter('woocommerce_shipping_methods', 'aispfs_add_shipping_method'); 37 require_once plugin_dir_path( __FILE__ ) . 'includes/functions.php'; 38 require_once plugin_dir_path( __FILE__ ) . 'includes/class-aispfs-shipping-percentage.php'; 39 require_once plugin_dir_path( __FILE__ ) . 'includes/class-aispfs-shipping-free.php'; 40 } 43 41 44 function aispfs_add_shipping_method($methods) { 45 $methods['percentage_fee_shipping'] = 'AISPFS_Shipping_Percentage'; 42 43 add_filter( 'woocommerce_shipping_methods', 'aispfs_register_shipping_methods' ); 44 45 function aispfs_register_shipping_methods( $methods ) { 46 47 $methods['aispfs_percentage_fee_shipping'] = 'AISPFS_Shipping_Percentage'; 48 $methods['aispfs_free_shipping'] = 'AISPFS_Shipping_Free'; 49 46 50 return $methods; 47 51 } 48 49 50 -
percentage-fee-shipping/trunk/readme.txt
r3412961 r3441322 6 6 WC tested up to: 10.2 7 7 Requires PHP: 7.2 8 Stable tag: 1. 2.18 Stable tag: 1.3.0 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 45 45 == Changelog == 46 46 47 = 1.3.0 = 48 *Added support for Free Shipping restricted by User ID. Free shipping can now be limited to specific customers via shipping method settings. 49 *Refactored plugin structure into multiple class files for improved maintainability. 50 *Improved: shipping logic to ensure correct availability based on user eligibility and cart subtotal. 51 47 52 = 1.2.1 = 48 53 * Enhancement: Added integration with **AISP Order Tools** (future plugin) support to skip shipping minimum fees for administrators when managing orders manually. No action required at this time. … … 66 71 == Upgrade Notice == 67 72 73 = 1.3.0 = 74 This update introduces Free Shipping restricted by User ID. 75 You can now limit free shipping availability to specific customers directly from the shipping method settings. 76 Recommended update for stores offering VIP or customer-specific shipping benefits. 77 68 78 = 1.2.1 = 69 79 This version improves how admin users are detected during shipping calculations and lays the foundation for future integration with the upcoming **AISP Order Tools** plugin. 70 The feature to bypass minimum shipping fees for admins is now **capability-based** and will work seamlessly once AISP Order Tools is released.71 80 No setup or action is required at this time.. 72 81
Note: See TracChangeset
for help on using the changeset viewer.