Skip to content

Instantly share code, notes, and snippets.

@ramiabraham
Last active June 7, 2016 00:22
Show Gist options
  • Save ramiabraham/15d3d2d19ccd252f87f5a5f0ca4d7c94 to your computer and use it in GitHub Desktop.
Save ramiabraham/15d3d2d19ccd252f87f5a5f0ca4d7c94 to your computer and use it in GitHub Desktop.
Adjusts the referral amount based on whether the product is virtual or not.
<?php
/**
* Plugin Name: AffiliateWP - Modify Referral If Virtual Product (WC)
* Plugin URI: https://affiliatewp.com
* Description: Modify referral rate
* Author: AffiliateWP Team
* Author URI: https://affiliatewp.com
* Version: 1.0
*/
function prefix_affwp_modify_referral_if_virtual( $referral_amount, $affiliate_id, $amount, $reference, $product_id ) {
$new_desired_amount = '10';
global $product;
if( $product->product_type == 'variable' ) :
// Get product variations
$variations = $product->get_available_variations();
// Identifying that all variations are virtual
$is_virtual = array();
foreach ( $variations as $v ){
$is_virtual[] = $v['is_virtual'];
}
$is_virtual = array_unique($is_virtual);
$all_variations_are_virtual = false;
if (count($is_virtual) === 1 && end($is_virtual) === true) {
$all_variations_are_virtual = true;
}
// Check if current product type is variable and all variations are virtual
if( $product->product_type == 'variable' && $all_variations_are_virtual == true ){
// It's a virtual product in WooCommerce
$amount = $new_desired_amount;
}
endif;
}
add_filter( 'affwp_calc_referral_amount', 'prefix_affwp_modify_referral_if_virtual', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment