gruene-seele/app/Services/PromotionCart.php
2021-12-25 02:51:22 +01:00

113 lines
No EOL
3.6 KiB
PHP

<?php
namespace App\Services;
use Yard;
use App\Models\Product;
use App\Models\ShippingCountry;
class PromotionCart
{
public static $points = 0;
public static $price = 0;
public static $price_net = 0;
public static $ek_price = 0;
public static $income_price = 0;
public static function initYard(){
$id = 1;
if($ShippingCountry = ShippingCountry::all()->first()){
$id = $ShippingCountry->id;
}
Yard::instance('shopping')->setShippingCountryWithPrice($id, 'shop');
}
public static function getLowestShippingPrice($shipping_for = 2)
{
if($ShippingCountry = ShippingCountry::all()->first()){
if($ShippingCountry->shipping){
if($ShippingPrices = $ShippingCountry->shipping->getShippingPricesBy($shipping_for)->orderBy('price', 'ASC')->first()){
return formatNumber($ShippingPrices->price);
}
}
}
}
public static function clearCart($data)
{
Yard::instance('shopping')->destroy();
}
public static function switchShipping($data)
{
//pick_up//dhl_shipping
Yard::instance('shopping')->setShippingOption($data['shipping_option']);
Yard::instance('shopping')->reCalculateShippingPrice();
}
public static function updateProduct($data, $add=false)
{
if($product = Product::find($data['product_id'])){
$image = "";
if($product->images->count()){
$image = $product->images->first()->slug;
}
//get the card item
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->price, $product->tax,
[
'image' => $image,
'slug' => $product->slug,
'weight' => $product->weight,
]);
Yard::setTax($cartItem->rowId, $product->tax);
if(!$add){
if(isset($data['qty']) && $data['qty'] > 0){
Yard::instance('shopping')->update($cartItem->rowId, $data['qty']);
}else{
//if 0 get the item by qty:1 and remove it
Yard::instance('shopping')->remove($cartItem->rowId);
}
}
Yard::instance('shopping')->reCalculate();
return $cartItem->qty;
}
}
public static function updateFeeProduct($data)
{
foreach (Yard::instance('shopping')->content() as $row) {
//wenn kleiner wurde ein produkt entfernt aufgrund der Anzahl
//wenn gleich löschen, da neue Versandkosten
if($row->options->free_product_id) {
Yard::instance('shopping')->remove($row->rowId);
}
}
if(isset($data['free_product_id'])) {
if ($product = Product::find($data['product_id'])) {
$image = "";
if ($product->images->count()) {
$image = $product->images->first()->slug;
}
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, 0, 0,
[
'image' => $image,
'slug' => $product->slug,
'weight' => $product->weight,
'free_product_id' => intval($data['free_product_id']),
'product_id' => $product->id
]);
Yard::setTax($cartItem->rowId, 0);
}
}
}
}