Advertisement
daymobrew

Business Bloomer WooCommerce: Shipping Rates by Order Amount

Apr 26th, 2024
725
0
18 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.29 KB | Source Code | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Plugin Name: Business Bloomer WooCommerce: Shipping Rates by Order Amount
  5.  * Description: Provide Tiered Shipping where available shipping rates are based on order amount.
  6.  * Plugin URI: https://www.businessbloomer.com/shop/plugins/woocommerce-tiered-shipping-rate-by-order-amount/
  7.  * Update URI: https://www.businessbloomer.com/shop/plugins/woocommerce-tiered-shipping-rate-by-order-amount/
  8.  * Author: Business Bloomer
  9.  * Author URI: https://www.businessbloomer.com
  10.  * Text Domain: bbloomer-woocommerce-shipping-rates-by-order-amount
  11.  * Requires Plugins: woocommerce
  12.  * Version: 0.1.20231027
  13.  */
  14.  
  15. defined( 'ABSPATH' ) || exit;
  16.  
  17. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  18. if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
  19.     return;
  20. }
  21.  
  22. define( 'BBWSRBOA', 'https://www.businessbloomer.com/wp-json/bb/v1/downloads?product_id=XXXXXX' );
  23.  
  24. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'bbwsrboa_action_links', 9999, 4 );
  25.  
  26. function bbwsrboa_action_links( $links, $plugin_file, $plugin_data, $context ) {
  27.     if ( ! isset( $plugin_data ) ) return $links;
  28.     $plugin_links = array(
  29.         '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=shipping&section=options#bbwcofaw-start' ) . '">' . esc_html__( 'Settings', 'woocommerce' ) . '</a>',
  30.         '<a href="' . $plugin_data['PluginURI'] . '#tab-docs" target="_blank">' . esc_html__( 'Docs', 'woocommerce' ) . '</a>',
  31.         '<a href="https://businessbloomer.clarityflow.com/i/intake?title=' . rawurlencode( $plugin_data['Name'] ) . rawurlencode( ' ' ) . $plugin_data['Version'] . '" target="_blank">' . esc_html__( 'Premium Support', 'woocommerce' ) . '</a>',
  32.         '<a href="' . $plugin_data['PluginURI'] . '#tab-reviews" target="_blank">' . esc_html__( 'Add a review', 'woocommerce' ) . '</a>',
  33.     );
  34.     return array_merge( $plugin_links, $links );
  35. }
  36.  
  37. add_filter( 'plugin_row_meta', 'bbwsrboa_hide_view_details', 9999, 4 );
  38.  
  39. function bbwsrboa_hide_view_details( $plugin_meta, $plugin_file, $plugin_data, $status ) {
  40.     if ( isset( $plugin_data['TextDomain'] ) && $plugin_data['TextDomain'] == plugin_basename( __DIR__ ) ) unset( $plugin_meta[2] );
  41.     return $plugin_meta;
  42. }
  43.  
  44. add_filter( 'plugins_api', 'bbwsrboa_plugin_view_version_details', 9999, 3 );
  45.  
  46. function bbwsrboa_plugin_view_version_details( $res, $action, $args ) {
  47.     if ( 'plugin_information' !== $action ) return $res;
  48.     if ( $args->slug !== plugin_basename( __DIR__ ) ) return $res;
  49.     $response = wp_remote_get( BBWSRBOA, array(  'headers' => array( 'Accept' => 'application/json' ) ) );
  50.     if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
  51.         $download = json_decode( wp_remote_retrieve_body( $response ), true );
  52.     } else return $res;
  53.     $res = new stdClass();
  54.     $res->name = $download['name'];
  55.     $res->slug = plugin_basename( __DIR__ );
  56.     $res->path = plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php';
  57.     $res->version = $download['version'];
  58.     $res->download_link = $download['download_link'];
  59.     $res->sections = array(
  60.         'description' => $download['description'],
  61.     );
  62.     return $res;
  63. }
  64.  
  65. add_filter( 'update_plugins_www.businessbloomer.com', function( $update, array $plugin_data, string $plugin_file, $locales ) {
  66.     if ( $plugin_file !== plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php' ) return $update;    if ( ! empty( $update ) ) return $update;
  67.     $response = wp_remote_get( BBWSRBOA, array(  'headers' => array( 'Accept' => 'application/json' ) ) );
  68.     if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
  69.         $download = json_decode( wp_remote_retrieve_body( $response ), true );
  70.     } else return $update;
  71.     if ( ! version_compare( $plugin_data['Version'], $download['version'], '<' ) ) return $update;
  72.     return [
  73.         'slug' => plugin_basename( __DIR__ ),
  74.         'version' => $download['version'],
  75.         'url' => $plugin_data['PluginURI'],
  76.         'package' => $download['download_link'],
  77.     ];
  78. }, 9999, 4 );
  79.  
  80. // Return the wp_options option names - used in a number of functions.
  81. function bbwsrboa_settings_option_name() {
  82.     return 'bb_srboa';
  83. }
  84.  
  85. // Verify and save the information submitted in the settings form.
  86. add_action( 'admin_init', 'bbwsrboa_save_method_thresholds' );
  87.  
  88. function bbwsrboa_save_method_thresholds() {
  89.     if ( isset( $_GET['page'], $_GET['tab'], $_GET['section'], $_POST['_wpnonce'] )
  90.         && 'wc-settings' == $_GET['page'] && 'shipping' == $_GET['tab'] && 'options' == $_GET['section'] && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-settings' ) ) {
  91.         if ( current_user_can( 'manage_woocommerce' ) ) {
  92.             $default_options = array();  // Allow for default options in the future.
  93.             $stored_options = get_option( bbwsrboa_settings_option_name(), array() );
  94.             $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
  95.  
  96.             // ToDo: Should $bbaccpc_options be emptied before processing submitted data?
  97.             // Not emptying it could result in old shipping methods values never being deleted.
  98.             $methods_found = 0;
  99.  
  100.             foreach ( $_POST as $key => $value ) {
  101.                 if ( 0 === strpos( $key, 'flat_rate:' ) ) {
  102.                     $threshold = absint( sanitize_text_field( $value ) );
  103.                     if ( $threshold ) {
  104.                         $bbaccpc_options[ $key ] = $threshold;
  105.                         $methods_found++;
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             if ( $methods_found ) {
  111.                 update_option( bbwsrboa_settings_option_name(), $bbaccpc_options );
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117. // Settings section displayed in WooCommerce/Settings/Advanced.
  118. add_action( 'woocommerce_settings_shipping', 'bbwsrboa_settings_fields', 20 );
  119.  
  120. function bbwsrboa_settings_fields( $email_obj ) {
  121.     $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . plugin_basename( __FILE__ ) );
  122.     $quick_links = bbwsrboa_action_links( array(), '', $plugin_data, '' );
  123.     array_shift( $quick_links );
  124.  
  125.     if ( isset( $_GET['section'] ) && 'options' == $_GET['section'] ) {
  126.         echo '<div id="bbwcofaw-start" style="border: 2px dashed red; background: #fae6e6; padding: 0 2em 1em;"><h2>', $plugin_data['Name'], ' <small>v ', $plugin_data['Version'], '</small></h2>';
  127.         echo '<p>' . $plugin_data['Description'] . '</p>';
  128.         echo '<h4>Quick links: ' . implode( ' - ', $quick_links ) . '</h4>';
  129.         echo '<table class="form-table"><tbody>';
  130.  
  131.         $default_options = array();  // Allow for default options in the future.
  132.         $stored_options = get_option( bbwsrboa_settings_option_name(), array() );
  133.         $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
  134.  
  135.         // Display help for creating shipping zones.
  136.         echo '<details><summary><strong>', esc_html__( 'How to set up Shipping Zones', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ). '</strong></summary>';
  137.         echo '<p>', esc_html__( 'Read the WooCommerce documentation on <a href="https://woocommerce.com/document/setting-up-shipping-zones/" target="_blank">setting up Shipping Zones</a>.', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ), '</p>';
  138.         echo '</details>';
  139.  
  140.         $options = array();
  141.  
  142.         $zones = WC_Shipping_Zones::get_zones();
  143.         foreach ( $zones as $zone ) {
  144.             foreach ( $zone['shipping_methods'] as $index => $method ) {
  145.                 // Skip shipping methods that are not Flat Rate.
  146.                 if ( 'flat_rate' != $method->id ) { continue; }
  147.  
  148.                 $field_id = $method->id . ':' . $index;
  149.                 // Number field for shipping method.
  150.                 $value = null;
  151.                 if ( isset( $bbaccpc_options[ $field_id ] ) ) { $value = $bbaccpc_options[ $field_id ]; }
  152.                 $args = array(
  153.                             //'title'       => sprintf( '%s: %s %s', esc_html__( 'Method title', 'woocommerce' ), $zone['zone_name'], $method->title ),
  154.                             'title'       => esc_html( sprintf( '%s: %s', $zone['zone_name'], $method->title ) ),
  155.                             'desc'        => esc_html__( 'Enter the maximum cart amount to offer this shipping method.', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ),
  156.                             'id'          => $field_id,
  157.                             'placeholder' => esc_html__( 'Enter maximum cart amount', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ),
  158.                             'type'        => 'number',
  159.                             'value'       => $value,
  160.                             'autoload'    => false,
  161.                         );
  162.                 $options[] = $args;
  163.             }
  164.         }
  165.  
  166.         // Render the fields.
  167.         if ( count( $options ) ) {
  168.             echo woocommerce_admin_fields( $options );
  169.         }
  170.         else {
  171.             echo '<p>', esc_html__( 'There are no Flat Rate shipping methods. Please create some.', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ), '</p>';
  172.         }
  173.        
  174.         echo '</tr>';
  175.  
  176.         echo '</tbody></table>';
  177.         echo '<p class="submit"><a id="bbacppcsave" class="button-primary">', esc_html__( 'Save changes', 'woocommerce' ), '</a></p>';
  178.         echo '</div>';
  179.  
  180.         wc_enqueue_js( "
  181.             $('#bbacppcsave').click(function(e){
  182.                 e.preventDefault();
  183.                 $('.button-primary.woocommerce-save-button').trigger('click');
  184.             });
  185.         " );
  186.     }
  187. }
  188.  
  189.  
  190. add_filter( 'woocommerce_package_rates', 'bbwsrboa_apply_tiered_shipping', 10, 2 );
  191.  
  192. function bbwsrboa_apply_tiered_shipping( $rates, $package ) {
  193.         $default_options = array();  // Allow for default options in the future.
  194.         $stored_options = get_option( bbwsrboa_settings_option_name(), array() );
  195.         $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
  196.  
  197.         $cart_subtotal = WC()->cart->subtotal;
  198.         foreach ( $bbaccpc_options as $method => $threshold ) {
  199.             if ( $cart_subtotal >= $threshold ) {
  200.                 unset( $rates[ $method ] );
  201.             }
  202.         }
  203.  
  204.         return $rates;
  205. }
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement