-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-dynamic-pricing-display-table.php
83 lines (72 loc) · 2.43 KB
/
woocommerce-dynamic-pricing-display-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
Plugin Name: WooCommerce Dynamic Pricing Display Table
Plugin URI: https://www.joellisenby.com/woocommerce-dynamic-pricing-display-table
Description: Display a quantity discount pricing table and update price based on discounts when quantity adjusted on single product pages while using the official WooCommerce Dynamic Pricing plugin.
Version: 20170919
Author: Joel Lisenby
Author URI: https://www.joellisenby.com/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wcdpdt
*/
class WooCommerceDynamicPricingDisplayTable {
private $version = 1;
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
}
public function wp_enqueue_scripts() {
if(is_product()) {
wp_register_style( 'wcdpdt', plugin_dir_url( __FILE__ ) . '/woocommerce-dynamic-pricing-display-table.css', null, $this->version, false );
wp_register_script( 'wcdpdt', plugin_dir_url( __FILE__ ) . '/woocommerce-dynamic-pricing-display-table.js', array( 'jquery' ), $this->version, true);
wp_localize_script( 'wcdpdt', 'wcdpdt_wc_product_pricing', $this->get_product_pricing() );
wp_localize_script( 'wcdpdt', 'wcdpdt_wc_price_num_decimals', get_option( 'woocommerce_price_num_decimals' ) );
wp_enqueue_script( 'wcdpdt' );
wp_enqueue_style( 'wcdpdt' );
}
}
public function get_product_pricing() {
$has_rules = false;
$pricing_rules = array();
$product = get_post_custom();
// Product pricing rules
$rules = unserialize( $product['_pricing_rules'][0] );
// Global pricing rules
if(empty($rules)) {
$product_cats = get_the_terms( $post->ID, 'product_cat' );
$product_cats_ids = array();
foreach($product_cats as $cat) {
$product_cats_ids[] = $cat->term_id;
}
$rules = get_option( '_a_category_pricing_rules' );
foreach($rules as $rule) {
switch($rule['collector']['type']) {
case 'cat_product':
case 'cat':
foreach($rule['collector']['args']['cats'] as $arg) {
if(in_array($arg, $product_cats_ids)) {
$has_rules = true;
}
}
break;
}
}
} else {
$has_rules = true;
}
if($has_rules) {
foreach($rules as $rule) {
$pricing_rules[] = $rule['rules'];
}
$pricing = array(
'prices' => $product['_price'],
'rules' => $pricing_rules
);
return $pricing;
} else {
return $global_rules;
}
}
}
new WooCommerceDynamicPricingDisplayTable();
?>