Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Plugin Name: Business Bloomer WooCommerce: Bulk Sale Price
- * Description: Set a discount percentage to apply to all products.
- * Plugin URI: https://www.businessbloomer.com/shop/plugins/woocommerce-bulk-sale-price/
- * Update URI: https://www.businessbloomer.com/shop/plugins/woocommerce-bulk-sale-price/
- * Author: Business Bloomer
- * Author URI: https://www.businessbloomer.com
- * Text Domain: bbloomer-woocommerce-bulk-sale-price
- * Requires Plugins: woocommerce
- * Version: 0.1.20231101
- */
- defined( 'ABSPATH' ) || exit;
- include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
- if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
- return;
- }
- define( 'BBWBSP', 'https://www.businessbloomer.com/wp-json/bb/v1/downloads?product_id=XXXXXX' );
- add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'bbwbsp_action_links', 9999, 4 );
- function bbwbsp_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=products§ion=inventory#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', 'bbwbsp_hide_view_details', 9999, 4 );
- function bbwbsp_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', 'bbwbsp_plugin_view_version_details', 9999, 3 );
- function bbwbsp_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( BBWBSP, 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( BBWBSP, 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 bbwbsp_settings_option_name() {
- return 'bb_bsp';
- }
- // Return the wp_options option names - used in a number of functions.
- function bbwbsp_get_default_option_values() {
- return array( 'enable_discount' => '0', 'percent_discount' => 'no' );
- }
- // Verify and save the information submitted in the above form.
- add_action( 'admin_init', 'bbwbsp_save_discount_details' );
- function bbwbsp_save_discount_details() {
- // Note that $_POST['enable_discount'] is not in isset() check as it is a checkbox
- // and is not submitted if the checkbox is not checked.
- if ( isset( $_GET['page'], $_GET['tab'], $_GET['section'], $_POST['percent_discount'], $_POST['_wpnonce'] )
- && 'wc-settings' == $_GET['page'] && 'products' == $_GET['tab'] && 'inventory' == $_GET['section'] && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-settings' ) ) {
- if ( current_user_can( 'manage_woocommerce' ) ) {
- //$default_options = array( 'enable_discount' => '0', 'percent_discount' => 'no' );
- $stored_options = get_option( bbwbsp_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
- if ( isset( $_POST['enable_discount'] ) ) {
- if ( '1' == $_POST['enable_discount'] ) {
- $bbaccpc_options['enable_discount'] = 'yes';
- }
- }
- else {
- $bbaccpc_options['enable_discount'] = 'no';
- }
- if ( isset( $_POST['percent_discount'] ) ) {
- $discount = absint( $_POST['percent_discount'] );
- if ( 0 < $discount && $discount < 100 ) {
- $bbaccpc_options['percent_discount'] = $discount;
- }
- }
- update_option( bbwbsp_settings_option_name(), $bbaccpc_options );
- }
- }
- }
- // Settings section displayed in WooCommerce/Settings/Advanced.
- add_action( 'woocommerce_settings_products', 'bbwbsp_settings_fields', 20 );
- function bbwbsp_settings_fields( $email_obj ) {
- $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . plugin_basename( __FILE__ ) );
- $quick_links = bbwbsp_action_links( array(), '', $plugin_data, '' );
- array_shift( $quick_links );
- if ( isset( $_GET['section'] ) && 'inventory' == $_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( 'enable_discount' => 'no', 'percent_discount' => null );
- $stored_options = get_option( bbwbsp_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
- $options = array();
- $args = array(
- 'title' => __( 'Enable store wide discount', 'bbloomer-woocommerce-bulk-sale-price' ),
- 'desc' => __( 'Enable the discount percentage specified below.', 'bbloomer-woocommerce-bulk-sale-price' ),
- 'id' => 'enable_discount',
- 'default' => 'no',
- 'type' => 'checkbox',
- 'value' => $bbaccpc_options['enable_discount'],
- 'autoload' => false,
- );
- $options[] = $args;
- $args = array(
- 'title' => __( 'Percentage discount', 'bbloomer-woocommerce-bulk-sale-price' ),
- 'desc' => __( 'Enter the percentage discount, between 0 and 100.', 'bbloomer-woocommerce-bulk-sale-price' ),
- 'id' => 'percent_discount',
- //'placeholder' => esc_html__( 'Enter maximum cart amount', 'bbloomer-woocommerce-bulk-sale-price' ),
- 'type' => 'number',
- 'value' => $bbaccpc_options['percent_discount'],
- 'autoload' => false,
- );
- $options[] = $args;
- // Render the fields.
- echo woocommerce_admin_fields( $options );
- 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_get_price_html', 'bbwbsp_alter_price_display', 9999, 2 );
- function bbwbsp_alter_price_display( $price_html, $product ) {
- // Do not run code in backend.
- if ( is_admin() ) { return $price_html; }
- $stored_options = get_option( bbwbsp_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
- // Do not run if discounts disabled.
- if ( 'yes' != $bbaccpc_options['enable_discount'] ) { return $price_html; }
- $orig_price = wc_get_price_to_display( $product );
- $price_html = wc_price( $orig_price * ( 100 - $bbaccpc_options['percent_discount'] ) / 100 );
- return $price_html;
- }
- add_action( 'woocommerce_before_calculate_totals', 'bbwbsp_alter_price_cart', 9999 );
- function bbwbsp_alter_price_cart( $cart ) {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
- if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
- $stored_options = get_option( bbwbsp_settings_option_name(), array() );
- $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
- // Do not run if discounts disabled.
- if ( 'yes' != $bbaccpc_options['enable_discount'] ) { return; }
- // Loop through cart items and apply discount
- foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
- $product = $cart_item['data'];
- $price = $product->get_price();
- $cart_item['data']->set_price( $price * ( 100 - $bbaccpc_options['percent_discount'] ) / 100 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement