Skip to content

Commit

Permalink
Save bulk product commission.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Dec 5, 2024
1 parent c17068f commit e9452e0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
37 changes: 37 additions & 0 deletions includes/Admin/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WeDevs\Dokan\Admin;

use WeDevs\Dokan\Product\Hooks as ProductHooks;
use WP_Post;

// don't call the file directly
Expand Down Expand Up @@ -35,6 +36,9 @@ public function __construct() {

// Ajax hooks
add_action( 'wp_ajax_dokan_product_search_author', [ $this, 'search_vendors' ] );

add_action( 'woocommerce_product_bulk_edit_end', [ $this, 'add_product_commission_bulk_edit_field' ] );
add_action( 'woocommerce_product_bulk_edit_save', [ $this, 'save_custom_bulk_edit_field' ], 10, 1 );
}

/**
Expand Down Expand Up @@ -223,4 +227,37 @@ public function update_pages( $value, $name ) {

return array_replace_recursive( $current_settings, $value );
}

/**
* Add commission settings in bulk product edit.
*
* @since DOKAN_SINCE
*
* @return void
*/
public function add_product_commission_bulk_edit_field() {
dokan_get_template_part( 'products/dokan-products-edit-bulk-commission', '', [] );
}

/**
* Save commission settings from bulk product edit
*
* @since DOKAN_SINCE
*
* @param \WC_Product $product
*
* @return void
*/
public function save_custom_bulk_edit_field( $product ) {
$excluded_product_types = apply_filters( 'dokan_excluded_product_types_for_bulk_edit', [ 'product_pack', 'external', 'grouped' ] );
if ( ! current_user_can( 'manage_woocommerce' ) || in_array( $product->get_type(), $excluded_product_types, true ) ) {
return;
}

if ( ! isset( $_REQUEST['dokan_override_bulk_product_commission'] ) || sanitize_text_field( $_REQUEST['dokan_override_bulk_product_commission'] ) !== 'on' ) { // phpcs:ignore
return;
}

ProductHooks::save_per_product_commission_options( $product->get_id(), $_REQUEST ); // phpcs:ignore
}
}
39 changes: 24 additions & 15 deletions includes/Product/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ public function store_products_orderby() {
<div class="dokan-store-products-filter-area dokan-clearfix">
<form class="dokan-store-products-ordeby" method="get">
<input type="text" name="product_name" class="product-name-search dokan-store-products-filter-search"
placeholder="<?php esc_attr_e( 'Enter product name', 'dokan-lite' ); ?>" autocomplete="off"
data-store_id="<?php echo esc_attr( $store_id ); ?>">
placeholder="<?php esc_attr_e( 'Enter product name', 'dokan-lite' ); ?>" autocomplete="off"
data-store_id="<?php echo esc_attr( $store_id ); ?>">
<div id="dokan-store-products-search-result" class="dokan-ajax-store-products-search-result"></div>
<input type="submit" name="search_store_products" class="search-store-products dokan-btn-theme"
value="<?php esc_attr_e( 'Search', 'dokan-lite' ); ?>">
value="<?php esc_attr_e( 'Search', 'dokan-lite' ); ?>">

<?php if ( is_array( $orderby_options['catalogs'] ) && isset( $orderby_options['orderby'] ) ) : ?>
<select name="product_orderby" class="orderby orderby-search"
Expand Down Expand Up @@ -522,36 +522,45 @@ class="woocommerce-help-tip"
*
* @since 2.4.12
*
* @param integer $post_id
* @param integer $post_id
* @param array $data
*
* @return void
*/
public static function save_per_product_commission_options( $post_id ) {
public static function save_per_product_commission_options( $post_id, $data = [] ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}

$commission_type = Fixed::SOURCE;
$admin_commission = '';
$additional_fee = '';
$data = empty( $data ) ? $_POST : $data; // phpcs:ignore

if ( isset( $_POST['_per_product_admin_commission_type'] ) ) { // phpcs:ignore
$commission_type = ! empty( $_POST['_per_product_admin_commission_type'] ) ? sanitize_text_field( $_POST['_per_product_admin_commission_type'] ) : Fixed::SOURCE; // phpcs:ignore
update_post_meta( $post_id, '_per_product_admin_commission_type', $commission_type );
if ( isset( $data['_per_product_admin_commission_type'] ) ) {
$commission_type = ! empty( $data['_per_product_admin_commission_type'] ) ? sanitize_text_field( $data['_per_product_admin_commission_type'] ) : Fixed::SOURCE;
}

if ( isset( $_POST['_per_product_admin_commission'] ) ) { // phpcs:ignore
$_per_product_admin_commission = wc_format_decimal( sanitize_text_field( $_POST['_per_product_admin_commission'] ) ); // phpcs:ignore
if ( isset( $data['_per_product_admin_commission'] ) ) {
$_per_product_admin_commission = wc_format_decimal( sanitize_text_field( $data['_per_product_admin_commission'] ) );

if ( 0 <= $_per_product_admin_commission && 100 >= $_per_product_admin_commission ) {
$admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : $_per_product_admin_commission; // phpcs:ignore
$admin_commission = ( '' === $data['_per_product_admin_commission'] ) ? '' : $_per_product_admin_commission;
}
}

if ( isset( $_POST['_per_product_admin_additional_fee'] ) ) { // phpcs:ignore
$additional_fee = ( '' === $_POST['_per_product_admin_additional_fee'] ) ? '' : sanitize_text_field( $_POST['_per_product_admin_additional_fee'] ); // phpcs:ignore
if ( isset( $data['_per_product_admin_additional_fee'] ) ) {
$additional_fee = ( '' === $data['_per_product_admin_additional_fee'] ) ? '' : sanitize_text_field( $data['_per_product_admin_additional_fee'] );
$additional_fee = wc_format_decimal( $additional_fee );
}

update_post_meta( $post_id, '_per_product_admin_commission', $admin_commission );
update_post_meta( $post_id, '_per_product_admin_additional_fee', wc_format_decimal( $additional_fee ) );
dokan()->product->save_commission_settings(
$post_id,
[
'type' => $commission_type,
'percentage' => $admin_commission,
'flat' => $additional_fee,
]
);
}
}
38 changes: 38 additions & 0 deletions templates/products/dokan-products-edit-bulk-commission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Template Name: Dokan commission setting bulk product edit
*
* @since DOKAN_SINCE
*
* @package Dokan
*/

use WeDevs\Dokan\Commission\Formula\Fixed;
?>

<div class="inline-edit-col" style="float: left">
<h4><?php esc_html_e( 'Commission settings', 'dokan-lite' ); ?></h4>
<div class="inline-edit-group dokan-admin-bulk-product-commission-override-box">
<label class="alignleft">
<span class="title"><?php esc_html_e( 'Override', 'dokan-lite' ); ?></span>
<input type="checkbox" name="dokan_override_bulk_product_commission" id="dokan_override_bulk_product_commission">
</label>
</div>

<div class="inline-edit-group dokan-admin-bulk-product-commission-data-box">
<p class="form-field dimensions_field">
<label for="admin_commission">
<?php esc_html_e( 'Admin Commission', 'dokan-lite' ); ?>
</label>

<span class="input-text-wrap" style="display: flex">
<input type="hidden" name="_per_product_admin_commission_type" value="<?php echo esc_attr( Fixed::SOURCE ); ?>">
<input class="input-text wc_input_price" min="0" max="100" type="text" name="_per_product_admin_commission" value=""/>
<span style="display: flex; align-items: center">%&nbsp;&nbsp;+</span>
<input type="text" name="_per_product_admin_additional_fee" class="input-text wc_input_price" value="">
</span>
</span>
</p>
</div>
</div>

0 comments on commit e9452e0

Please sign in to comment.