Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/vendor coupon distribution #2493

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
76 changes: 42 additions & 34 deletions includes/Order/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use WC_Order_Refund;
use WeDevs\Dokan\Cache;
use WeDevs\Dokan\Utilities\OrderUtil;
use WeDevs\Dokan\Vendor\Coupon;
use WP_Error;

/**
Expand Down Expand Up @@ -628,7 +629,7 @@ public function create_sub_order( $parent_order, $seller_id, $seller_products )
$this->create_taxes( $order, $parent_order, $seller_products );

// add coupons if any
$this->create_coupons( $order, $parent_order, $seller_products );
$this->create_coupons( $order, $parent_order );

$order->save(); // need to save order data before passing it to a hook

Expand Down Expand Up @@ -814,24 +815,18 @@ private function create_shipping( $order, $parent_order ) {
*
* @param WC_Order $order
* @param WC_Order $parent_order
* @param array $products
*
* @return void
*/
private function create_coupons( $order, $parent_order, $products ) {
private function create_coupons( $order, $parent_order ) {
if ( dokan()->is_pro_exists() && property_exists( dokan_pro(), 'vendor_discount' ) ) {
// 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 ) {
return $item->get_product_id();
}, $products
);
$parent_coupons = $parent_order->get_items( 'coupon' );

if ( ! $used_coupons ) {
if ( ! $parent_coupons ) {
return;
}

Expand All @@ -841,32 +836,45 @@ function ( $item ) {
return;
}

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

if (
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->set_props(
[
'code' => $item->get_code(),
'discount' => $item->get_discount(),
'discount_tax' => $item->get_discount_tax(),
]
);

$new_item->add_meta_data( 'coupon_data', $coupon->get_data() );
$parent_coupons = array_map(
function ( $coupon ) {
/** @var WC_Order_Item_Coupon $coupon */
return $coupon->get_code();
},
$parent_coupons
);

$order->add_item( $new_item );
$order_items = $order->get_items();
$used_coupons_data = [];
foreach ( $order_items as $order_item ) {
$item_coupons = $order_item->get_meta( Coupon::DOKAN_COUPON_META_KEY, true );
if ( ! is_array( $item_coupons ) ) {
continue;
}
foreach ( $item_coupons as $code => $item ) {
if ( ! isset( $used_coupons_data[ $code ] ) ) {
$used_coupons_data[ $code ] = 0;
}
if ( in_array( $code, $parent_coupons, true ) ) {
$used_coupons_data[ $code ] += $item['discount'];
}
}
}

foreach ( $used_coupons_data as $code => $total_discount ) {
$coupon = new \WC_Coupon( $code );
$coupon_item = new WC_Order_Item_Coupon();
$coupon_item->set_props(
[
'code' => $code,
'discount' => $total_discount,
'discount_tax' => 0,
]
);
$coupon_info = $coupon->get_short_info();
$coupon_item->add_meta_data( 'coupon_info', $coupon_info );
$coupon_item->add_meta_data( 'coupon_data', $coupon->get_data() );
$order->add_item( $coupon_item );
}
}

Expand Down
Loading
Loading