Skip to content

Commit

Permalink
new: added a new filter hook named `dokan_should_copy_coupon_to_sub_o…
Browse files Browse the repository at this point in the history
…rder` so that we can decide if coupon should be copied to sub-order or not

fix: for vendor discount, coupon named got changed to coupon description, so we are removing that filter to revert the changes
  • Loading branch information
nurul-umbhiya committed Nov 5, 2023
1 parent 21e9fcf commit 7ae4116
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions includes/Order/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use WC_Order;
use WC_Order_Item_Coupon;
use WC_Order_Refund;
use WeDevs\Dokan\Cache;
use WeDevs\Dokan\Utilities\OrderUtil;
Expand Down Expand Up @@ -385,12 +386,13 @@ public function is_order_already_synced( $order_id ) {
*/
public function is_seller_has_order( $seller_id, $order_id ) {
global $wpdb;

return 1 === (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT 1 FROM {$wpdb->prefix}dokan_orders WHERE seller_id = %d AND order_id = %d LIMIT 1",
[ $seller_id, $order_id ]
)
);
$wpdb->prepare(
"SELECT 1 FROM {$wpdb->prefix}dokan_orders WHERE seller_id = %d AND order_id = %d LIMIT 1",
[ $seller_id, $order_id ]
)
);
}

/**
Expand Down Expand Up @@ -803,7 +805,7 @@ private function create_shipping( $order, $parent_order ) {
}

/**
* Create coupons for a sub-order if neccessary
* Create coupons for a sub-order if necessary
*
* @param WC_Order $order
* @param WC_Order $parent_order
Expand All @@ -812,6 +814,11 @@ private function create_shipping( $order, $parent_order ) {
* @return void
*/
private function create_coupons( $order, $parent_order, $products ) {
if ( dokan()->is_pro_exists() ) {
// remove vendor discount coupon code changes
remove_filter( 'woocommerce_order_get_items', [ dokan_pro()->vendor_discount->woocommerce_hooks, 'replace_coupon_name' ], 10 );
}

$used_coupons = $parent_order->get_items( 'coupon' );
$product_ids = array_map(
function ( $item ) {
Expand All @@ -830,17 +837,19 @@ function ( $item ) {
}

foreach ( $used_coupons as $item ) {
/**
* @var WC_Order_Item_Coupon $item
*/
$coupon = new \WC_Coupon( $item->get_code() );

if (
$coupon &&
! is_wp_error( $coupon ) &&
apply_filters( 'dokan_should_copy_coupon_to_sub_order', true, $coupon, $item, $order ) &&
(
array_intersect( $product_ids, $coupon->get_product_ids() ) ||
apply_filters( 'dokan_is_order_have_admin_coupon', false, $coupon, [ $seller_id ], $product_ids )
)
) {
$new_item = new \WC_Order_Item_Coupon();
$new_item = new WC_Order_Item_Coupon();
$new_item->set_props(
[
'code' => $item->get_code(),
Expand Down
2 changes: 1 addition & 1 deletion templates/orders/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
</table>

<?php
$coupons = $order->get_items( array( 'coupon' ) );
$coupons = $order->get_items( 'coupon' );

if ( $coupons ) {
?>
Expand Down

0 comments on commit 7ae4116

Please sign in to comment.