Wholesale For WooCommerce , by wpexperts.io
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 |
add_filter( 'wpo_get_item_by_product', function($item, $item_data,$product){ $user_id = $_REQUEST["cart"]["customer"]["id"]; if( !$user_id ) return $item; // customer not selected $data = $product->get_meta("wholesale_multi_user_pricing"); if( !$data ) return $item; // no wholesale prices at all $role_id = get_current_user_role_id(); if( !isset($data[$role_id]) ) return $item; // no wholesale prices for user role $rule = $data[ $role_id ]; //variation ?? if( isset($rule[$product->get_id()]) ) { $rule['wholesale_price'] = $rule[$product->get_id()]['wholesaleprice']; $rule['qty'] = $rule[$product->get_id()]['qty']; } if($rule["discount_type"] == "fixed") { $item["item_cost"] = $rule["wholesale_price"]; } else {// %% ? $price = $product->get_price(); $item["item_cost"] = round( $price * (100-$rule["wholesale_price"])/100, 2); } return $item; },10,3); add_filter("wpo_prepare_item", function($item,$product){ if( @$item['cost_updated_manually'] ) return $item;// edited via UI ? // set default cost $item["item_cost"] = $item["wpo_item_discount"]["discounted_price"] = $item["wpo_item_discount"]["original_price"] = $product->get_price(); $item["wpo_item_discount"]["discount"] = 0; // no discount! $user_id = $_REQUEST["cart"]["customer"]["id"]; if( !$user_id ) return $item; // customer not selected $data = $product->get_meta("wholesale_multi_user_pricing"); if( !$data ) return $item; // no wholesale prices at all $role_id = get_current_user_role_id(); if( !isset($data[$role_id]) ) return $item; // no wholesale prices for user role $rule = $data[ $role_id ]; //variation ?? if( isset($rule[$product->get_id()]) ) { $rule['wholesale_price'] = $rule[$product->get_id()]['wholesaleprice']; $rule['qty'] = $rule[$product->get_id()]['qty']; } if( isset ($rule['qty']) AND ($item['qty'] < $rule['qty']) ) return $item; // too few items $item["wpo_item_discount"]["discount_type"] = $rule["discount_type"]; //calc it once if($rule["discount_type"] == "fixed") { $item["wpo_item_discount"]["discount"] = round($item["wpo_item_discount"]["original_price"] - $rule["wholesale_price"],2); $item["item_cost"] = $item["wpo_item_discount"]["discounted_price"] = $rule["wholesale_price"]; } else {// %% ? $item["wpo_item_discount"]["discount"] = $discount = round($item["wpo_item_discount"]["original_price"] * $rule["wholesale_price"]/100,2); $item["item_cost"] = $item["wpo_item_discount"]["discounted_price"] = $item["wpo_item_discount"]["original_price"] - $discount; } return $item; },10,2); |