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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Product_Simple' ) ) {
return;
}
class WC_Product_Braintree_Variable_Subscription extends WC_Product_Variable {
public static function init() {
add_filter( 'woocommerce_data_stores', __CLASS__ . '::add_data_store' );
}
public function __construct( $product ) {
$this->extra_data['min_subscription_price'] = 0;
$this->extra_data['max_subscription_price'] = 0;
$this->extra_data['min_subscription_price_variation_id'] = 0;
$this->extra_data['max_subscription_price_variation_id'] = 0;
$this->extra_data['subscription_one_time_shipping'] = '';
parent::__construct( $product );
$this->subscription_period = 'month';
$this->post = get_post( $this->id );
}
public static function add_data_store( $stores ) {
$stores['product-braintree-variable-subscription'] = 'WC_Product_Variable_Data_Store_CPT';
return $stores;
}
public function __get( $key ) {
return $this->get_prop( $key );
}
public function __set( $key, $value ) {
$this->set_prop( $key, $value );
}
public function get_price_html( $price = '' ) {
if ( $this->sync_required() ) {
self::sync_product( $this );
}
if ( $this->min_subscription_price_variation_id !== $this->max_subscription_price_variation_id ) {
$low_variation = $this->get_child( $this->min_subscription_price_variation_id );
$high_variation = $this->get_child( $this->max_subscription_price_variation_id );
$text = sprintf( __( 'From %1$s to %2$s', 'woo-payment-gateway' ), $low_variation->get_price_html(), $high_variation->get_price_html() );
} else {
$variation = $this->get_child( $this->min_subscription_price_variation_id );
$text = $variation->get_price_html();
}
return $text;
}
public function get_available_variation( $variation ) {
$variation_data = parent::get_available_variation( $variation );
if ( empty( $variation_data['price_html'] ) ) {
$variation_data['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
}
return $variation_data;
}
public function get_child( $child_id ) {
return new WC_Product_Braintree_Subscription_Variation( $child_id );
}
public function get_variation_subscription_prices() {
$min_price = $this->min_subscription_price;
$max_price = $this->max_subscription_price;
if ( ! $min_price || ! $max_price ) {
self::sync_product( $this->id );
}
return array(
'min' => $min_price,
'max' => $max_price,
);
}
public function get_plans() {
$var = 'braintree_' . wc_braintree_environment() . '_plans';
return $this->$var;
}
public function get_signup_fee() {
return $this->subscription_sign_up_fee ? $this->subscription_sign_up_fee : 0;
}
public function is_one_time_shipping() {
return $this->needs_shipping() && $this->subscription_one_time_shipping === 'yes' && ! $this->subscription_trial_length;
}
public static function sync_product( $product, $save = true ) {
$product = new self( $product );
$children = get_posts(
array(
'post_parent' => $product->get_id(),
'posts_per_page' => - 1,
'post_type' => 'product_variation',
'post_status' => 'publish',
)
);
if ( ! $children ) {
return;
}
$price_types = array( 'subscription_price' );
foreach ( $price_types as $price_type ) {
$min_price_type = null;
$max_price_type = null;
$min_price_type_variation_id = null;
$max_price_type_variation_id = null;
foreach ( $children as $child ) {
$child_price = get_post_meta( $child->ID, '_' . $price_type, true );
if ( is_null( $min_price_type ) || $min_price_type > $child_price ) {
$min_price_type = $child_price;
$min_price_type_variation_id = $child->ID;
}
if ( is_null( $max_price_type ) || $max_price_type < $child_price ) {
$max_price_type = $child_price;
$max_price_type_variation_id = $child->ID;
}
}
$product->{"set_min_$price_type"}( $min_price_type );
$product->{"set_max_$price_type"}( $max_price_type );
$product->{"set_min_{$price_type}_variation_id"}( $min_price_type_variation_id );
$product->{"set_max_{$price_type}_variation_id"}( $max_price_type_variation_id );
$product->save();
}
}
public function sync_required() {
return ! $this->min_subscription_price || ! $this->max_subscription_price;
}
public function add_to_cart_handler( $product_type, $product ) {
if ( $this->get_type() === $product_type ) {
$product_type = 'variable';
}
return $product_type;
}
public function get_type() {
return 'braintree-variable-subscription';
}
public function get_product_type() {
return $this->product_type;
}
public function set_min_subscription_price( $price ) {
$this->min_subscription_price = $price;
}
public function set_max_subscription_price( $price ) {
$this->max_subscription_price = $price;
}
public function set_min_subscription_price_variation_id( $id ) {
$this->min_subscription_price_variation_id = $id;
}
public function set_max_subscription_price_variation_id( $id ) {
$this->max_subscription_price_variation_id = $id;
}
public function set_subscription_one_time_shipping( $one_time_shipping ) {
$this->subscription_one_time_shipping = $one_time_shipping;
}
public function get_min_subscription_price() {
return $this->min_subscription_price;
}
public function get_max_subscription_price() {
return $this->max_subscription_price;
}
public function get_min_subscription_price_variation_id() {
return $this->min_subscription_price_variation_id;
}
public function get_max_subscription_price_variation_id() {
return $this->max_subscription_price_variation_id;
}
public function get_subscription_one_time_shipping() {
return $this->subscription_one_time_shipping;
}
}
WC_Product_Braintree_Variable_Subscription::init();