Skip to content

Commit

Permalink
add brand support
Browse files Browse the repository at this point in the history
make commission names unique
  • Loading branch information
davodsaraei committed Dec 14, 2020
1 parent 1fa344d commit 4414555
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 22 deletions.
2 changes: 1 addition & 1 deletion affili_ir.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion classes/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +57,7 @@ public function menu()

public function renderPage()
{
$show_brand = AffiliIR_ActivePluginsCheck::wooBrandActiveCheck();
$woocommerce = new AffiliIR_Woocommerce;

$account_id = $this->getAccountId();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down
43 changes: 43 additions & 0 deletions classes/ActivePluginsCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace AffiliIR;


class ActivePluginsCheck
{
private static $active_plugins;

public static function init()
{
self::$active_plugins = (array) get_option('active_plugins', []);

if(is_multisite()) {
self::$active_plugins = array_merge(
self::$active_plugins,
get_site_option('active_sitewide_plugins', [])
);
}
}

public static function woocommerceActiveCheck() {

if(!self::$active_plugins) {
self::init();
}

return in_array('woocommerce/woocommerce.php', self::$active_plugins)
|| array_key_exists('woocommerce/woocommerce.php', self::$active_plugins)
;
}

public static function wooBrandActiveCheck()
{
if(!self::$active_plugins) {
self::init();
}

return in_array('woo-brand/main.php', self::$active_plugins)
|| array_key_exists('woo-brand/main.php', self::$active_plugins)
;
}
}
46 changes: 44 additions & 2 deletions classes/ListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@


require_once 'Woocommerce.php';
require_once 'ActivePluginsCheck.php';

// WP_List_Table is not loaded automatically so we need to load it in our application
if( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

use AffiliIR\Woocommerce as AffiliIR_Woocommerce;
use AffiliIR\ActivePluginsCheck as AffiliIR_ActivePluginsCheck;

class ListTable extends \WP_List_Table
{
Expand Down Expand Up @@ -55,7 +57,16 @@ public function get_columns()
$columns = [
'id' => __('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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 ];

Expand Down Expand Up @@ -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;
}
}
?>
110 changes: 95 additions & 15 deletions classes/Woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace AffiliIR;


require_once 'ActivePluginsCheck.php';


use AffiliIR\ActivePluginsCheck as AffiliIR_ActivePluginsCheck;

class Woocommerce
{
private $table_name;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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) : [];

Expand All @@ -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;
}
Expand All @@ -94,17 +152,31 @@ 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']}";

$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] ?? '';
}
Expand All @@ -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,
Expand Down Expand Up @@ -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',
] : [];
];
}

/**
Expand Down
5 changes: 4 additions & 1 deletion languages/affili_ir-fa_IR.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ msgid "Add Commission key"
msgstr "افزودن کلید کمیسیون"

msgid "Custom Code"
msgstr "کد دلخواه"
msgstr "کد دلخواه"

msgid "Brand"
msgstr "برند"
Loading

0 comments on commit 4414555

Please sign in to comment.