<?php
/*
Plugin Name: Dokan Product Categories Additional Fees
Plugin URI: https://example.com
Description: Adds an "Additional Fees" field to WooCommerce product categories and displays additional fees for orders with products from multiple vendors based on the "Additional Fees" value.
Version: 1.0
Author: Your Name
Author URI: https://example.com
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
function enqueue_sweetalert2() {
wp_enqueue_style('sweetalert2', 'https://cdn.jsdelivr.net/npm/sweetalert2@11.7.3/dist/sweetalert2.min.css');
wp_enqueue_script('sweetalert2', 'https://cdn.jsdelivr.net/npm/sweetalert2@11.7.3/dist/sweetalert2.all.min.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'enqueue_sweetalert2');
// Enqueue the JavaScript file
// Save the "Additional Fees" field value when adding/editing a product category
add_action('created_term', 'save_additional_fees_field', 10, 3);
add_action('edit_term', 'save_additional_fees_field', 10, 3);
function save_additional_fees_field($term_id, $tt_id, $taxonomy)
{
if ('product_cat' !== $taxonomy) {
return;
}
$additional_fees = isset($_POST['additional_fees']) ? sanitize_text_field($_POST['additional_fees']) : '';
update_term_meta($term_id, 'additional_fees', $additional_fees);
}
// Add the "Additional Fees" field to the product category add/edit page
add_action('product_cat_add_form_fields', 'add_additional_fees_field');
add_action('product_cat_edit_form_fields', 'edit_additional_fees_field', 10, 2);
function add_additional_fees_field()
{
?>
<div class="form-field term-additional-fees-wrap">
<label for="additional-fees"><?php esc_html_e('Additional Fees', 'woocommerce'); ?></label>
<input type="text" name="additional_fees" id="additional-fees" value="">
</div>
<?php
}
function edit_additional_fees_field($term, $taxonomy)
{
$additional_fees = get_term_meta($term->term_id, 'additional_fees', true);
?>
<tr class="form-field term-additional-fees-wrap">
<th scope="row"><label for="additional-fees"><?php esc_html_e('Additional Fees', 'woocommerce'); ?></label></th>
<td><input type="text" name="additional_fees" id="additional-fees" value="<?php echo esc_attr($additional_fees); ?>"></td>
</tr>
<?php
}
/**
* Display Additional Fees message in the cart
*/
add_action('woocommerce_cart_totals_before_order_total', 'display_additional_fees_in_cart');
function display_additional_fees_in_cart()
{
$product_id = get_the_ID();
$product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
// Iterate through the categories and get the additional fees
$additional_fees = 0;
foreach ($product_categories as $category_id) {
$category_additional_fees = get_term_meta($category_id, 'additional_fees', true);
if (!empty($category_additional_fees)) {
$additional_fees = max($additional_fees, $category_additional_fees);
}
}
// Display the additional fees if they exist
if ($additional_fees > 0) {
echo '<div class="woocommerce-notices-wrapper">';
echo '<ul class="woocommerce-error" role="alert">';
echo '<li>' . esc_html__('رسوم إضافية:', 'woocommerce') . ' ' . wc_price($additional_fees) . '</li>';
echo '</ul>';
echo '</div>';
}
}
/**
* Display Additional Fees message on the product page
*/
add_action('woocommerce_before_add_to_cart_button', 'display_additional_fees_message');
function display_additional_fees_message() {
global $product;
// Get the product categories
$product_categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids'));
// Iterate through the categories and get the additional fees
$additional_fees = 0;
foreach ($product_categories as $category_id) {
$category_additional_fees = get_term_meta($category_id, 'additional_fees', true);
if (!empty($category_additional_fees)) {
$additional_fees = max($additional_fees, $category_additional_fees);
}
}
// Get the vendor ID for this product
$vendor_id = get_post_field('post_author', $product->get_id());
// Check if there are products from other vendors in the cart
$other_vendors = false;
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$item_vendor_id = get_post_field('post_author', $product_id);
if ($item_vendor_id != $vendor_id) {
$other_vendors = true;
break;
}
}
// Display the message if additional fees exist and there are products from other vendors in the cart
if ($additional_fees > 0 && $other_vendors) {
?>
<script type="text/javascript">
(function($) {
$('form.cart').on('submit', function(e) {
e.preventDefault();
Swal.fire({
title: '<?php esc_html_e('رسوم إضافية', 'woocommerce'); ?>',
html: '<?php echo sprintf(esc_html__('سيتم تطبيق رسوم إضافية بقيمة %s على طلبك.', 'woocommerce'), wc_price($additional_fees)); ?>',
icon: 'warning',
confirmButtonText: '<?php esc_html_e('موافق', 'woocommerce'); ?>'
}).then((result) => {
if (result.isConfirmed) {
$(this).unbind('submit').submit();
}
});
});
})(jQuery);
</script>
<?php
}
}
add_action('wp_ajax_display_multi_vendor_warning', 'display_multi_vendor_warning');
add_action('wp_ajax_nopriv_display_multi_vendor_warning', 'display_multi_vendor_warning');
function display_multi_vendor_warning() {
$product_ids = $_POST['product_ids'];
$vendors = array();
foreach ($product_ids as $product_id) {
$vendor_id = get_post_field('post_author', $product_id);
$vendors[$vendor_id] = true;
}
if (count($vendors) > 1) {
$response = array(
'success' => true,
'message' => esc_html__('سلة التسوق الخاصة بك تحتوي على منتجات من عدة بائعين. قد تطبق رسوم إضافية.', 'woocommerce')
);
} else {
$response = array(
'success' => false,
'message' => ''
);
}
wp_send_json($response);
}
/**
* Display a warning message if cart contains products from multiple vendors
*/
function add_additional_fees_script_to_footer() {
?>
<script type="text/javascript">
(function($) {
var $woocommerce_div = $('div.woocommerce');
function check_and_display_multi_vendor_warning() {
var product_ids = [];
$('div.woocommerce-cart-form__cart-item').each(function() {
var $product_id = $(this).data('product_id');
product_ids.push($product_id);
});
$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: 'display_multi_vendor_warning',
product_ids: product_ids
},
success: function(response) {
if (response.success) {
$message = response.message;
if ($message) {
$woocommerce_div.prepend('<div class="woocommerce-notices-wrapper">' +
'<ul class="woocommerce-error" role="alert">' +
'<li>' + $message + '</li>' +
'</ul>' +
'</div>');
} else {
$woocommerce_div.find('.woocommerce-notices-wrapper').remove();
}
}
},
error: function() {
console.log('Error displaying multi-vendor warning');
}
});
}
$(document).ready(function() {
check_and_display_multi_vendor_warning();
});
$woocommerce_div.on('updated_wc_div', function() {
check_and_display_multi_vendor_warning();
});
})(jQuery);
</script>
<?php
}
add_action('wp_footer', 'add_additional_fees_script_to_footer');
//=========================================================================================================
function calculate_additional_fees() {
$additional_fees = 0;
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
foreach ($product_categories as $category_id) {
$category_additional_fees = get_term_meta($category_id, 'additional_fees', true);
if (!empty($category_additional_fees)) {
$additional_fees = max($additional_fees, $category_additional_fees);
}
}
}
return $additional_fees;
}
add_action('woocommerce_cart_calculate_fees', 'add_additional_fees_to_cart_total');
function add_additional_fees_to_cart_total(WC_Cart $cart) {
$additional_fees = calculate_additional_fees();
if ($additional_fees > 0) {
$cart->add_fee(__('رسوم إضافية', 'woocommerce'), $additional_fees);
}
}
//=================================================================================
/**
* Display additional fees in order details
*/
add_action('woocommerce_admin_order_data_after_order_details', 'display_additional_fees_order_meta');
function display_additional_fees_order_meta($order) {
$additional_fees = get_post_meta($order->get_id(), 'additional_fees', true);
if (!empty($additional_fees)) {
?>
<div class="order_data_column">
<h4><?php esc_html_e('Additional Fees', 'woocommerce'); ?></h4>
<p><?php echo wc_price($additional_fees); ?></p>
</div>
<?php
}
}
/**
* Save additional fees meta for the order
*/
add_action('woocommerce_checkout_create_order_line_item', 'save_additional_fees_order_meta', 10, 4);
function save_additional_fees_order_meta($item, $cart_item_key, $values, $order) {
$additional_fees = calculate_additional_fees();
if ($additional_fees > 0) {
$order->update_meta_data('additional_fees', $additional_fees);
}
}