Skip to content

Commit

Permalink
Add upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Nov 25, 2024
1 parent 27fa0e1 commit 3f3d634
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 264 deletions.
161 changes: 161 additions & 0 deletions includes/Commission/Upugrader/Update_Category_Commission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace WeDevs\Dokan\Commission\Upugrader;

use WeDevs\Dokan\Commission\Formula\Flat;
use WeDevs\Dokan\Commission\Formula\Percentage;
use WP_Term;

class Update_Category_Commission {
/**
* The batch size for processing categories
*
* @since DOKAN_PRO_SINCE
*/
const BATCH_SIZE = 20;

/**
* The hook name for processing batches
*
* @since DOKAN_PRO_SINCE
*/
const PROCESS_BATCH_HOOK = 'process_category_batch';

/**
* Initialize the processor
*/
public function init_hooks() {
add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 1 );
}

/**
* Start the batch processing
*
* @since DOKAN_PRO_SINCE
*
* @return void
*/
public function start_processing() {
// Schedule the first batch with page 1
$this->schedule_next_batch( 1 );
}

/**
* Process a batch of categories
*
* @since DOKAN_PRO_SINCE
*
* @param int $page_number Current page number
*
* @return void
*/
public function process_batch( $page_number ) {
// Get categories for this batch
$categories = $this->get_categories_batch( $page_number );

if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
foreach ( $categories as $category ) {
$this->process_single_category( $category );
}

// Schedule next batch since we have categories in this batch
$this->schedule_next_batch( $page_number + 1 );
}
}

/**
* Schedule the next batch of categories
*
* @since DOKAN_PRO_SINCE
*
* @param int $page_number Next page number to process
*
* @return void
*/
protected function schedule_next_batch( $page_number ) {
WC()->queue()->add(
self::PROCESS_BATCH_HOOK,
[ $page_number ],
'category_processing'
);
}

/**
* Get a batch of categories.
*
* @since DOKAN_PRO_SINCE
*
* @param int $page_number Page number to fetch
*
* @return array Array of term objects
*/
protected function get_categories_batch( $page_number ) {
$args = [
'taxonomy' => 'product_cat',
'number' => self::BATCH_SIZE,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'offset' => ( $page_number - 1 ) * self::BATCH_SIZE,
];

return get_terms( $args );
}

/**
* Process a single category.
*
* @since DOKAN_PRO_SINCE
*
* @param WP_Term $term Category term object
*
* @return void
*/
protected function process_single_category( $term ) {
error_log(
sprintf(
'Processing category #%d: %s',
$term->term_id,
$term->name
)
);

$dokan_selling = get_option( 'dokan_selling', [] );
$category_commission = dokan_get_option( 'commission_category_based_values', 'dokan_selling', [] );

$commission_type = get_term_meta( $term->term_id, 'per_category_admin_commission_type', true );
$admin_additional_fee = get_term_meta( $term->term_id, 'per_category_admin_additional_fee', true );
$commission = get_term_meta( $term->term_id, 'per_category_admin_commission', true );

if ( ! empty( $commission_type ) ) {
$category_commission_item = [
'flat' => $admin_additional_fee,
'percentage' => $commission,
];

if ( Flat::SOURCE === $commission_type ) {
$category_commission_item['percentage'] = 0;
$category_commission_item['flat'] = $commission;
} elseif ( Percentage::SOURCE === $commission_type ) {
$category_commission_item['percentage'] = $commission;
$category_commission_item['flat'] = 0;
}

$category_commission['items'][ $term->term_id ] = $category_commission_item;
}

$dokan_selling['commission_category_based_values'] = $category_commission;
update_option( 'dokan_selling', $dokan_selling );
}

/**
* Check if processing is currently running.
*
* @since DOKAN_PRO_SINCE
*
* @return bool
*/
public function is_processing() {
return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null;
}
}
186 changes: 186 additions & 0 deletions includes/Commission/Upugrader/Update_Product_Commission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

namespace WeDevs\Dokan\Commission\Upugrader;

use WC_Product;
use WeDevs\Dokan\Commission\Formula\Fixed;
use WeDevs\Dokan\Commission\Formula\Flat;
use WeDevs\Dokan\Commission\Formula\Percentage;

class Update_Product_Commission {

/**
* The batch size for processing products
*
* @since DOKAN_PRO_SINCE
*/
const BATCH_SIZE = 10;

/**
* The hook name for processing batches
*
* @since DOKAN_PRO_SINCE
*/
const PROCESS_BATCH_HOOK = 'process_product_batch';

public function init_hooks() {
add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 2 );
}

/**
* Start the batch processing
*
* @since DOKAN_PRO_SINCE
*
* @return void
*/
public function start_processing() {
// Get total number of products
$total_products = $this->get_total_products();

if ( $total_products === 0 ) {
return;
}

// Schedule the first batch
$this->schedule_next_batch( 0, $total_products );
}

/**
* Process a batch of products
*
* @since DOKAN_PRO_SINCE
*
* @param int $offset Current offset
* @param int $total_products Total number of products
*
* @return void
*/
public function process_batch( $offset, $total_products ) {
// Get products for this batch
$products = $this->get_products_batch( $offset );

foreach ( $products as $product ) {
$this->process_single_product( $product );
}

// Calculate next offset
$next_offset = $offset + self::BATCH_SIZE;

// If there are more products to process, schedule the next batch
if ( $next_offset < $total_products ) {
$this->schedule_next_batch( $next_offset, $total_products );
}
}

/**
* Schedule the next batch of products
*
* @since DOKAN_PRO_SINCE
*
* @param int $offset Current offset
* @param int $total_products Total number of products
*
* @return void
*/
protected function schedule_next_batch( $offset, $total_products ) {
WC()->queue()->add(
self::PROCESS_BATCH_HOOK, [
$offset,
$total_products,
], 'product_processing'
);
}

/**
* Get a batch of products
*
* @since DOKAN_PRO_SINCE
*
* @param int $offset Current offset
*
* @return WC_Product[] Array of product objects
*/
protected function get_products_batch( $offset ) {
$args = [
'status' => 'publish',
'limit' => self::BATCH_SIZE,
'offset' => $offset,
'orderby' => 'ID',
'order' => 'ASC',
];

return wc_get_products( $args );
}

/**
* Get total number of products
*
* @since DOKAN_PRO_SINCE
*
* @return int
*/
protected function get_total_products() {
$args = [
'status' => 'publish',
'limit' => -1,
'return' => 'ids',
];

$products = wc_get_products( $args );
return count( $products );
}

/**
* Process a single product
* Customize this method based on what you need to do with each product
*
* @since DOKAN_PRO_SINCE
*
* @param WC_Product $product
*
* @return void
*/
protected function process_single_product( $product ) {
// Example processing - customize this based on your needs
error_log(
sprintf(
'Processing product #%d: %s',
$product->get_id(),
$product->get_name()
)
);

$commission = dokan()->product->get_commission_settings( $product->get_id() );

$commission_type_old = $commission->get_type();
$commission->set_type( Fixed::SOURCE );

if ( Flat::SOURCE === $commission_type_old ) {
$commission->set_flat( $commission->get_percentage() );
$commission->set_percentage( 0 );
} elseif ( Percentage::SOURCE === $commission_type_old ) {
$commission->set_flat( 0 );
}

dokan()->product->save_commission_settings(
$product->get_id(),
[
'type' => $commission->get_type(),
'percentage' => $commission->get_percentage(),
'flat' => $commission->get_flat(),
]
);
}

/**
* Check if processing is currently running
*
* @since DOKAN_PRO_SINCE
*
* @return bool
*/
public function is_processing() {
return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null;
}
}
Loading

0 comments on commit 3f3d634

Please sign in to comment.