Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Plugin Name: Business Bloomer WooCommerce: Shipping Rates by Order Amount
- * Description: Provide Tiered Shipping where available shipping rates are based on order amount.
- * Plugin URI: https://www.businessbloomer.com/shop/plugins/woocommerce-tiered-shipping-rate-by-order-amount/
- * Update URI: https://www.businessbloomer.com/shop/plugins/woocommerce-tiered-shipping-rate-by-order-amount/
- * Author: Business Bloomer
- * Author URI: https://www.businessbloomer.com
- * Text Domain: bbloomer-woocommerce-shipping-rates-by-order-amount
- * Requires Plugins: woocommerce
- * Version: 0.1.20231027
- */
- defined( 'ABSPATH' ) || exit;
- include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
- if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
- return;
- }
- define( 'BBWSRBOA', 'https://www.businessbloomer.com/wp-json/bb/v1/downloads?product_id=XXXXXX' );
- add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'bbwsrboa_action_links', 9999, 4 );
- function bbwsrboa_action_links( $links, $plugin_file, $plugin_data, $context ) {
- if ( ! isset( $plugin_data ) ) return $links;
- $plugin_links = array(
- '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=shipping§ion=options#bbwcofaw-start' ) . '">' . esc_html__( 'Settings', 'woocommerce' ) . '</a>',
- '<a href="' . $plugin_data['PluginURI'] . '#tab-docs" target="_blank">' . esc_html__( 'Docs', 'woocommerce' ) . '</a>',
- '<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>',
- '<a href="' . $plugin_data['PluginURI'] . '#tab-reviews" target="_blank">' . esc_html__( 'Add a review', 'woocommerce' ) . '</a>',
- );
- return array_merge( $plugin_links, $links );
- }
- add_filter( 'plugin_row_meta', 'bbwsrboa_hide_view_details', 9999, 4 );
- function bbwsrboa_hide_view_details( $plugin_meta, $plugin_file, $plugin_data, $status ) {
- if ( isset( $plugin_data['TextDomain'] ) && $plugin_data['TextDomain'] == plugin_basename( __DIR__ ) ) unset( $plugin_meta[2] );
- return $plugin_meta;
- }
- add_filter( 'plugins_api', 'bbwsrboa_plugin_view_version_details', 9999, 3 );
- function bbwsrboa_plugin_view_version_details( $res, $action, $args ) {
- if ( 'plugin_information' !== $action ) return $res;
- if ( $args->slug !== plugin_basename( __DIR__ ) ) return $res;
- $response = wp_remote_get( BBWSRBOA, array( 'headers' => array( 'Accept' => 'application/json' ) ) );
- if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
- $download = json_decode( wp_remote_retrieve_body( $response ), true );
- } else return $res;
- $res = new stdClass();
- $res->name = $download['name'];
- $res->slug = plugin_basename( __DIR__ );
- $res->path = plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php';
- $res->version = $download['version'];
- $res->download_link = $download['download_link'];
- $res->sections = array(
- 'description' => $download['description'],
- );
- return $res;
- }
- add_filter( 'update_plugins_www.businessbloomer.com', function( $update, array $plugin_data, string $plugin_file, $locales ) {
- if ( $plugin_file !== plugin_basename( __DIR__ ) . '/' . plugin_basename( __DIR__ ) . '.php' ) return $update; if ( ! empty( $update ) ) return $update;
- $response = wp_remote_get( BBWSRBOA, array( 'headers' => array( 'Accept' => 'application/json' ) ) );
- if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) {
- $download = json_decode( wp_remote_retrieve_body( $response ), true );
- } else return $update;
- if ( ! version_compare( $plugin_data['Version'], $download['version'], '<' ) ) return $update;
- return [
- 'slug' => plugin_basename( __DIR__ ),
- 'version' => $download['version'],
- 'url' => $plugin_data['PluginURI'],
- 'package' => $download['download_link'],
- ];
- }, 9999, 4 );
- // Return the wp_options option names - used in a number of functions.
- function bbwsrboa_settings_option_name() {
- return 'bb_srboa';
- }
- // Verify and save the information submitted in the settings form.
- add_action( 'admin_init', 'bbwsrboa_save_method_thresholds' );
- function bbwsrboa_save_method_thresholds() {
- if ( isset( $_GET['page'], $_GET['tab'], $_GET['section'], $_POST['_wpnonce'] )
- && 'wc-settings' == $_GET['page'] && 'shipping' == $_GET['tab'] && 'options' == $_GET['section'] && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-settings' ) ) {
- if ( current_user_can( 'manage_woocommerce' ) ) {
- $default_options = array(); // Allow for default options in the future.
- $stored_options = get_option( bbwsrboa_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
- // ToDo: Should $bbaccpc_options be emptied before processing submitted data?
- // Not emptying it could result in old shipping methods values never being deleted.
- $methods_found = 0;
- foreach ( $_POST as $key => $value ) {
- if ( 0 === strpos( $key, 'flat_rate:' ) ) {
- $threshold = absint( sanitize_text_field( $value ) );
- if ( $threshold ) {
- $bbaccpc_options[ $key ] = $threshold;
- $methods_found++;
- }
- }
- }
- if ( $methods_found ) {
- update_option( bbwsrboa_settings_option_name(), $bbaccpc_options );
- }
- }
- }
- }
- // Settings section displayed in WooCommerce/Settings/Advanced.
- add_action( 'woocommerce_settings_shipping', 'bbwsrboa_settings_fields', 20 );
- function bbwsrboa_settings_fields( $email_obj ) {
- $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . plugin_basename( __FILE__ ) );
- $quick_links = bbwsrboa_action_links( array(), '', $plugin_data, '' );
- array_shift( $quick_links );
- if ( isset( $_GET['section'] ) && 'options' == $_GET['section'] ) {
- 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>';
- echo '<p>' . $plugin_data['Description'] . '</p>';
- echo '<h4>Quick links: ' . implode( ' - ', $quick_links ) . '</h4>';
- echo '<table class="form-table"><tbody>';
- $default_options = array(); // Allow for default options in the future.
- $stored_options = get_option( bbwsrboa_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
- // Display help for creating shipping zones.
- echo '<details><summary><strong>', esc_html__( 'How to set up Shipping Zones', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ). '</strong></summary>';
- 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>';
- echo '</details>';
- $options = array();
- $zones = WC_Shipping_Zones::get_zones();
- foreach ( $zones as $zone ) {
- foreach ( $zone['shipping_methods'] as $index => $method ) {
- // Skip shipping methods that are not Flat Rate.
- if ( 'flat_rate' != $method->id ) { continue; }
- $field_id = $method->id . ':' . $index;
- // Number field for shipping method.
- $value = null;
- if ( isset( $bbaccpc_options[ $field_id ] ) ) { $value = $bbaccpc_options[ $field_id ]; }
- $args = array(
- //'title' => sprintf( '%s: %s %s', esc_html__( 'Method title', 'woocommerce' ), $zone['zone_name'], $method->title ),
- 'title' => esc_html( sprintf( '%s: %s', $zone['zone_name'], $method->title ) ),
- 'desc' => esc_html__( 'Enter the maximum cart amount to offer this shipping method.', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ),
- 'id' => $field_id,
- 'placeholder' => esc_html__( 'Enter maximum cart amount', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ),
- 'type' => 'number',
- 'value' => $value,
- 'autoload' => false,
- );
- $options[] = $args;
- }
- }
- // Render the fields.
- if ( count( $options ) ) {
- echo woocommerce_admin_fields( $options );
- }
- else {
- echo '<p>', esc_html__( 'There are no Flat Rate shipping methods. Please create some.', 'bbloomer-woocommerce-shipping-rates-by-order-amount' ), '</p>';
- }
- echo '</tr>';
- echo '</tbody></table>';
- echo '<p class="submit"><a id="bbacppcsave" class="button-primary">', esc_html__( 'Save changes', 'woocommerce' ), '</a></p>';
- echo '</div>';
- wc_enqueue_js( "
- $('#bbacppcsave').click(function(e){
- e.preventDefault();
- $('.button-primary.woocommerce-save-button').trigger('click');
- });
- " );
- }
- }
- add_filter( 'woocommerce_package_rates', 'bbwsrboa_apply_tiered_shipping', 10, 2 );
- function bbwsrboa_apply_tiered_shipping( $rates, $package ) {
- $default_options = array(); // Allow for default options in the future.
- $stored_options = get_option( bbwsrboa_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, $default_options );
- $cart_subtotal = WC()->cart->subtotal;
- foreach ( $bbaccpc_options as $method => $threshold ) {
- if ( $cart_subtotal >= $threshold ) {
- unset( $rates[ $method ] );
- }
- }
- return $rates;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement