WooThemes “Product Vendors” Error and Patch

Update: The below still also applies up through “Product Vendors” v1.1.11 as the developer still has not patched the error.  (I notified and had a lengthy e-mail conversation with WooThemes support and also sent them the code patch below on 2015-02-22)

Issue: Using the WooThemes “Product Vendors” v1.1.8 When I manually enter an order for a product with more than one associated vendor, only the first vendor in the list is getting the commission applied to their account.

Solution: So I’ve tracked down and made a patch to fix this issue in the “Product Vendors” woocommerce extension of items with multiple vendors not setting commissions correctly:

In “class-woocommerce-product-vendors.php”, lines ~926 through ~963, in the “record_commission(…)” function, the function is forced to return after processing only the first vendor in the product vendor list. They setup the loop to iterate through all vendors attached to the product, but force a return after just processing the first.

I’ve made a code patch as shown below that addresses the issue and have been testing it on my development site. I haven’t seen any issues so far.

Old Code:

/**
* Record individual commission
* @param int $product_id ID of product for commission
* @param int $line_total Line total of product
* @param int $discount Total discount applied to order
* @param int $order_id ID of order
* @return void
*/
public function record_commission( $product_id = 0, $line_total = 0, $discount = 0, $order_id = 0 ) {

if( $product_id > 0 && $line_total > 0 ) {
$vendors = get_product_vendors( $product_id );

if( $vendors ) {
foreach( $vendors as $vendor ) {

// Get percentage for commission
$commission = (float) get_commission_percent( $product_id, $vendor->ID );

// If commission is 0% then go no further
if( ! $commission ) continue;

// Subtract total discount for this line item
$discounted_total = (float) $line_total – $discount;

// If total is 0 after discounts then go no further
if( ! $discounted_total ) continue;

// Get total amount for commission
$amount = (float) $discounted_total * ( $commission / 100 );

// If commission amount is 0 then go no further
if( ! $amount ) continue;

// Create new commission
$this->create_commission( $vendor->ID, $product_id, $amount, $order_id );

// Return commission total
return $amount;
}
}
}

New Tweaked Version:

/**
* Record individual commission
* @param int $product_id ID of product for commission
* @param int $line_total Line total of product
* @param int $discount Total discount applied to order
* @param int $order_id ID of order
*
* Modified 2015/2/22 by Jonathan Lareau in order to have products with multiple vendors processed correctly
*
* @return void
*/
public function record_commission( $product_id = 0, $line_total = 0, $discount = 0, $order_id = 0 ) {

if( $product_id > 0 && $line_total > 0 ) {
$vendors = get_product_vendors( $product_id );

$total = 0;
if( $vendors ) {
foreach( $vendors as $vendor ) {

// Get percentage for commission
$commission = (float) get_commission_percent( $product_id, $vendor->ID );

// If commission is 0% then go no further
if( ! $commission ) continue;

// Subtract total discount for this line item
$discounted_total = (float) $line_total – $discount;

// If total is 0 after discounts then go no further
if( ! $discounted_total ) continue;

// Get total amount for commission
$amount = (float) $discounted_total * ( $commission / 100 );

// If commission amount is 0 then go no further
if( ! $amount ) continue;

// Create new commission
$this->create_commission( $vendor->ID, $product_id, $amount, $order_id );

// Return commission total
$total += $amount;
}
}
return $total;
}

Leave a Reply