Skip to content

Commit

Permalink
fix Upselling
Browse files Browse the repository at this point in the history
  • Loading branch information
xxl4 committed Aug 9, 2024
1 parent 07261a3 commit a10ad5c
Showing 1 changed file with 99 additions and 7 deletions.
106 changes: 99 additions & 7 deletions src/Upselling.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
use Webkul\Checkout\Models\CartAddress;
use Webkul\Tax\Helpers\Tax;
use Illuminate\Support\Facades\Log;
use NexaMerchant\Apis\Docs\V1\Shop\Models\Customer\CartItem;
use Webkul\Checkout\Models\CartItem;
use Webkul\Checkout\Facades\Cart;

use Webkul\Sales\Repositories\OrderRepository;

class Upselling {

private $cart;

public function __construct(
protected CartRepository $cartRepository,
protected OrderRepository $orderRepository,
protected CartItemRepository $cartItemRepository,
protected CartAddressRepository $cartAddressRepository,
protected ProductRepository $productRepository,
Expand Down Expand Up @@ -75,26 +79,114 @@ public function applyUpselling($cart) {
// }

//check the email of the customer have processing order
$customer = auth()->guard('customer')->user();
//$customer = auth()->guard('customer')->user();

//check the email of the customer have processing order in 24 hours
//$cart->customer_email = '[email protected]'; // for testing
$processingOrder = $this->orderRepository->findOneWhere([
'customer_email' => "[email protected]",
'status' => 'processing',
]);
//order findwhere created_at >= now()->subDay()


Log::info('Processing order found '.json_encode($processingOrder));
Log::info("Processing Data:". now()->subDay());


if($processingOrder) {
//Log::info('Processing order found '.json_encode($processingOrder));

Cart::saveShippingMethod('free_free');

foreach ($cart->items as $item) {
$itemCartRuleIds = $this->process($item);
}

// free shipping for the items


foreach ($cart->items as $item) {
//$itemCartRuleIds = $this->process($item);
//Cart::collectTotals();

Log::info('Processing order cart found '.json_encode($cart));
}



}

/**
* Cart item discount calculation process
*
* @param \Webkul\Checkout\Models\CartItem $item
* @return array
*/

public function process(CartItem $item): array
{
// Process cart item
//Log::info('Cart item processed '.json_encode($cartItem));
$item->discount_percent = 0;
$item->discount_amount = 0;
$item->base_discount_amount = 0;
//$item->discount_percent = 0;
//$item->discount_amount = 0;
//$item->base_discount_amount = 0;

Log::info('Upselling Discount applied old '.json_encode($item));

$appliedRuleIds = [];

// 50% discount for the items
$rule = new \stdClass();
$rule->discount_amount = 50;
$rule->discount_quantity = 0;
$rule->discount_step = 0;
$rule->discount_type = 'percent';
$rule->quantity = 1;

$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;

$discountAmount = $baseDiscountAmount = 0;


$rulePercent = min(100, $rule->discount_amount);

$discountAmount = ($quantity * $item->price + $item->tax_amount - $item->discount_amount) * ($rulePercent / 100);

$baseDiscountAmount = ($quantity * $item->base_price + $item->base_tax_amount - $item->base_discount_amount) * ($rulePercent / 100);


$item->discount_amount = min(
$item->discount_amount + $discountAmount,
$item->price * $quantity + $item->tax_amount
);
$item->base_discount_amount = min(
$item->base_discount_amount + $baseDiscountAmount,
$item->base_price * $quantity + $item->base_tax_amount
);

Log::info('Upselling Discount applied new '.json_encode($item));


$item->save();

return $appliedRuleIds;

//$this->processFreeShipping();


}


public function processFreeShipping() {
// Process shipping discount
// free shipping for the items

$cart = $this->cart;
$cart->shipping_discount_amount = 0;
$cart->base_shipping_discount_amount = 0;


Log::info('Free Shipping processed '.json_encode($cart));
$cart->save();

}
}

0 comments on commit a10ad5c

Please sign in to comment.