Advertisement
daymobrew

Business Bloomer WooCommerce: Bulk Sale Price

Apr 26th, 2024
711
0
19 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.61 KB | Source Code | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Plugin Name: Business Bloomer WooCommerce: Bulk Sale Price
  5.  * Description: Set a discount percentage to apply to all products.
  6.  * Plugin URI: https://www.businessbloomer.com/shop/plugins/woocommerce-bulk-sale-price/
  7.  * Update URI: https://www.businessbloomer.com/shop/plugins/woocommerce-bulk-sale-price/
  8.  * Author: Business Bloomer
  9.  * Author URI: https://www.businessbloomer.com
  10.  * Text Domain: bbloomer-woocommerce-bulk-sale-price
  11.  * Requires Plugins: woocommerce
  12.  * Version: 0.1.20231101
  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( 'BBWBSP', 'https://www.businessbloomer.com/wp-json/bb/v1/downloads?product_id=XXXXXX' );
  23.  
  24. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'bbwbsp_action_links', 9999, 4 );
  25.  
  26. function bbwbsp_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=products&section=inventory#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', 'bbwbsp_hide_view_details', 9999, 4 );
  38.  
  39. function bbwbsp_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', 'bbwbsp_plugin_view_version_details', 9999, 3 );
  45.  
  46. function bbwbsp_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( BBWBSP, 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( BBWBSP, 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 bbwbsp_settings_option_name() {
  82.     return 'bb_bsp';
  83. }
  84.  
  85. // Return the wp_options option names - used in a number of functions.
  86. function bbwbsp_get_default_option_values() {
  87.     return array( 'enable_discount' => '0', 'percent_discount' => 'no' );
  88. }
  89.  
  90. // Verify and save the information submitted in the above form.
  91. add_action( 'admin_init', 'bbwbsp_save_discount_details' );
  92.  
  93. function bbwbsp_save_discount_details() {
  94.     // Note that $_POST['enable_discount'] is not in isset() check as it is a checkbox
  95.     // and is not submitted if the checkbox is not checked.
  96.     if ( isset( $_GET['page'], $_GET['tab'], $_GET['section'], $_POST['percent_discount'], $_POST['_wpnonce'] )
  97.         && 'wc-settings' == $_GET['page'] && 'products' == $_GET['tab'] && 'inventory' == $_GET['section'] && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-settings' ) ) {
  98.         if ( current_user_can( 'manage_woocommerce' ) ) {
  99.             //$default_options = array( 'enable_discount' => '0', 'percent_discount' => 'no' );
  100.             $stored_options = get_option( bbwbsp_settings_option_name(), array() );
  101.             $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
  102.            
  103.             if ( isset( $_POST['enable_discount'] ) ) {
  104.                 if ( '1' == $_POST['enable_discount'] ) {
  105.                     $bbaccpc_options['enable_discount'] = 'yes';
  106.                 }
  107.             }
  108.             else {
  109.                 $bbaccpc_options['enable_discount'] = 'no';
  110.             }
  111.             if ( isset( $_POST['percent_discount'] ) ) {
  112.                 $discount = absint( $_POST['percent_discount'] );
  113.                 if ( 0 < $discount && $discount < 100 ) {
  114.                     $bbaccpc_options['percent_discount'] = $discount;
  115.                 }
  116.             }
  117.  
  118.             update_option( bbwbsp_settings_option_name(), $bbaccpc_options );
  119.         }
  120.     }
  121. }
  122.  
  123. // Settings section displayed in WooCommerce/Settings/Advanced.
  124. add_action( 'woocommerce_settings_products', 'bbwbsp_settings_fields', 20 );
  125.  
  126. function bbwbsp_settings_fields( $email_obj ) {
  127.     $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . plugin_basename( __FILE__ ) );
  128.     $quick_links = bbwbsp_action_links( array(), '', $plugin_data, '' );
  129.     array_shift( $quick_links );
  130.  
  131.     if ( isset( $_GET['section'] ) && 'inventory' == $_GET['section'] ) {
  132.         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>';
  133.         echo '<p>' . $plugin_data['Description'] . '</p>';
  134.         echo '<h4>Quick links: ' . implode( ' - ', $quick_links ) . '</h4>';
  135.         echo '<table class="form-table"><tbody>';
  136.  
  137.         //$default_options = array( 'enable_discount' => 'no', 'percent_discount' => null );
  138.         $stored_options = get_option( bbwbsp_settings_option_name(), array() );
  139.         $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
  140.  
  141.         $options = array();
  142.  
  143.         $args = array(
  144.                     'title'       => __( 'Enable store wide discount', 'bbloomer-woocommerce-bulk-sale-price' ),
  145.                     'desc'        => __( 'Enable the discount percentage specified below.', 'bbloomer-woocommerce-bulk-sale-price' ),
  146.                     'id'          => 'enable_discount',
  147.                     'default'     => 'no',
  148.                     'type'        => 'checkbox',
  149.                     'value'       => $bbaccpc_options['enable_discount'],
  150.                     'autoload'    => false,
  151.                 );
  152.         $options[] = $args;
  153.  
  154.         $args = array(
  155.                     'title'       => __( 'Percentage discount', 'bbloomer-woocommerce-bulk-sale-price' ),
  156.                     'desc'        => __( 'Enter the percentage discount, between 0 and 100.', 'bbloomer-woocommerce-bulk-sale-price' ),
  157.                     'id'          => 'percent_discount',
  158.                     //'placeholder' => esc_html__( 'Enter maximum cart amount', 'bbloomer-woocommerce-bulk-sale-price' ),
  159.                     'type'        => 'number',
  160.                     'value'       => $bbaccpc_options['percent_discount'],
  161.                     'autoload'    => false,
  162.                 );
  163.         $options[] = $args;
  164.  
  165.  
  166.         // Render the fields.
  167.         echo woocommerce_admin_fields( $options );
  168.        
  169.         echo '</tr>';
  170.  
  171.         echo '</tbody></table>';
  172.         echo '<p class="submit"><a id="bbacppcsave" class="button-primary">', esc_html__( 'Save changes', 'woocommerce' ), '</a></p>';
  173.         echo '</div>';
  174.  
  175.         wc_enqueue_js( "
  176.             $('#bbacppcsave').click(function(e){
  177.                 e.preventDefault();
  178.                 $('.button-primary.woocommerce-save-button').trigger('click');
  179.             });
  180.         " );
  181.     }
  182. }
  183.  
  184.  
  185. add_filter( 'woocommerce_get_price_html', 'bbwbsp_alter_price_display', 9999, 2 );
  186.  
  187. function bbwbsp_alter_price_display( $price_html, $product ) {
  188.     // Do not run code in backend.
  189.     if ( is_admin() ) { return $price_html; }
  190.  
  191.     $stored_options = get_option( bbwbsp_settings_option_name(), array() );
  192.     $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
  193.  
  194.     // Do not run if discounts disabled.
  195.     if ( 'yes' != $bbaccpc_options['enable_discount'] ) { return $price_html; }
  196.  
  197.     $orig_price = wc_get_price_to_display( $product );
  198.     $price_html = wc_price( $orig_price * ( 100 - $bbaccpc_options['percent_discount'] ) / 100 );
  199.  
  200.     return $price_html;
  201. }
  202.  
  203.  
  204. add_action( 'woocommerce_before_calculate_totals', 'bbwbsp_alter_price_cart', 9999 );
  205.  
  206. function bbwbsp_alter_price_cart( $cart ) {
  207.     if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
  208.  
  209.     if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
  210.  
  211.     $stored_options = get_option( bbwbsp_settings_option_name(), array() );
  212.     $bbaccpc_options = wp_parse_args( $stored_options, bbwbsp_get_default_option_values() );
  213.  
  214.     // Do not run if discounts disabled.
  215.     if ( 'yes' != $bbaccpc_options['enable_discount'] ) { return; }
  216.  
  217.     // Loop through cart items and apply discount
  218.     foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
  219.         $product = $cart_item['data'];
  220.         $price = $product->get_price();
  221.         $cart_item['data']->set_price( $price * ( 100 - $bbaccpc_options['percent_discount'] ) / 100 );
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement