Skip to content

Commit

Permalink
Add upgrader item
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Nov 25, 2024
1 parent 3f3d634 commit 1e19d0b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
39 changes: 23 additions & 16 deletions includes/Commission/Upugrader/Update_Category_Commission.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ class Update_Category_Commission {
*/
const PROCESS_BATCH_HOOK = 'process_category_batch';

/**
*
* @since DOKAN_PRO_SINCE
*/
const PROCESS_ITEM_HOOK = 'process_category_item';

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

/**
Expand Down Expand Up @@ -55,7 +62,7 @@ public function process_batch( $page_number ) {

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

// Schedule next batch since we have categories in this batch
Expand All @@ -76,7 +83,15 @@ protected function schedule_next_batch( $page_number ) {
WC()->queue()->add(
self::PROCESS_BATCH_HOOK,
[ $page_number ],
'category_processing'
'dokan_updater_category_processing'
);
}

private function schedule_cat_item( $term ) {
WC()->queue()->add(
self::PROCESS_ITEM_HOOK,
[ $term ],
'dokan_updater_category_item_processing'
);
}

Expand Down Expand Up @@ -107,25 +122,17 @@ protected function get_categories_batch( $page_number ) {
*
* @since DOKAN_PRO_SINCE
*
* @param WP_Term $term Category term object
* @param int $term Category term object
*
* @return void
*/
protected function process_single_category( $term ) {
error_log(
sprintf(
'Processing category #%d: %s',
$term->term_id,
$term->name
)
);

public function process_single_category( $term_id ) {
$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 );
$commission_type = get_term_meta( $term_id, 'per_category_admin_commission_type', true );
$admin_additional_fee = get_term_meta( $term_id, 'per_category_admin_additional_fee', true );
$commission = get_term_meta( $term_id, 'per_category_admin_commission', true );

if ( ! empty( $commission_type ) ) {
$category_commission_item = [
Expand All @@ -141,7 +148,7 @@ protected function process_single_category( $term ) {
$category_commission_item['flat'] = 0;
}

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

$dokan_selling['commission_category_based_values'] = $category_commission;
Expand Down
37 changes: 22 additions & 15 deletions includes/Commission/Upugrader/Update_Product_Commission.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ class Update_Product_Commission {
*/
const PROCESS_BATCH_HOOK = 'process_product_batch';

/**
*
* @since DOKAN_PRO_SINCE
*/
const PROCESS_ITEM_HOOK = 'process_product_item';

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

/**
Expand Down Expand Up @@ -61,7 +68,7 @@ public function process_batch( $offset, $total_products ) {
$products = $this->get_products_batch( $offset );

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

// Calculate next offset
Expand All @@ -88,7 +95,16 @@ protected function schedule_next_batch( $offset, $total_products ) {
self::PROCESS_BATCH_HOOK, [
$offset,
$total_products,
], 'product_processing'
],
'dokan_updater_product_processing'
);
}

private function schedule_item( $item ) {
WC()->queue()->add(
self::PROCESS_ITEM_HOOK,
[ $item ],
'dokan_updater_product_item_processing'
);
}

Expand Down Expand Up @@ -137,21 +153,12 @@ protected function get_total_products() {
*
* @since DOKAN_PRO_SINCE
*
* @param WC_Product $product
* @param int $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() );
public function process_single_product( $product_id ) {
$commission = dokan()->product->get_commission_settings( $product_id );

$commission_type_old = $commission->get_type();
$commission->set_type( Fixed::SOURCE );
Expand All @@ -164,7 +171,7 @@ protected function process_single_product( $product ) {
}

dokan()->product->save_commission_settings(
$product->get_id(),
$product_id,
[
'type' => $commission->get_type(),
'percentage' => $commission->get_percentage(),
Expand Down
32 changes: 20 additions & 12 deletions includes/Commission/Upugrader/Update_Vendor_Commission.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ class Update_Vendor_Commission {
*/
const PROCESS_BATCH_HOOK = 'process_vendor_batch';

/**
*
* @since DOKAN_PRO_SINCE
*/
const PROCESS_ITEM_HOOK = 'process_vendor_item';

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

/**
Expand Down Expand Up @@ -51,7 +58,7 @@ public function process_batch( $page_number ) {

if ( ! empty( $vendors ) ) {
foreach ( $vendors as $vendor ) {
$this->process_single_vendor( $vendor );
$this->schedule_item( $vendor->get_id() );
}

// Schedule next batch since we have vendors in this batch
Expand All @@ -72,7 +79,15 @@ protected function schedule_next_batch( $page_number ) {
WC()->queue()->add(
self::PROCESS_BATCH_HOOK,
[ $page_number ],
'vendor_processing'
'dokan_updater_vendor_processing'
);
}

private function schedule_item( $item ) {
WC()->queue()->add(
self::PROCESS_ITEM_HOOK,
[ $item ],
'dokan_updater_vendor_item_processing'
);
}

Expand All @@ -83,7 +98,7 @@ protected function schedule_next_batch( $page_number ) {
*
* @param int $page_number Page number to fetch
*
* @return array Array of vendor objects
* @return \WeDevs\Dokan\Vendor\Vendor[] Array of vendor objects
*/
protected function get_vendors_batch( $page_number ) {
return dokan()->vendor->all(
Expand All @@ -103,15 +118,8 @@ protected function get_vendors_batch( $page_number ) {
*
* @return void
*/
protected function process_single_vendor( $vendor ) {
error_log(
sprintf(
'Processing vendor #%d: %s',
$vendor->get_id(),
$vendor->get_shop_name()
)
);

public function process_single_vendor( $vendor_id ) {
$vendor = dokan()->vendor->get( $vendor_id );
$commission = $vendor->get_commission_settings();

$commission_type_old = $commission->get_type();
Expand Down

0 comments on commit 1e19d0b

Please sign in to comment.