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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
<?php
class DFW_Manage_Cart {
public function __construct(){
add_action( 'woocommerce_before_add_to_cart_button' ,array( &$this, 'dfw_display_deposits_form' ), 99 );
add_filter( 'woocommerce_add_cart_item_data' ,array( &$this, 'dfw_add_cart_item_data' ), 10, 2 );
add_filter( 'woocommerce_add_cart_item' ,array( &$this, 'dfw_add_cart_item' ), 99, 1 );
add_filter( 'woocommerce_get_cart_item_from_session' ,array( $this, 'dfw_get_cart_item_from_session' ), 99, 3 );
add_filter( 'woocommerce_get_item_data' ,array( &$this, 'dfw_get_product_data' ), 10, 2 );
add_filter( 'woocommerce_cart_item_price' ,array( &$this, 'dfw_get_item_price' ), 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal' ,array( &$this, 'dfw_display_item_subtotal'), 10, 3);
add_action( 'woocommerce_cart_totals_before_order_total' ,array( &$this, 'dfw_display_cart_totals_before' ), 1 );
add_action( 'woocommerce_review_order_before_order_total' ,array( &$this, 'dfw_display_cart_totals_before' ), 99 );
add_action( 'woocommerce_new_order_item' ,array( &$this, 'dfw_new_order_item' ), 50, 3 );
}
public function dfw_display_deposits_form(){
$product_id = $GLOBALS['post']->ID;
if( DFW_Manage_Products::dfw_is_deposits_enabled( $product_id ) ){
wc_get_template( 'deposit-form.php', array( 'post' => $GLOBALS['post'] ), 'deposits-for-woocommerce', plugin_dir_path(__FILE__) .'frontend/' );
}
}
public function dfw_add_cart_item_data( $cart_item_meta, $product_id ){
if( ! DFW_Manage_Products::dfw_is_deposits_enabled( $product_id ) ){
return $cart_item_meta;
}
$plans = array();
$type = DFW_Manage_Products::dfw_get_deposit_type( $product_id );
if( 'product_payment_plan' == $type ) {
$plans = DFW_Manage_Plans::get_plans_for_product( $product_id );
}else if( 'payment_plan' == $type ){
$plans = DFW_Manage_Plans::get_default_plans();
}else {
$plans = false;
}
$plan_id = false;
if( ! empty( $plans ) ) {
$plan_id = $plans[0]->get_plan_id();
}
$wc_deposit_option = isset( $_POST[ 'wcd_option' ] ) ? sanitize_text_field( $_POST[ 'wcd_option' ] ) : 'enabled' ;
$wc_deposit_payment_plan = isset( $_POST['wcd_plan']) ? sanitize_text_field( $_POST[ 'wcd_plan'] ) : $plan_id;
if( 'enabled' === $wc_deposit_option || DFW_Manage_Products::dfw_is_deposits_forced( $product_id) ) {
$cart_item_meta[ 'has_deposit' ] = true;
if( 'product_payment_plan' === DFW_Manage_Products::dfw_get_deposit_type( $product_id ) || 'payment_plan' === DFW_Manage_Products::dfw_get_deposit_type( $product_id ) ) {
$cart_item_meta[ 'payment_plan' ] = $wc_deposit_payment_plan;
}
$product = wc_get_product( $product_id );
$cart_item_meta[ 'full_amount' ] = $product->get_price();
}
return $cart_item_meta;
}
public function dfw_get_cart_item_from_session( $cart_item, $values, $cart_item_key ) {
$cart_item['has_deposit'] = ! empty( $values['has_deposit'] );
$cart_item['payment_plan'] = ! empty( $values['payment_plan'] ) ? absint( $values['payment_plan'] ) : 0;
return $this->dfw_add_cart_item( $cart_item );
}
public function dfw_add_cart_item( $cart_item ) {
$product_id = $cart_item[ 'data' ]->get_id();
if( !empty ( $cart_item[ 'has_deposit' ] ) ){
$deposit = DFW_Manage_Products::dfw_get_deposit( $cart_item['data'], ! empty( $cart_item['payment_plan'] ) ? $cart_item['payment_plan'] : 0 );
$cart_item[ 'deposit' ] = $deposit;
if( !empty ( $cart_item['payment_plan'] ) ){
$plan = DFW_Manage_Plans::get_plan( $cart_item['payment_plan'] );
$total_percent = $plan->get_total_percent();
$cart_item['full_amount'] = ( $cart_item['data']->get_price() / 100 ) * $total_percent;
}else {
$cart_item['full_amount'] = $cart_item['data']->get_price();
}
if( false == $cart_item[ 'deposit' ] ) {
$cart_item[ 'deposit' ] = $cart_item['full_amount'];
}
$cart_item[ 'data' ]->set_price( $cart_item[ 'deposit' ] );
}
return $cart_item;
}
public function dfw_get_product_data( $other_data, $cart_item){
if( ! empty( $cart_item['payment_plan'] ) ){
$plan = DFW_Manage_Plans::get_plan( $cart_item['payment_plan'] );
$other_data[] = array(
'name' => __( 'Payment Plan', 'deposits-for-woocommerce' ),
'value' => $plan->get_plan_name(),
'display' => ''
);
}
return $other_data;
}
public function dfw_get_item_price( $output, $cart_item, $cart_item_key ) {
if( !empty ( $cart_item[ 'has_deposit' ] ) ) {
$product_data = $cart_item[ 'data' ];
$amount = $cart_item['full_amount'];
$output = wc_price( $amount );
}
return $output;
}
public function dfw_display_item_subtotal( $output, $cart_item, $cart_item_key ){
if ( ! isset( $cart_item['full_amount'] ) ) {
return $output;
}
if( !empty ( $cart_item[ 'has_deposit' ] ) ) {
$product_data = $cart_item[ 'data' ];
$quantity = $cart_item[ 'quantity' ];
$total_deposit = $cart_item[ 'deposit' ] * $quantity;
$full_amount = $quantity * ( $cart_item['full_amount'] );
if( ! empty( $cart_item['payment_plan'] ) ){
$plan = new DFW_Deposits_Plan( $cart_item['payment_plan'] );
$output .= '<br/><small>'. $plan->get_formatted_plan_schedule( $full_amount );
}else {
$output .= '<br/><small>'. wc_price( $full_amount ) .' payable in total </small>';
}
}
return $output;
}
public function dfw_display_cart_totals_before(){
$future_payment_amount = self::dfw_get_future_payments();
global $woocommerce;
$is_deposits_enabled = false;
foreach( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$product_id = $_product->get_id();
if( DFW_Manage_Products::dfw_is_deposits_enabled( $product_id ) ) {
$is_deposits_enabled = true;
}
}
?>
<?php if( $is_deposits_enabled ) { ?>
<tr class="order-total">
<th><?php _e( 'Payable Today', 'deposits-for-woocommerce' ); ?></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<tr class="order-total">
<th><?php _e( 'Future Payments', 'deposits-for-woocommerce' ); ?></th>
<td><strong><?php echo wc_price( $future_payment_amount ) ; ?></strong></td>
</tr>
<?php }
}
public function dfw_get_remaining_amount() {
$remaining_amount = 0;
foreach( WC()->cart->get_cart() as $cart_item ) {
if( !empty ( $cart_item[ 'has_deposit' ] ) ) {
$product_data = $cart_item[ 'data' ];
$quantity = $cart_item[ 'quantity' ];
$remaining_amount += $quantity * ( $cart_item[ 'full_amount' ] - $cart_item[ 'deposit' ] );
}
}
return $remaining_amount;
}
public function dfw_get_future_payments(){
return $this->dfw_get_remaining_amount();
}
public function dfw_add_order_item_meta( $item_id, $cart_item ) {
if( ! empty( $cart_item['has_deposit'] ) ) {
$quantity = $cart_item[ 'quantity' ];
$total = $quantity * $cart_item['full_amount'] ;
wc_add_order_item_meta( $item_id, '_has_deposit', 'enabled' );
wc_add_order_item_meta( $item_id, '_full_amount', $total );
if ( ! empty( $values['payment_plan'] ) ) {
wc_add_order_item_meta( $item_id, '_payment_plan', $values['payment_plan'] );
}
}
}
public function dfw_new_order_item( $item_id, $item, $order_id ){
if( ! empty( $item->legacy_values['has_deposit'] ) ) {
$quantity = $item->legacy_values[ 'quantity' ];
$total = $quantity * $item->legacy_values['full_amount'] ;
wc_add_order_item_meta( $item_id, '_has_deposit', 'enabled' );
wc_add_order_item_meta( $item_id, '_full_amount', $total );
if ( ! empty( $item->legacy_values['payment_plan'] ) ) {
wc_add_order_item_meta( $item_id, '_payment_plan', $item->legacy_values['payment_plan'] );
}
}
}
}
$dfw_manage_cart = new DFW_Manage_Cart();