From 10593a23db09ee77ae8b1fef4736c7f963a0bdd7 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Sat, 2 Sep 2017 14:27:05 +0200
Subject: [PATCH] Support for WooCommerce Product Vendors >=2.0

---
 .../rules_shipping_framework_woocommerce.php  | 24 ++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/includes/rules_shipping_framework_woocommerce.php b/includes/rules_shipping_framework_woocommerce.php
index 5d8edde..bd7da51 100644
--- a/includes/rules_shipping_framework_woocommerce.php
+++ b/includes/rules_shipping_framework_woocommerce.php
@@ -242,7 +242,7 @@ class RulesShippingFrameworkWooCommerce extends RulesShippingFramework {
 			$data['vendors'] = array_unique($vendors);
 		}
 		
-		// "WooThemes Vendor Products" support (vendors stored in its own taxonomy)
+		// "WooThemes Vendor Products" <2.0 support (vendors stored in its own taxonomy)
 		if (class_exists("WooCommerce_Product_Vendors") && function_exists("get_product_vendors")) {
 			$vendors = array();
 			$vendornames = array();
@@ -250,7 +250,6 @@ class RulesShippingFrameworkWooCommerce extends RulesShippingFramework {
 			// The plugin provides its own function to retrieve the vendor for a product
 			foreach ($products as $product) {
 				foreach (get_product_vendors($product['data']->get_id()) as $vendor) {
-// $this->printWarning("<pre>vendor: ".print_r($vendor,1)."</pre>");
 					$vendors[] = urldecode($vendor->slug);
 					$vendornames[] = $vendor->title;
 					$vendorids[] = $vendor->ID;
@@ -260,6 +259,25 @@ class RulesShippingFrameworkWooCommerce extends RulesShippingFramework {
 			$data['vendornames'] = array_unique($vendornames);
 			$data['vendorids'] = array_unique($vendorids);
 		}
+		// "WooThemes Vendor Products" >=2.0 support (vendors stored in its own taxonomy)
+		if (class_exists("WC_Product_Vendors") && method_exists("WC_Product_Vendors_Utils", "get_vendor_id_from_product")) {
+			$vendors = array();
+			$vendornames = array();
+			$vendorids = array();
+			// The plugin provides its own function to retrieve the vendor for a product
+			foreach ($products as $product) {
+				$vndid = WC_Product_Vendors_Utils::get_vendor_id_from_product($product['data']->get_id());
+				if ($vndid>0) {
+					$vendor = WC_Product_Vendors_Utils::get_vendor_data_by_id($vndid);
+					$vendors[] = urldecode($vendor["slug"]);
+					$vendornames[] = $vendor["name"];
+					$vendorids[] = $vendor["term_id"];
+				}
+			}
+			$data['vendors'] = array_unique($vendors);
+			$data['vendornames'] = array_unique($vendornames);
+			$data['vendorids'] = array_unique($vendorids);
+		}
 		
 		// "YITH WooCommerce Multi Vendor" support (vendors stored in its own taxonomy)
 		if (function_exists("yith_get_vendor")) {
@@ -426,7 +444,7 @@ class RulesShippingFrameworkWooCommerce extends RulesShippingFramework {
 					$vnd_props[] = $vnd->user_login;
 				}
 
-				// "WooThemes Vendor Products" support (vendors stored in its own taxonomy)
+				// "WooThemes Product Vendors" support (vendors stored in its own taxonomy)
 				if (class_exists("WooCommerce_Product_Vendors") && function_exists("get_product_vendors")) {
 					foreach (get_product_vendors($p['data']->id) as $vendor) {
 						$vnd_props[] = urldecode($vendor->slug);
-- 
GitLab