Forked from gabrielmerovingi/myCRED Pay on Completed or Shipped Orders
Created
February 19, 2019 12:05
-
-
Save tharlab/e75300bf4e5d1fa6d54c9188cfc913f2 to your computer and use it in GitHub Desktop.
By default, myCRED pays out profit shares of WooCommerce orders when the order has been paid. This is an example of how to move the payout process so it's payed out when the order has been marked as "Completed" or "Shipped". Requires the myCRED Gateways "Profit Sharing" value to be set to zero!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pay Sale Profit on Completed or Shipped Orders | |
* @version 1.0 | |
*/ | |
add_action( 'woocommerce_order_status_completed', 'mycred_pro_pay_profit_from_sale' ); | |
add_action( 'woocommerce_order_status_shipped', 'mycred_pro_pay_profit_from_sale' ); | |
function mycred_pro_pay_profit_from_sale( $order_id ) { | |
// Get current user id | |
$cui = get_current_user_id(); | |
// Get gateway settings | |
$settings = get_option( 'woocommerce_mycred_settings', false ); | |
if ( ! isset( $settings['point_type'] ) ) return; | |
// Grab Order | |
$order = new WC_Order( $order_id ); | |
// Get the point type (if multiple point types are used) | |
if ( $settings['point_type'] === NULL || $settings['point_type'] == '' ) | |
$settings['point_type'] = 'mycred_default'; | |
// Load myCRED | |
$mycred = mycred( $settings['point_type'] ); | |
// Get Cost | |
$currency = get_woocommerce_currency(); | |
if ( $currency != 'MYC' ) | |
$cost = $mycred->apply_exchange_rate( $order->order_total, $settings['exchange_rate'] ); | |
else | |
$cost = $order->order_total; | |
$cost = apply_filters( 'mycred_woo_order_cost', $cost, $order, false, $this ); | |
// Profit Share | |
// Change this to your own value or get this value from anywhere | |
// you might have it saved. Make sure the value is properly formatted! | |
// Example: 90% - 90% of the point cost of each item in the order is paid to the | |
// product author (owner). | |
$set_share = 90; | |
// Get Items | |
$items = $order->get_items(); | |
// Loop though items | |
foreach ( $items as $item ) { | |
// Get Product | |
$product = get_post( (int) $item['product_id'] ); | |
// Continue if product has just been deleted or owner is buyer | |
if ( $product === NULL || $product->post_author == $cui ) continue; | |
// Calculate Share | |
// by: Leonie Heinle | |
$share = ( $set_share / 100 ) * $item['line_total']; | |
// Payout | |
$mycred->add_creds( | |
'store_sale', | |
$product->post_author, | |
$mycred->number( $share ), | |
$settings['profit_sharing_log'], | |
$product->ID, | |
array( 'ref_type' => 'post' ), | |
$settings['point_type'] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment