Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add_action ( 'woocommerce_checkout_order_processed', 'add_facilitator_commission' );
/**
* Record a product referral when a new order has been saved.
* @param int $order_id
*/
function add_facilitator_commission( $order_id ) {
$product_options = array();
// You need to replicate this code for each product. In this case, 10 is the product ID
$product_options['10'] = array(
'rate' => 0.1,
'affiliate_id' => 4
);
$order_total = get_post_meta( $order_id, '_order_total', true );
$order_tax = get_post_meta( $order_id, '_order_tax', true );
$order_shipping = get_post_meta( $order_id, '_order_shipping', true );
$order_shipping_tax = get_post_meta( $order_id, '_order_shipping_tax', true );
$order_subtotal = $order_total - $order_tax - $order_shipping - $order_shipping_tax;
$currency = get_option( 'woocommerce_currency' );
$order_link = '<a href="' . admin_url( 'post.php?post=' . $order_id . '&action=edit' ) . '">';
$order_link .= sprintf( __( 'Order #%s', AFFILIATES_PRODUCTS_PLUGIN_DOMAIN ), $order_id );
$order_link .= "</a>";
if ( $order = Affiliates_Products_WooCommerce::get_order( $order_id ) ) {
$items = $order->get_items();
$nets = Affiliates_Products_WooCommerce::get_net_item_totals( $order_id );
foreach( $items as $order_item_id => $item ) {
$product = $order->get_product_from_item( $item );
// check if it's assigned to an affiliate
if ( $product->exists() && isset( $product_options[$product->id] ) ) {
$product_id = $product->id;
if ( isset( $product_options[$product_id]['affiliate_id'] ) ) {
$affiliate_id = $product_options[$product_id]['affiliate_id'];
$rate = isset( $product_options[$product_id]['rate'] ) ? $product_options[$product_id]['rate'] : null;
if ( $rate && affiliates_check_affiliate_id( $affiliate_id ) ) {
// get the quantity and calculate the product subtotal
$product_price = $order->get_item_total( $item );
$quantity = $item['qty'];
if ( isset( $nets[$order_item_id] ) ) {
$product_subtotal = $nets[$order_item_id];
} else {
$product_subtotal = $order->get_line_total( $item );
}
if ( $product_subtotal > 0 ) {
$commission = bcmul( $product_subtotal, $rate, AFFILIATES_REFERRAL_AMOUNT_DECIMALS );
$product_description = $product->get_title();
// store a referral
$data = array(
'order_id' => array(
'title' => 'Order ID',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $order_id )
),
'order_total' => array(
'title' => 'Total',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $order_subtotal )
),
'order_currency' => array(
'title' => 'Currency',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $currency )
),
'order_link' => array(
'title' => 'Order',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $order_link )
),
'product_id' => array(
'title' => 'Product ID',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $product_id )
),
'product_description' => array(
'title' => 'Product Description',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $product_description )
),
'product_price' => array(
'title' => 'Product Price',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $product_price )
),
'product_quantity' => array(
'title' => 'Product Quantity',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $quantity )
),
'product_subtotal' => array(
'title' => 'Product Subtotal',
'domain' => AFFILIATES_PRODUCTS_PLUGIN_DOMAIN,
'value' => esc_sql( $product_subtotal )
),
);
$post_id = $order_id;
$description = sprintf( '%s (Order #%s, Product #%s)', $product_description, $order_id, $product_id );
if ( class_exists( 'Affiliates_Referral_WordPress' ) ) {
$r = new Affiliates_Referral_WordPress();
$r->add_referrals( array( $affiliate_id ), $post_id, $description, $data, $product_subtotal, $commission, $currency, null, 'product', $order_id );
} else {
Affiliates_Products_WooCommerce::add_referral( $affiliate_id, $post_id, $description, $data, $commission, $currency );
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.