Promotion Frontend dynamic
This commit is contained in:
parent
c9e1545693
commit
1cc8e025a1
29 changed files with 551 additions and 163 deletions
105
app/Services/PromotionCart.php
Normal file
105
app/Services/PromotionCart.php
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
<?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, $add=false)
|
||||
{
|
||||
Yard::instance('shopping')->destroy();
|
||||
}
|
||||
|
||||
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) {
|
||||
Yard::instance('shopping')->remove($row->rowId);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data['free_poduct_id'])) {
|
||||
if ($product = Product::find($data['free_poduct_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' => 0,
|
||||
'free_product' => 1,
|
||||
'product_id' => $product->id
|
||||
]);
|
||||
Yard::setTax($cartItem->rowId, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -323,36 +323,46 @@ class Yard extends Cart
|
|||
return;
|
||||
}
|
||||
$shipping = $shippingCountry->shipping;
|
||||
$shipping_price_for = 1;
|
||||
if($this->shipping_is_for === 'me' || $this->shipping_is_for === 'ot'){
|
||||
$shipping_price_for = 1;
|
||||
}
|
||||
if($this->shipping_is_for === 'shop'){
|
||||
$shipping_price_for = 2;
|
||||
}
|
||||
if($this->weight() == 0){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
$shipping_price = $shipping->getShippingPricesBy($shipping_price_for)->first();
|
||||
if(!$shipping_price){
|
||||
$shipping_price = new \App\Models\ShippingPrice();
|
||||
}
|
||||
$shipping_price->price = 0;
|
||||
$shipping_price->price_comp = 0;
|
||||
}else{
|
||||
//first by price
|
||||
$shipping_price = $this->shippingPriceBySubTotal($shipping->shipping_prices, $this->subtotal(2, '.', ''));
|
||||
$shipping_price = $this->shippingPriceBySubTotal($shipping->getShippingPricesBy($shipping_price_for), $this->subtotal(2, '.', ''));
|
||||
//sec by weight
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
|
||||
$shipping_price = $this->shippingPriceByWeight($shipping->getShippingPricesBy($shipping_price_for), $this->weight());
|
||||
}
|
||||
//default
|
||||
if(!$shipping_price){
|
||||
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
$shipping_price = $shipping->getShippingPricesBy($shipping_price_for)->first();
|
||||
}
|
||||
}
|
||||
if($shipping_price){
|
||||
$price = $shipping_price->price;
|
||||
$this->num_comp = 0;
|
||||
|
||||
if($this->shipping_is_for === 'me' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_me')){
|
||||
$price = $shipping_price->price_comp;
|
||||
$this->num_comp = $shipping_price->num_comp;
|
||||
if($this->weight() > 0){
|
||||
if($this->shipping_is_for === 'me' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_me')){
|
||||
$price = $shipping_price->price_comp;
|
||||
$this->num_comp = $shipping_price->num_comp;
|
||||
}
|
||||
if($this->shipping_is_for === 'ot' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_ot')){
|
||||
$price = $shipping_price->price_comp;
|
||||
$this->num_comp = $shipping_price->num_comp;
|
||||
}
|
||||
}
|
||||
if($this->shipping_is_for === 'ot' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_ot')){
|
||||
$price = $shipping_price->price_comp;
|
||||
$this->num_comp = $shipping_price->num_comp;
|
||||
}
|
||||
|
||||
$this->shipping_price = $price;
|
||||
$this->shipping_tax_rate = $shipping_price->tax_rate;
|
||||
$this->shipping_price_net = round($price / ((100+$shipping_price->tax_rate) / 100), 2);
|
||||
|
|
@ -584,7 +594,7 @@ class Yard extends Cart
|
|||
}
|
||||
|
||||
|
||||
public function getCartItemByProduct($product_id, $set_price='with'){
|
||||
public function getCartItemByProduct($product_id, $set_price='with', $commission=true){
|
||||
if($product = Product::find($product_id)) {
|
||||
$image = "";
|
||||
if ($product->images->count()) {
|
||||
|
|
@ -594,8 +604,8 @@ class Yard extends Cart
|
|||
if($set_price === 'with'){
|
||||
$price = $product->getPriceWith(false, true);
|
||||
}
|
||||
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price,
|
||||
[
|
||||
if($commission){
|
||||
$options = [
|
||||
'image' => $image,
|
||||
'slug' => $product->slug,
|
||||
'weight' => $product->weight,
|
||||
|
|
@ -603,8 +613,16 @@ class Yard extends Cart
|
|||
'amount_commission' => $product->amount_commission,
|
||||
'value_commission' => $product->value_commission,
|
||||
'partner_commission' => $product->partner_commission,
|
||||
]
|
||||
);
|
||||
];
|
||||
}else{
|
||||
$options = [
|
||||
'image' => $image,
|
||||
'slug' => $product->slug,
|
||||
'weight' => $product->weight,
|
||||
];
|
||||
}
|
||||
|
||||
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price, $options);
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($content->has($cartItem->rowId)){
|
||||
|
|
@ -635,11 +653,19 @@ class Yard extends Cart
|
|||
|
||||
}
|
||||
|
||||
public function rowPrice(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
return $this->numberFormat($row->price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function rowPriceNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
|
||||
return $this->numberFormat($price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function rowSubtotal(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
return $this->numberFormat(($row->price * $row->qty), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function rowSubtotalNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
|
||||
return $this->numberFormat(($price * $row->qty), $decimals, $decimalPoint, $thousandSeperator);
|
||||
|
|
@ -658,6 +684,15 @@ class Yard extends Cart
|
|||
return false;
|
||||
}
|
||||
|
||||
public function getFreeProductId(){
|
||||
foreach ($this->content() as $row) {
|
||||
if($row->options->free_product) {
|
||||
return $row->options->product_id;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getContentByOrder(){
|
||||
$ret = [];
|
||||
$comp = [];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue