From 44145550ac444ce95b24ea121a17b9bf89022dab Mon Sep 17 00:00:00 2001 From: Davod Saraei Date: Tue, 15 Dec 2020 02:44:02 +0330 Subject: [PATCH] add brand support make commission names unique --- affili_ir.php | 2 +- classes/Action.php | 25 +++++++- classes/ActivePluginsCheck.php | 43 +++++++++++++ classes/ListTable.php | 46 +++++++++++++- classes/Woocommerce.php | 110 ++++++++++++++++++++++++++++----- languages/affili_ir-fa_IR.po | 5 +- views/form.php | 10 ++- 7 files changed, 219 insertions(+), 22 deletions(-) create mode 100644 classes/ActivePluginsCheck.php diff --git a/affili_ir.php b/affili_ir.php index dfdf0a8..4dd8588 100644 --- a/affili_ir.php +++ b/affili_ir.php @@ -7,7 +7,7 @@ * Plugin Name: affili_ir * Plugin URI: https://github.com/affili-ir/wordpress * Description: The WordPress plugin for Affili's merchants. - * Version: 1.1.0 + * Version: 3.2.0 * Author: Affili IR * Author URI: https://affili.ir * License: GPLv2 or later diff --git a/classes/Action.php b/classes/Action.php index e4fa642..93df327 100644 --- a/classes/Action.php +++ b/classes/Action.php @@ -6,8 +6,10 @@ require_once 'Woocommerce.php'; require_once 'ListTable.php'; require_once 'Installer.php'; +require_once 'ActivePluginsCheck.php'; +use AffiliIR\ActivePluginsCheck as AffiliIR_ActivePluginsCheck; use AffiliIR\Woocommerce as AffiliIR_Woocommerce; use AffiliIR\ListTable as AffiliIR_ListTable; use AffiliIR\Installer as AffiliIR_Installer; @@ -55,6 +57,7 @@ public function menu() public function renderPage() { + $show_brand = AffiliIR_ActivePluginsCheck::wooBrandActiveCheck(); $woocommerce = new AffiliIR_Woocommerce; $account_id = $this->getAccountId(); @@ -102,7 +105,7 @@ public function setAccountId() } $woocommerce = new AffiliIR_Woocommerce; - $woocommerce->insertCommissionKeys($_POST['category']); + $woocommerce->insertCommissionKeys($_POST['item']); $admin_notice = "success"; $message = __('Data saved successful.', $this->plugin_name); @@ -262,6 +265,7 @@ public function setup() add_action('woocommerce_thankyou', [$this, 'trackOrders']); add_action('wp_ajax_affili_find_category', [$this, 'findCategoryAjax']); + add_action('wp_ajax_affili_find_brand', [$this, 'findBrandAjax']); } public static function factory() @@ -343,6 +347,25 @@ public function findCategoryAjax() wp_die(); } + public function findBrandAjax() + { + // we will pass brand IDs and names to this array + $return = []; + + $search_results = (new AffiliIR_Woocommerce)->getBrands(null, [ + 'name__like' => $_GET['q'], + ]); + + foreach($search_results as $result) { + $return[] = [ + $result->term_id, + $result->name, + ]; + } + echo json_encode( $return ); + wp_die(); + } + private function createTableIfNotExists() { $sql = AffiliIR_Installer::sqlString(); diff --git a/classes/ActivePluginsCheck.php b/classes/ActivePluginsCheck.php new file mode 100644 index 0000000..27509f3 --- /dev/null +++ b/classes/ActivePluginsCheck.php @@ -0,0 +1,43 @@ + __('Category ID', $this->plugin_name), 'category' => __('Category', $this->plugin_name), - 'value' => __('Commission key', $this->plugin_name), + ]; + + if(AffiliIR_ActivePluginsCheck::wooBrandActiveCheck()) { + $columns += [ + 'brand' => __('Brand', $this->plugin_name), + ]; + } + + $columns += [ + 'value' => __('Commission key', $this->plugin_name), ]; return $columns; @@ -94,7 +105,14 @@ private function table_data() $commission_keys = $woocommerce->getCommissionKeys(); foreach($commission_keys as $key => $commission) { - $cat_id = str_replace('category-commission-', '', $commission->name); + $cat_id = str_replace('commission-cat-', '', $commission->name); + if($this->strContains($cat_id, 'brand-')) { + $cat_id = $this->strBefore($cat_id, 'brand-'); + } + + $brand_id = $this->strAfter($commission->name, 'brand-'); + + $commission_keys[$key]->brand = $brand_id ? get_the_category_by_ID((int)$brand_id) : null; $commission_keys[$key]->category = get_the_category_by_ID((int)$cat_id); } @@ -114,6 +132,7 @@ public function column_default( $item, $column_name ) switch( $column_name ) { case 'id': case 'category': + case 'brand': case 'value': return $item[ $column_name ]; @@ -155,5 +174,28 @@ private function sort_data( $a, $b ) return -$result; } + + private function strContains($haystack, $needles) + { + foreach ((array) $needles as $needle) { + if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { + return true; + } + } + + return false; + } + + private function strBefore($subject, $search) + { + return $search === '' ? $subject : explode($search, $subject)[0]; + } + + private function strAfter($subject, $search) + { + $result = $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0]; + + return $result === $subject ? null : $result; + } } ?> \ No newline at end of file diff --git a/classes/Woocommerce.php b/classes/Woocommerce.php index 4e7d353..07f1ee1 100644 --- a/classes/Woocommerce.php +++ b/classes/Woocommerce.php @@ -2,6 +2,12 @@ namespace AffiliIR; + +require_once 'ActivePluginsCheck.php'; + + +use AffiliIR\ActivePluginsCheck as AffiliIR_ActivePluginsCheck; + class Woocommerce { private $table_name; @@ -49,17 +55,63 @@ public function getCategories($category_id = null, $options = []) return get_categories( $args ); } + public function getBrands($brand_id = null, $options = []) + { + $taxonomy = 'product_brand'; + $orderby = 'id'; + $show_count = 0; // 1 for yes, 0 for no + $pad_counts = 0; // 1 for yes, 0 for no + $hierarchical = 1; // 1 for yes, 0 for no + $title = ''; + $empty = 0; + + $args = [ + 'taxonomy' => $taxonomy, + 'orderby' => $orderby, + 'show_count' => $show_count, + 'pad_counts' => $pad_counts, + 'hierarchical' => $hierarchical, + 'title_li' => $title, + 'hide_empty' => $empty, + ]; + + if($brand_id !== null) { + $args += [ + 'parent' => $brand_id, + 'child_of' => 0, + ]; + } + + if(is_array($options) && $options) { + $args = array_merge($args, $options); + } + + $brands = get_terms($args); + + if (is_wp_error($brands)) { + $brands = []; + } + + return $brands; + } + public function insertCommissionKeys($item) { - if(empty($item['id'])) { + $category_id = $item['category_id'] ?? null; + $brand_id = $item['brand_id'] ?? null; + + if($category_id === null) { return; } - $commission_key = $this->findCommissionKey($item['id']); + $commission_key = $this->findCommissionKey($category_id, $brand_id); + $name = $brand_id ? "commission-cat-{$category_id}-brand-{$brand_id}" + : "commission-cat-{$category_id}" + ; $data = [ - 'name' => "category-commission-{$item['id']}", - 'value' => $item['commission-key'], + 'name' => $name, + 'value' => $item['commission_key'], ]; if(empty($commission_key)) { @@ -71,10 +123,13 @@ public function insertCommissionKeys($item) } } - public function findCommissionKey($category_id) + public function findCommissionKey($category_id, $brand_id = null) { + $name = $brand_id ? "commission-cat-{$category_id}-brand-{$brand_id}" + : "commission-cat-{$category_id}" + ; $result = $this->wpdb->get_results( - "SELECT * FROM {$this->table_name} WHERE name = 'category-commission-{$category_id}' limit 1" + "SELECT * FROM {$this->table_name} WHERE name = '{$name}' limit 1" ); $result = is_array($result) ? array_pop($result) : []; @@ -83,9 +138,12 @@ public function findCommissionKey($category_id) public function getCommissionKeys() { - $result = $this->wpdb->get_results( - "SELECT * FROM {$this->table_name} WHERE name LIKE 'category-commission-%'" - ); + $sql = "SELECT * FROM {$this->table_name} WHERE name LIKE 'commission-cat-%'"; + if(!AffiliIR_ActivePluginsCheck::wooBrandActiveCheck()) { + $sql .= " AND name NOT LIKE 'commission-cat-%-brand-%'"; + } + + $result = $this->wpdb->get_results($sql); return $result; } @@ -94,10 +152,17 @@ public function getOrderData($order) { $options = []; $commissions = []; + $holder = []; foreach ($order->get_items() as $item) { - $subtotal = $this->getOrderItemSubtotal($order, $item); - $commissions[] = $this->getOrderItemCommissions($item, $subtotal); + $subtotal = $this->getOrderItemSubtotal($order, $item); + $item_commission = $this->getOrderItemCommissions($item, $subtotal); + + $commission_name = $item_commission['name']; + + $holder[$commission_name] = + ($holder[$commission_name] ?? 0) + $item_commission['sub_amount'] + ; $key = "product-{$item->get_product_id()}"; $line_item = "{$item['name']} - qty: {$item['qty']}"; @@ -105,6 +170,13 @@ public function getOrderData($order) $options['meta_data'][$key] = $line_item; } + foreach($holder as $name => $sub_amount) { + $commissions[] = [ + 'name' => $name, + 'sub_amount' => $sub_amount + ]; + } + if($coupons = $order->get_coupon_codes()) { $options['coupon'] = array_values($coupons)[0] ?? ''; } @@ -122,7 +194,6 @@ public function getOrderData($order) $commissions = count($commissions) ? json_encode($commissions) : json_encode($commissions, JSON_FORCE_OBJECT); $options = count($options) ? json_encode($options) : json_encode($options, JSON_FORCE_OBJECT); - return [ 'commissions' => $commissions, 'options' => $options, @@ -156,17 +227,26 @@ private function getOrderItemCommissions($item, $subtotal) { $category_commission_type = false; $categories = wp_get_post_terms($item->get_product_id(), 'product_cat'); + + $brand_id = null; + if(AffiliIR_ActivePluginsCheck::wooBrandActiveCheck()) { + $brands = get_the_terms($item->get_product_id(), 'product_brand'); + if($brands) { + $brand_id = end($brands)->term_id ?? null; + } + } + foreach ( $categories as $category ) { - $commission_key = $this->findCommissionKey($category->term_id); + $commission_key = $this->findCommissionKey($category->term_id, $brand_id); if($commission_key && $commission_key->value) { $category_commission_type = $commission_key->value; } } - return $category_commission_type ? [ + return [ 'sub_amount' => $subtotal, 'name' => $category_commission_type ?: 'default', - ] : []; + ]; } /** diff --git a/languages/affili_ir-fa_IR.po b/languages/affili_ir-fa_IR.po index cb4fa89..334c0a7 100644 --- a/languages/affili_ir-fa_IR.po +++ b/languages/affili_ir-fa_IR.po @@ -43,4 +43,7 @@ msgid "Add Commission key" msgstr "افزودن کلید کمیسیون" msgid "Custom Code" -msgstr "کد دلخواه" \ No newline at end of file +msgstr "کد دلخواه" + +msgid "Brand" +msgstr "برند" \ No newline at end of file diff --git a/views/form.php b/views/form.php index d0c3e47..64caacd 100644 --- a/views/form.php +++ b/views/form.php @@ -12,11 +12,17 @@
- +
+ +
+ + +
+
- +