Advertisement
Guest User

WooCommerce Bulk Discount - 10 Ranges

a guest
Dec 19th, 2014
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.42 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WooCommerce Bulk Discount
  4. Plugin URI: http://wordpress.org/plugins/woocommerce-bulk-discount/
  5. Description: Apply fine-grained bulk discounts to items in the shopping cart.
  6. Author: Rene Puchinger
  7. Version: 2.3.1
  8. Author URI: https://profiles.wordpress.org/rene-puchinger/
  9. License: GPL3
  10.  
  11. Copyright (C) 2013 Rene Puchinger
  12.  
  13. This program is free software: you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation, either version 3 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program. If not, see <http://www.gnu.org/licenses/>.
  25.  
  26. */
  27.  
  28. if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  29.  
  30. if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return; // Check if WooCommerce is active
  31.  
  32. if ( !class_exists( 'Woo_Bulk_Discount_Plugin_t4m' ) ) {
  33.  
  34. class Woo_Bulk_Discount_Plugin_t4m {
  35.  
  36. var $discount_coeffs;
  37. var $bulk_discount_calculated = false;
  38.  
  39. public function __construct() {
  40.  
  41. load_plugin_textdomain( 'wc_bulk_discount', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
  42.  
  43. $this->current_tab = ( isset( $_GET['tab'] ) ) ? $_GET['tab'] : 'general';
  44.  
  45. $this->settings_tabs = array(
  46. 'bulk_discount' => __( 'Bulk Discount', 'wc_bulk_discount' )
  47. );
  48.  
  49. add_action( 'admin_enqueue_scripts', array( $this, 'action_enqueue_dependencies_admin' ) );
  50. add_action( 'wp_head', array( $this, 'action_enqueue_dependencies' ) );
  51.  
  52. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'action_links' ) );
  53.  
  54. add_action( 'woocommerce_settings_tabs', array( $this, 'add_tab' ), 10 );
  55.  
  56. // Run these actions when generating the settings tabs.
  57. foreach ( $this->settings_tabs as $name => $label ) {
  58. add_action( 'woocommerce_settings_tabs_' . $name, array( $this, 'settings_tab_action' ), 10 );
  59. add_action( 'woocommerce_update_options_' . $name, array( $this, 'save_settings' ), 10 );
  60. }
  61.  
  62. // Add the settings fields to each tab.
  63. add_action( 'woocommerce_bulk_discount_settings', array( $this, 'add_settings_fields' ), 10 );
  64.  
  65. add_action( 'woocommerce_loaded', array( $this, 'woocommerce_loaded' ) );
  66.  
  67. }
  68.  
  69. /**
  70. * Main processing hooks
  71. */
  72. public function woocommerce_loaded() {
  73.  
  74. if ( get_option( 'woocommerce_t4m_enable_bulk_discounts', 'yes' ) == 'yes' ) {
  75.  
  76. add_action( 'woocommerce_before_calculate_totals', array( $this, 'action_before_calculate' ), 10, 1 );
  77. add_action( 'woocommerce_calculate_totals', array( $this, 'action_after_calculate' ), 10, 1 );
  78. add_action( 'woocommerce_before_cart_table', array( $this, 'before_cart_table' ) );
  79. add_action( 'woocommerce_single_product_summary', array( $this, 'single_product_summary' ), 45 );
  80. add_filter( 'woocommerce_cart_item_subtotal', array( $this, 'filter_subtotal_price' ), 10, 2 );
  81. add_filter( 'woocommerce_checkout_item_subtotal', array( $this, 'filter_subtotal_price' ), 10, 2 );
  82. add_filter( 'woocommerce_order_formatted_line_subtotal', array( $this, 'filter_subtotal_order_price' ), 10, 3 );
  83. add_filter( 'woocommerce_product_write_panel_tabs', array( $this, 'action_product_write_panel_tabs' ) );
  84. add_filter( 'woocommerce_product_write_panels', array( $this, 'action_product_write_panels' ) );
  85. add_action( 'woocommerce_process_product_meta', array( $this, 'action_process_meta' ) );
  86. add_filter( 'woocommerce_cart_product_subtotal', array( $this, 'filter_cart_product_subtotal' ), 10, 3 );
  87. add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'order_update_meta' ) );
  88.  
  89. if ( version_compare( WOOCOMMERCE_VERSION, "2.1.0" ) >= 0 ) {
  90. add_filter( 'woocommerce_cart_item_price', array( $this, 'filter_item_price' ), 10, 2 );
  91. add_filter( 'woocommerce_update_cart_validation', array( $this, 'filter_before_calculate' ), 10, 1 );
  92. } else {
  93. add_filter( 'woocommerce_cart_item_price_html', array( $this, 'filter_item_price' ), 10, 2 );
  94. }
  95.  
  96. }
  97.  
  98. }
  99.  
  100. /**
  101. * Add action links under WordPress > Plugins
  102. *
  103. * @param $links
  104. * @return array
  105. */
  106. public function action_links( $links ) {
  107.  
  108. $settings_slug = 'woocommerce';
  109.  
  110. if ( version_compare( WOOCOMMERCE_VERSION, "2.1.0" ) >= 0 ) {
  111.  
  112. $settings_slug = 'wc-settings';
  113.  
  114. }
  115.  
  116. $plugin_links = array(
  117. '<a href="' . admin_url( 'admin.php?page=' . $settings_slug . '&tab=bulk_discount' ) . '">' . __( 'Settings', 'woocommerce' ) . '</a>',
  118. );
  119.  
  120. return array_merge( $plugin_links, $links );
  121. }
  122.  
  123. /**
  124. * For given product, and quantity return the price modifying factor (percentage discount) or value to deduct (flat discount).
  125. *
  126. * @param $product_id
  127. * @param $quantity
  128. * @param $order
  129. * @return float
  130. */
  131. protected function get_discounted_coeff( $product_id, $quantity ) {
  132.  
  133. $q = array( 0.0 );
  134. $d = array( 0.0 );
  135.  
  136. /* Find the appropriate discount coefficient by looping through up to the five discount settings */
  137. for ( $i = 1; $i <= 10; $i++ ) {
  138. array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) );
  139. if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) {
  140. array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) : 0.0 );
  141. } else {
  142. array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) : 0.0 );
  143. }
  144. if ( $quantity >= $q[$i] && $q[$i] > $q[0] ) {
  145. $q[0] = $q[$i];
  146. $d[0] = $d[$i];
  147. }
  148. }
  149.  
  150. // for percentage discount convert the resulting discount from % to the multiplying coefficient
  151. return ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ? max( 0, $d[0] ) : min( 1.0, max( 0, ( 100.0 - round( $d[0], 2 ) ) / 100.0 ) );
  152.  
  153. }
  154.  
  155. /**
  156. * Filter product price so that the discount is visible.
  157. *
  158. * @param $price
  159. * @param $values
  160. * @return string
  161. */
  162. public function filter_item_price( $price, $values ) {
  163.  
  164. if ( !$values || @!$values['data'] ) {
  165. return $price;
  166. }
  167. if ( $this->coupon_check() ) {
  168. return $price;
  169. }
  170. $_product = $values['data'];
  171. if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
  172. return $price;
  173. }
  174. if ( ( get_option( 'woocommerce_t4m_show_on_item', 'yes' ) == 'no' ) ) {
  175. return $price;
  176. }
  177. if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
  178. return $price; // for flat discount this filter has no meaning
  179. }
  180. if ( empty( $this->discount_coeffs ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )] )
  181. || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] )
  182. ) {
  183. $this->gather_discount_coeffs();
  184. }
  185. $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
  186. if ( $coeff == 1.0 ) {
  187. return $price; // no price modification
  188. }
  189. $discprice = woocommerce_price( $_product->get_price() * $coeff );
  190. $oldprice = woocommerce_price( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] );
  191. $old_css = esc_attr( get_option( 'woocommerce_t4m_css_old_price', 'color: #777; text-decoration: line-through; margin-right: 4px;' ) );
  192. $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
  193. return "<span class='discount-info' title='" . sprintf( __( '%s%% bulk discount applied!', 'wc_bulk_discount' ), round( ( 1.0 - $coeff ) * 100.0, 2 ) ) . "'>" .
  194. "<span class='old-price' style='$old_css'>$oldprice</span>" .
  195. "<span class='new-price' style='$new_css'>$discprice</span></span>";
  196.  
  197. }
  198.  
  199. /**
  200. * Filter product price so that the discount is visible.
  201. *
  202. * @param $price
  203. * @param $values
  204. * @return string
  205. */
  206. public function filter_subtotal_price( $price, $values ) {
  207.  
  208. if ( !$values || !$values['data'] ) {
  209. return $price;
  210. }
  211. if ( $this->coupon_check() ) {
  212. return $price;
  213. }
  214. $_product = $values['data'];
  215. if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
  216. return $price;
  217. }
  218. if ( ( get_option( 'woocommerce_t4m_show_on_subtotal', 'yes' ) == 'no' ) ) {
  219. return $price;
  220. }
  221. if ( empty( $this->discount_coeffs ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )] )
  222. || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] ) || !isset( $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] )
  223. ) {
  224. $this->gather_discount_coeffs();
  225. }
  226. $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
  227. if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' && $coeff == 0 ) || ( get_option( 'woocommerce_t4m_discount_type', '' ) == '' && $coeff == 1.0 ) ) {
  228. return $price; // no price modification
  229. }
  230. $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
  231. $bulk_info = sprintf( __( 'Incl. %s discount', 'wc_bulk_discount' ), ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ? get_woocommerce_currency_symbol() . $coeff : ( round( ( 1 - $coeff ) * 100, 2 ) . "%" ) ) );
  232.  
  233. return "<span class='discount-info' title='$bulk_info'>" .
  234. "<span>$price</span>" .
  235. "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
  236.  
  237. }
  238.  
  239. /**
  240. * Gather discount information to the array $this->discount_coefs
  241. */
  242. protected function gather_discount_coeffs() {
  243.  
  244. global $woocommerce;
  245.  
  246. $cart = $woocommerce->cart;
  247. $this->discount_coeffs = array();
  248.  
  249. if ( sizeof( $cart->cart_contents ) > 0 ) {
  250. foreach ( $cart->cart_contents as $cart_item_key => $values ) {
  251. $_product = $values['data'];
  252. $quantity = 0;
  253. if ( get_option( 'woocommerce_t4m_variations_separate', 'yes' ) == 'no' && $_product instanceof WC_Product_Variation && $_product->parent ) {
  254. $parent = $_product->parent;
  255. foreach ( $cart->cart_contents as $valuesInner ) {
  256. $p = $valuesInner['data'];
  257. if ( $p instanceof WC_Product_Variation && $p->parent && $p->parent->id == $parent->id ) {
  258. $quantity += $valuesInner['quantity'];
  259. $this->discount_coeffs[$_product->variation_id]['quantity'] = $quantity;
  260. }
  261. }
  262. } else {
  263. $quantity = $values['quantity'];
  264. }
  265. $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] = $this->get_discounted_coeff( $_product->id, $quantity );
  266. $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] = $_product->get_price();
  267. }
  268. }
  269.  
  270. }
  271.  
  272. /**
  273. * Filter product price so that the discount is visible during order viewing.
  274. *
  275. * @param $price
  276. * @param $values
  277. * @return string
  278. */
  279. public function filter_subtotal_order_price( $price, $values, $order ) {
  280.  
  281. if ( !$values || !$order ) {
  282. return $price;
  283. }
  284. if ( $this->coupon_check() ) {
  285. return $price;
  286. }
  287.  
  288. $_product = get_product( $values['product_id'] );
  289. if ( get_post_meta( $values['product_id'], "_bulkdiscount_enabled", true ) != '' && get_post_meta( $values['product_id'], "_bulkdiscount_enabled", true ) !== 'yes' ) {
  290. return $price;
  291. }
  292. if ( ( get_option( 'woocommerce_t4m_show_on_order_subtotal', 'yes' ) == 'no' ) ) {
  293. return $price;
  294. }
  295. $actual_id = $values['product_id'];
  296. if ( $_product && $_product instanceof WC_Product_Variable && $values['variation_id'] ) {
  297. $actual_id = $values['variation_id'];
  298. }
  299. $discount_coeffs = $this->gather_discount_coeffs_from_order( $order->id );
  300. if ( empty( $discount_coeffs ) ) {
  301. return $price;
  302. }
  303. @$coeff = $discount_coeffs[$actual_id]['coeff'];
  304. if ( !$coeff ) {
  305. return $price;
  306. }
  307. $discount_type = get_post_meta( $order->id, '_woocommerce_t4m_discount_type', true );
  308. if ( ( $discount_type == 'flat' && $coeff == 0 ) || ( $discount_type == '' && $coeff == 1.0 ) ) {
  309. return $price; // no price modification
  310. }
  311. $new_css = esc_attr( get_option( 'woocommerce_t4m_css_new_price', 'color: #4AB915; font-weight: bold;' ) );
  312. $bulk_info = sprintf( __( 'Incl. %s discount', 'wc_bulk_discount' ), ( $discount_type == 'flat' ? get_woocommerce_currency_symbol() . $coeff : ( round( ( 1 - $coeff ) * 100, 2 ) . "%" ) ) );
  313.  
  314. return "<span class='discount-info' title='$bulk_info'>" .
  315. "<span>$price</span>" .
  316. "<span class='new-price' style='$new_css'> ($bulk_info)</span></span>";
  317.  
  318. }
  319.  
  320. /**
  321. * Gather discount information from order.
  322. *
  323. * @param $order_id
  324. * @return array
  325. */
  326. protected function gather_discount_coeffs_from_order( $order_id ) {
  327.  
  328. $meta = get_post_meta( $order_id, '_woocommerce_t4m_discount_coeffs', true );
  329.  
  330. if ( !$meta ) {
  331. return null;
  332. }
  333.  
  334. $order_discount_coeffs = json_decode( $meta, true );
  335. return $order_discount_coeffs;
  336.  
  337. }
  338.  
  339. /**
  340. * Hook to woocommerce_before_calculate_totals action.
  341. *
  342. * @param WC_Cart $cart
  343. */
  344. public function action_before_calculate( WC_Cart $cart ) {
  345.  
  346. if ( $this->coupon_check() ) {
  347. return;
  348. }
  349.  
  350. if ($this->bulk_discount_calculated) {
  351. return;
  352. }
  353.  
  354. $this->gather_discount_coeffs();
  355.  
  356. if ( sizeof( $cart->cart_contents ) > 0 ) {
  357.  
  358. foreach ( $cart->cart_contents as $cart_item_key => $values ) {
  359. $_product = $values['data'];
  360. if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
  361. continue;
  362. }
  363. if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
  364. $row_base_price = max( 0, $_product->get_price() - ( $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] / $values['quantity'] ) );
  365. } else {
  366. $row_base_price = $_product->get_price() * $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
  367. }
  368.  
  369. $values['data']->set_price( $row_base_price );
  370. }
  371.  
  372. $this->bulk_discount_calculated = true;
  373.  
  374. }
  375.  
  376. }
  377.  
  378. public function filter_before_calculate( $res ) {
  379.  
  380. global $woocommerce;
  381.  
  382. if ($this->bulk_discount_calculated) {
  383. return $res;
  384. }
  385.  
  386. $cart = $woocommerce->cart;
  387.  
  388. if ( $this->coupon_check() ) {
  389. return $res;
  390. }
  391.  
  392. $this->gather_discount_coeffs();
  393.  
  394. if ( sizeof( $cart->cart_contents ) > 0 ) {
  395.  
  396. foreach ( $cart->cart_contents as $cart_item_key => $values ) {
  397. $_product = $values['data'];
  398. if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
  399. continue;
  400. }
  401. if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
  402. $row_base_price = max( 0, $_product->get_price() - ( $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'] / $values['quantity'] ) );
  403. } else {
  404. $row_base_price = $_product->get_price() * $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
  405. }
  406.  
  407. $values['data']->set_price( $row_base_price );
  408. }
  409.  
  410. $this->bulk_discount_calculated = true;
  411.  
  412. }
  413.  
  414. return $res;
  415.  
  416. }
  417.  
  418. /**
  419. * @param $product
  420. * @return int
  421. */
  422. protected function get_actual_id( $product ) {
  423.  
  424. if ( $product instanceof WC_Product_Variation ) {
  425. return $product->variation_id;
  426. } else {
  427. return $product->id;
  428. }
  429.  
  430. }
  431.  
  432. /**
  433. * Hook to woocommerce_calculate_totals.
  434. *
  435. * @param WC_Cart $cart
  436. */
  437. public function action_after_calculate( WC_Cart $cart ) {
  438.  
  439. if ( $this->coupon_check() ) {
  440. return;
  441. }
  442.  
  443. if ( sizeof( $cart->cart_contents ) > 0 ) {
  444. foreach ( $cart->cart_contents as $cart_item_key => $values ) {
  445. $_product = $values['data'];
  446. if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
  447. continue;
  448. }
  449. $values['data']->set_price( $this->discount_coeffs[$this->get_actual_id( $_product )]['orig_price'] );
  450. }
  451. }
  452.  
  453. }
  454.  
  455. /**
  456. * Show discount info in cart.
  457. */
  458. public function before_cart_table() {
  459.  
  460. if ( get_option( 'woocommerce_t4m_cart_info' ) != '' ) {
  461. echo "<div class='cart-show-discounts'>";
  462. echo get_option( 'woocommerce_t4m_cart_info' );
  463. echo "</div>";
  464. }
  465.  
  466. }
  467.  
  468. /**
  469. * Hook to woocommerce_cart_product_subtotal filter.
  470. *
  471. * @param $subtotal
  472. * @param $_product
  473. * @param $quantity
  474. * @param WC_Cart $cart
  475. * @return string
  476. */
  477. public function filter_cart_product_subtotal( $subtotal, $_product, $quantity ) {
  478.  
  479. if ( !$_product || !$quantity ) {
  480. return $subtotal;
  481. }
  482. if ( $this->coupon_check() ) {
  483. return $subtotal;
  484. }
  485. if ( get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) != '' && get_post_meta( $_product->id, "_bulkdiscount_enabled", true ) !== 'yes' ) {
  486. return $subtotal;
  487. }
  488.  
  489. $coeff = $this->discount_coeffs[$this->get_actual_id( $_product )]['coeff'];
  490. if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
  491. $newsubtotal = woocommerce_price( max( 0, ( $_product->get_price() * $quantity ) - $coeff ) );
  492. } else {
  493. $newsubtotal = woocommerce_price( $_product->get_price() * $quantity * $coeff );
  494. }
  495.  
  496. return $newsubtotal;
  497.  
  498. }
  499.  
  500. /**
  501. * Store discount info in order as well
  502. *
  503. * @param $order_id
  504. */
  505. public function order_update_meta( $order_id ) {
  506.  
  507. update_post_meta( $order_id, "_woocommerce_t4m_discount_type", get_option( 'woocommerce_t4m_discount_type', '' ) );
  508. update_post_meta( $order_id, "_woocommerce_t4m_discount_coeffs", json_encode( $this->discount_coeffs ) );
  509.  
  510. }
  511.  
  512. /**
  513. * Display discount information in Product Detail.
  514. */
  515. public function single_product_summary() {
  516.  
  517. global $thepostid, $post;
  518. if ( !$thepostid ) $thepostid = $post->ID;
  519.  
  520. echo "<div class='productinfo-show-discounts'>";
  521. echo get_post_meta( $thepostid, '_bulkdiscount_text_info', true );
  522. echo "</div>";
  523.  
  524. }
  525.  
  526. /**
  527. * Add entry to Product Settings.
  528. */
  529. public function action_product_write_panel_tabs() {
  530.  
  531. $style = '';
  532.  
  533. if ( version_compare( WOOCOMMERCE_VERSION, "2.1.0" ) >= 0 ) {
  534. $style = 'style = "padding: 10px !important"';
  535. }
  536.  
  537. echo '<li class="bulkdiscount_tab bulkdiscount_options"><a href="#bulkdiscount_product_data" '.$style.'>' . __( 'Bulk Discount', 'wc_bulk_discount' ) . '</a></li>';
  538.  
  539. }
  540.  
  541. /**
  542. * Add entry content to Product Settings.
  543. */
  544. public function action_product_write_panels() {
  545.  
  546. global $thepostid, $post;
  547.  
  548. if ( !$thepostid ) $thepostid = $post->ID;
  549. ?>
  550. <script type="text/javascript">
  551. jQuery( document ).ready( function () {
  552. var e = jQuery( '#bulkdiscount_product_data' );
  553. <?php
  554. for($i = 1; $i <= 11; $i++) :
  555. ?>
  556. e.find( '.block<?php echo $i; ?>' ).hide();
  557. e.find( '.options_group<?php echo max($i, 2); ?>' ).hide();
  558. e.find( '#add_discount_line<?php echo max($i, 2); ?>' ).hide();
  559. e.find( '#add_discount_line<?php echo $i; ?>' ).click( function () {
  560. if ( <?php echo $i; ?> == 1 || ( e.find( '#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>' ).val() != '' &&
  561. <?php if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) : ?>
  562. e.find( '#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>' ).val() != ''
  563. <?php else: ?>
  564. e.find( '#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>' ).val() != ''
  565. <?php endif; ?>
  566. ) )
  567. {
  568. e.find( '.block<?php echo $i; ?>' ).show( 400 );
  569. e.find( '.options_group<?php echo min($i+1, 11); ?>' ).show( 400 );
  570. e.find( '#add_discount_line<?php echo min($i+1, 10); ?>' ).show( 400 );
  571. e.find( '#add_discount_line<?php echo $i; ?>' ).hide( 400 );
  572. e.find( '#delete_discount_line<?php echo min($i+1, 11); ?>' ).show( 400 );
  573. e.find( '#delete_discount_line<?php echo $i; ?>' ).hide( 400 );
  574. }
  575. else
  576. {
  577. alert( '<?php _e( 'Please fill in the current line before adding new line.', 'wc_bulk_discount' ); ?>' );
  578. }
  579. } );
  580. e.find( '#delete_discount_line<?php echo max($i, 1); ?>' ).hide();
  581. e.find( '#delete_discount_line<?php echo $i; ?>' ).click( function () {
  582. e.find( '.block<?php echo max($i-1, 1); ?>' ).hide( 400 );
  583. e.find( '.options_group<?php echo min($i, 11); ?>' ).hide( 400 );
  584. e.find( '#add_discount_line<?php echo min($i, 10); ?>' ).hide( 400 );
  585. e.find( '#add_discount_line<?php echo max($i-1, 1); ?>' ).show( 400 );
  586. e.find( '#delete_discount_line<?php echo min($i, 11); ?>' ).hide( 400 );
  587. e.find( '#delete_discount_line<?php echo max($i-1, 2); ?>' ).show( 400 );
  588. e.find( '#_bulkdiscount_quantity_<?php echo max($i-1, 1); ?>' ).val( '' );
  589. <?php
  590. if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) :
  591. ?>
  592. e.find( '#_bulkdiscount_discount_flat_<?php echo max($i-1, 1); ?>' ).val( '' );
  593. <?php else: ?>
  594. e.find( '#_bulkdiscount_discount_<?php echo max($i-1, 1); ?>' ).val( '' );
  595. <?php endif; ?>
  596. } );
  597. <?php
  598. endfor;
  599. for ($i = 1, $j = 2; $i <= 10; $i++, $j++) {
  600. $cnt = 1;
  601. if (get_post_meta($thepostid, "_bulkdiscount_quantity_$i", true) || get_post_meta($thepostid, "_bulkdiscount_quantity_$j", true)) {
  602. ?>
  603. e.find( '.block<?php echo $i; ?>' ).show();
  604. e.find( '.options_group<?php echo $i; ?>' ).show();
  605. e.find( '#add_discount_line<?php echo $i; ?>' ).hide();
  606. e.find( '#delete_discount_line<?php echo $i; ?>' ).hide();
  607. e.find( '.options_group<?php echo min($i+1,11); ?>' ).show();
  608. e.find( '#add_discount_line<?php echo min($i+1,11); ?>' ).show();
  609. e.find( '#delete_discount_line<?php echo min($i+1,11); ?>' ).show();
  610. <?php
  611. $cnt++;
  612. }
  613. }
  614. if ($cnt >= 11) {
  615. ?>e.find( '#add_discount_line11' ).show();
  616. <?php
  617. }
  618. ?>
  619. } );
  620. </script>
  621.  
  622. <div id="bulkdiscount_product_data" class="panel woocommerce_options_panel">
  623.  
  624. <div class="options_group">
  625. <?php
  626. woocommerce_wp_checkbox( array( 'id' => '_bulkdiscount_enabled', 'value' => get_post_meta( $thepostid, '_bulkdiscount_enabled', true ) ? get_post_meta( $thepostid, '_bulkdiscount_enabled', true ) : 'yes', 'label' => __( 'Bulk Discount enabled', 'wc_bulk_discount' ) ) );
  627. woocommerce_wp_textarea_input( array( 'id' => "_bulkdiscount_text_info", 'label' => __( 'Bulk discount special offer text in product description', 'wc_bulk_discount' ), 'description' => __( 'Optionally enter bulk discount information that will be visible on the product page.', 'wc_bulk_discount' ), 'desc_tip' => 'yes', 'class' => 'fullWidth' ) );
  628. ?>
  629. </div>
  630.  
  631. <?php
  632. for ( $i = 1;
  633. $i <= 10;
  634. $i++ ) :
  635. ?>
  636.  
  637. <div class="options_group<?php echo $i; ?>">
  638. <a id="add_discount_line<?php echo $i; ?>" class="button-secondary"
  639. href="#block<?php echo $i; ?>"><?php _e( 'Add discount line', 'wc_bulk_discount' ); ?></a>
  640. <a id="delete_discount_line<?php echo $i; ?>" class="button-secondary"
  641. href="#block<?php echo $i; ?>"><?php _e( 'Remove last discount line', 'wc_bulk_discount' ); ?></a>
  642.  
  643. <div class="block<?php echo $i; ?> <?php echo ( $i % 2 == 0 ) ? 'even' : 'odd' ?>">
  644. <?php
  645. woocommerce_wp_text_input( array( 'id' => "_bulkdiscount_quantity_$i", 'label' => __( 'Quantity (min.)', 'wc_bulk_discount' ), 'type' => 'number', 'description' => __( 'Enter the minimal quantity for which the discount applies.', 'wc_bulk_discount' ), 'custom_attributes' => array(
  646. 'step' => '1',
  647. 'min' => '1'
  648. ) ) );
  649. if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) {
  650. woocommerce_wp_text_input( array( 'id' => "_bulkdiscount_discount_flat_$i", 'type' => 'number', 'label' => sprintf( __( 'Discount (%s)', 'wc_bulk_discount' ), get_woocommerce_currency_symbol() ), 'description' => sprintf( __( 'Enter the flat discount in %s.', 'wc_bulk_discount' ), get_woocommerce_currency_symbol() ), 'custom_attributes' => array(
  651. 'step' => 'any',
  652. 'min' => '0'
  653. ) ) );
  654. } else {
  655. woocommerce_wp_text_input( array( 'id' => "_bulkdiscount_discount_$i", 'type' => 'number', 'label' => __( 'Discount (%)', 'wc_bulk_discount' ), 'description' => __( 'Enter the discount in percents (Allowed values: 0 to 100).', 'wc_bulk_discount' ), 'custom_attributes' => array(
  656. 'step' => 'any',
  657. 'min' => '0',
  658. 'max' => '100'
  659. ) ) );
  660. }
  661. ?>
  662. </div>
  663. </div>
  664.  
  665. <?php
  666. endfor;
  667. ?>
  668.  
  669. <div class="options_group11">
  670. <a id="delete_discount_line11" class="button-secondary"
  671. href="#block11"><?php _e( 'Remove last discount line', 'wc_bulk_discount' ); ?></a>
  672. </div>
  673.  
  674. <br/>
  675.  
  676. </div>
  677.  
  678. <?php
  679. }
  680.  
  681. /**
  682. * Enqueue frontend dependencies.
  683. */
  684. public function action_enqueue_dependencies() {
  685.  
  686. wp_register_style( 'woocommercebulkdiscount-style', plugins_url( 'css/style.css', __FILE__ ) );
  687. wp_enqueue_style( 'woocommercebulkdiscount-style' );
  688. wp_enqueue_script( 'jquery' );
  689.  
  690. }
  691.  
  692. /**
  693. * Enqueue backend dependencies.
  694. */
  695. public function action_enqueue_dependencies_admin() {
  696.  
  697. wp_register_style( 'woocommercebulkdiscount-style-admin', plugins_url( 'css/admin.css', __FILE__ ) );
  698. wp_enqueue_style( 'woocommercebulkdiscount-style-admin' );
  699. wp_enqueue_script( 'jquery' );
  700.  
  701. }
  702.  
  703. /**
  704. * Updating post meta.
  705. *
  706. * @param $post_id
  707. */
  708. public function action_process_meta( $post_id ) {
  709.  
  710. if ( isset( $_POST['_bulkdiscount_text_info'] ) ) update_post_meta( $post_id, '_bulkdiscount_text_info', stripslashes( $_POST['_bulkdiscount_text_info'] ) );
  711.  
  712. if ( isset( $_POST['_bulkdiscount_enabled'] ) && $_POST['_bulkdiscount_enabled'] == 'yes' ) {
  713. update_post_meta( $post_id, '_bulkdiscount_enabled', stripslashes( $_POST['_bulkdiscount_enabled'] ) );
  714. } else {
  715. update_post_meta( $post_id, '_bulkdiscount_enabled', stripslashes( 'no' ) );
  716. }
  717.  
  718. for ( $i = 1; $i <= 10; $i++ ) {
  719. if ( isset( $_POST["_bulkdiscount_quantity_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_quantity_$i", stripslashes( $_POST["_bulkdiscount_quantity_$i"] ) );
  720. if ( ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ) {
  721. if ( isset( $_POST["_bulkdiscount_discount_flat_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_discount_flat_$i", stripslashes( $_POST["_bulkdiscount_discount_flat_$i"] ) );
  722. } else {
  723. if ( isset( $_POST["_bulkdiscount_discount_$i"] ) ) update_post_meta( $post_id, "_bulkdiscount_discount_$i", stripslashes( $_POST["_bulkdiscount_discount_$i"] ) );
  724. }
  725. }
  726.  
  727. }
  728.  
  729. /**
  730. * @access public
  731. * @return void
  732. */
  733. public function add_tab() {
  734.  
  735. $settings_slug = 'woocommerce';
  736.  
  737. if ( version_compare( WOOCOMMERCE_VERSION, "2.1.0" ) >= 0 ) {
  738.  
  739. $settings_slug = 'wc-settings';
  740.  
  741. }
  742.  
  743. foreach ( $this->settings_tabs as $name => $label ) {
  744. $class = 'nav-tab';
  745. if ( $this->current_tab == $name )
  746. $class .= ' nav-tab-active';
  747. echo '<a href="' . admin_url( 'admin.php?page=' . $settings_slug . '&tab=' . $name ) . '" class="' . $class . '">' . $label . '</a>';
  748. }
  749.  
  750. }
  751.  
  752. /**
  753. * @access public
  754. * @return void
  755. */
  756. public function settings_tab_action() {
  757.  
  758. global $woocommerce_settings;
  759.  
  760. // Determine the current tab in effect.
  761. $current_tab = $this->get_tab_in_view( current_filter(), 'woocommerce_settings_tabs_' );
  762.  
  763. do_action( 'woocommerce_bulk_discount_settings' );
  764.  
  765. // Display settings for this tab (make sure to add the settings to the tab).
  766. woocommerce_admin_fields( $woocommerce_settings[$current_tab] );
  767.  
  768. }
  769.  
  770. /**
  771. * Save settings in a single field in the database for each tab's fields (one field per tab).
  772. */
  773. public function save_settings() {
  774.  
  775. global $woocommerce_settings;
  776.  
  777. // Make sure our settings fields are recognised.
  778. $this->add_settings_fields();
  779.  
  780. $current_tab = $this->get_tab_in_view( current_filter(), 'woocommerce_update_options_' );
  781. woocommerce_update_options( $woocommerce_settings[$current_tab] );
  782.  
  783. }
  784.  
  785. /**
  786. * Get the tab current in view/processing.
  787. */
  788. public function get_tab_in_view( $current_filter, $filter_base ) {
  789.  
  790. return str_replace( $filter_base, '', $current_filter );
  791.  
  792. }
  793.  
  794.  
  795. /**
  796. * Add settings fields for each tab.
  797. */
  798. public function add_settings_fields() {
  799. global $woocommerce_settings;
  800.  
  801. // Load the prepared form fields.
  802. $this->init_form_fields();
  803.  
  804. if ( is_array( $this->fields ) )
  805. foreach ( $this->fields as $k => $v )
  806. $woocommerce_settings[$k] = $v;
  807. }
  808.  
  809. /**
  810. * Prepare form fields to be used in the various tabs.
  811. */
  812. public function init_form_fields() {
  813. global $woocommerce;
  814.  
  815. // Define settings
  816. $this->fields['bulk_discount'] = apply_filters( 'woocommerce_bulk_discount_settings_fields', array(
  817.  
  818. array( 'name' => __( 'Bulk Discount', 'wc_bulk_discount' ), 'type' => 'title', 'desc' => __( 'The following options are specific to product bulk discount.', 'wc_bulk_discount' ) . '<br /><br/><strong><i>' . __( 'After changing the settings, it is recommended to clear all sessions in WooCommerce &gt; System Status &gt; Tools.', 'wc_bulk_discount' ) . '</i></strong>', 'id' => 't4m_bulk_discounts_options' ),
  819.  
  820. array(
  821. 'name' => __( 'Bulk Discount globally enabled', 'wc_bulk_discount' ),
  822. 'id' => 'woocommerce_t4m_enable_bulk_discounts',
  823. 'desc' => __( '', 'wc_bulk_discount' ),
  824. 'std' => 'yes',
  825. 'type' => 'checkbox',
  826. 'default' => 'yes'
  827. ),
  828.  
  829. array(
  830. 'title' => __( 'Discount Type', 'wc_bulk_discount' ),
  831. 'id' => 'woocommerce_t4m_discount_type',
  832. 'desc' => sprintf( __( 'Select the type of discount. Percentage Discount deducts amount of %% from price while Flat Discount deducts fixed amount in %s', 'wc_bulk_discount' ), get_woocommerce_currency_symbol() ),
  833. 'desc_tip' => true,
  834. 'std' => 'yes',
  835. 'type' => 'select',
  836. 'css' => 'min-width:200px;',
  837. 'class' => 'chosen_select',
  838. 'options' => array(
  839. '' => __( 'Percentage Discount', 'wc_bulk_discount' ),
  840. 'flat' => __( 'Flat Discount', 'wc_bulk_discount' )
  841. )
  842. ),
  843.  
  844. array(
  845. 'name' => __( 'Treat product variations separately', 'wc_bulk_discount' ),
  846. 'id' => 'woocommerce_t4m_variations_separate',
  847. 'desc' => __( 'You need to have this option unchecked to apply discounts to variations by shared quantity.', 'wc_bulk_discount' ),
  848. 'std' => 'yes',
  849. 'type' => 'checkbox',
  850. 'default' => 'yes'
  851. ),
  852.  
  853. array(
  854. 'name' => __( 'Remove any bulk discounts if a coupon code is applied', 'wc_bulk_discount' ),
  855. 'id' => 'woocommerce_t4m_remove_discount_on_coupon',
  856. 'std' => 'yes',
  857. 'type' => 'checkbox',
  858. 'default' => 'yes'
  859. ),
  860.  
  861. array(
  862. 'name' => __( 'Show discount information next to cart item price', 'wc_bulk_discount' ),
  863. 'id' => 'woocommerce_t4m_show_on_item',
  864. 'desc' => __( 'Applies only to percentage discount.', 'wc_bulk_discount' ),
  865. 'std' => 'yes',
  866. 'type' => 'checkbox',
  867. 'default' => 'yes'
  868. ),
  869.  
  870. array(
  871. 'name' => __( 'Show discount information next to item subtotal price', 'wc_bulk_discount' ),
  872. 'id' => 'woocommerce_t4m_show_on_subtotal',
  873. 'std' => 'yes',
  874. 'type' => 'checkbox',
  875. 'default' => 'yes'
  876. ),
  877.  
  878. array(
  879. 'name' => __( 'Show discount information next to item subtotal price in order history', 'wc_bulk_discount' ),
  880. 'id' => 'woocommerce_t4m_show_on_order_subtotal',
  881. 'desc' => __( 'Includes showing discount in order e-mails and invoices.', 'wc_bulk_discount' ),
  882. 'std' => 'yes',
  883. 'type' => 'checkbox',
  884. 'default' => 'yes'
  885. ),
  886.  
  887. array(
  888. 'name' => __( 'Optionally enter information about discounts visible on cart page.', 'wc_bulk_discount' ),
  889. 'id' => 'woocommerce_t4m_cart_info',
  890. 'type' => 'textarea',
  891. 'css' => 'width:100%; height: 75px;'
  892. ),
  893.  
  894. array(
  895. 'name' => __( 'Optionally change the CSS for old price on cart before discounting.', 'wc_bulk_discount' ),
  896. 'id' => 'woocommerce_t4m_css_old_price',
  897. 'type' => 'textarea',
  898. 'css' => 'width:100%;',
  899. 'default' => 'color: #777; text-decoration: line-through; margin-right: 4px;'
  900. ),
  901.  
  902. array(
  903. 'name' => __( 'Optionally change the CSS for new price on cart after discounting.', 'wc_bulk_discount' ),
  904. 'id' => 'woocommerce_t4m_css_new_price',
  905. 'type' => 'textarea',
  906. 'css' => 'width:100%;',
  907. 'default' => 'color: #4AB915; font-weight: bold;'
  908. ),
  909.  
  910. array( 'type' => 'sectionend', 'id' => 't4m_bulk_discounts_options' ),
  911.  
  912. array(
  913. 'desc' => 'If you find the WooCommerce Bulk Discount extension useful, please rate it <a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/woocommerce-bulk-discount#postform">&#9733;&#9733;&#9733;&#9733;&#9733;</a>.',
  914. 'id' => 'woocommerce_t4m_bulk_discount_notice_text',
  915. 'type' => 'title'
  916. ),
  917.  
  918. array( 'type' => 'sectionend', 'id' => 'woocommerce_t4m_bulk_discount_notice_text' )
  919.  
  920. ) ); // End settings
  921.  
  922. $js = "
  923. jQuery('#woocommerce_t4m_enable_bulk_discounts').change(function() {
  924.  
  925. jQuery('#woocommerce_t4m_cart_info, #woocommerce_t4m_variations_separate, #woocommerce_t4m_discount_type, #woocommerce_t4m_css_old_price, #woocommerce_t4m_css_new_price, #woocommerce_t4m_show_on_item, #woocommerce_t4m_show_on_subtotal, #woocommerce_t4m_show_on_order_subtotal').closest('tr').hide();
  926.  
  927. if ( jQuery(this).attr('checked') ) {
  928. jQuery('#woocommerce_t4m_cart_info').closest('tr').show();
  929. jQuery('#woocommerce_t4m_variations_separate').closest('tr').show();
  930. jQuery('#woocommerce_t4m_discount_type').closest('tr').show();
  931. jQuery('#woocommerce_t4m_css_old_price').closest('tr').show();
  932. jQuery('#woocommerce_t4m_css_new_price').closest('tr').show();
  933. jQuery('#woocommerce_t4m_show_on_item').closest('tr').show();
  934. jQuery('#woocommerce_t4m_show_on_subtotal').closest('tr').show();
  935. jQuery('#woocommerce_t4m_show_on_order_subtotal').closest('tr').show();
  936. }
  937.  
  938. }).change();
  939.  
  940. ";
  941.  
  942. $this->run_js( $js );
  943.  
  944. }
  945.  
  946. /**
  947. * Includes inline JavaScript.
  948. *
  949. * @param $js
  950. */
  951. protected function run_js( $js ) {
  952.  
  953. global $woocommerce;
  954.  
  955. if ( function_exists( 'wc_enqueue_js' ) ) {
  956. wc_enqueue_js( $js );
  957. } else {
  958. $woocommerce->add_inline_js( $js );
  959. }
  960.  
  961. }
  962.  
  963. /**
  964. * @return bool
  965. */
  966. protected function coupon_check() {
  967.  
  968. global $woocommerce;
  969.  
  970. if ( get_option( 'woocommerce_t4m_remove_discount_on_coupon', 'yes' ) == 'no' ) return false;
  971. return !( empty( $woocommerce->cart->applied_coupons ) );
  972. }
  973.  
  974. }
  975.  
  976. new Woo_Bulk_Discount_Plugin_t4m();
  977.  
  978. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement