conedevelopment / bazar

Bazar is an e-commerce package for Laravel applications.
https://root.conedevelopment.com
MIT License
421 stars 56 forks source link

Discount for all products or only for one #186

Closed bobitza closed 2 years ago

bobitza commented 2 years ago

Is your feature request related to a problem? Please describe

I don't know how to add a discount that can be applied to all products, or save in db somewhere discount, for users to see the products with discounted applied on listings

bobitza commented 2 years ago
bobitza commented 2 years ago

And when i call ->toVariation() i dont have description of product, media, and parent elements, need to override somehow to to get with relations details of parent

iamgergo commented 2 years ago

After you extend the product model, do you override the container binding for the proxy as well?

iamgergo commented 2 years ago

You need to extend the base Product model. Here are the docs: https://bazar.conedevelopment.com/docs/contracts-container-and-proxies

bobitza commented 2 years ago

I extended, sorry bad code:

<?php

namespace App\Models\Bazar;

use \Illuminate\Database\Eloquent\Builder;
use Bazar\Models\Product as BaseProduct;

class Product extends BaseProduct
{

    /**
     * Scope the query only to the given categories in.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  array  $values
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeIncategories(Builder $query, array $values): Builder
    {
        return $query->whereHas('categories', static function (Builder $query) use ($values): Builder {
            return $query->whereIn($query->getModel()->qualifyColumn('id'), (array) $values);
        });
    }

}

Than i call directly from App\Models\Bazar\Product(), everything work i got response but media missing, and variants

bobitza commented 2 years ago

This is response directly to bazar product model:

Bazar\Models\Product {[#1298 ▼]()
  #appends: array:2 [[▼]()
    0 => "price"
    1 => "formatted_price"
  ]
  #attributes: array:10 [[▼]()
    "id" => 1
    "name" => "Blana naturala de vulpe polara alba"
    "slug" => "blana-vulpe-naturala"
    "description" => "<h4>Haina de blana naturala urs negru</h4><p> - blana este neagra neagra, iar la radacina este maro</p><p> - functie de lumina zilei sau a noptii, blitz-uri, bl[ ▶]()"
    "prices" => "{"ron":{"default":3000,"sale":2498.98},"usd":{"default":57.9,"sale":46.3},"eur":{"default":80.5,"sale":64.4}}"
    "properties" => "{"Size":["XS","S","M","L","XXL"],"Tags":["blana","urs negru","naturala"],"Colors":["negru","alb"]}"
    "inventory" => "{"files":[],"sku":"blana-vulpe-naturala-alb","width":300,"height":400,"length":200,"weight":2000,"quantity":5,"virtual":false,"downloadable":false}"
    "created_at" => "2022-02-07 14:03:39"
    "updated_at" => "2022-02-14 17:18:57"
    "deleted_at" => null
  ]
  #casts: array:4 [[▼]()
    "properties" => "json"
    "prices" => "Bazar\Casts\Prices"
    "inventory" => "Bazar\Casts\Inventory"
    "deleted_at" => "datetime"
  ]
  #fillable: array:6 [[▼]()
    0 => "name"
    1 => "slug"
    2 => "prices"
    3 => "inventory"
    4 => "properties"
    5 => "description"
  ]
  #table: "bazar_products"
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #original: array:10 [[▶]()]
  #changes: []
  #classCastCache: array:1 [[▼]()
    "prices" => Bazar\Casts\Prices {[#1275 ▶]()}
  ]
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #dispatchesEvents: []
  #observables: []
  #relations: array:3 [[▼]()
    "media" => Illuminate\Database\Eloquent\Collection {[#1302 ▼]()
      #items: array:4 [[▶]()]
      #escapeWhenCastingToString: false
    }
    "categories" => Illuminate\Database\Eloquent\Collection {[#1316 ▼]()
      #items: array:1 [[▶]()]
      #escapeWhenCastingToString: false
    }
    "variants" => Illuminate\Database\Eloquent\Collection {[#1323 ▶]()}
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [[▼]()
    0 => "*"
  ]
  #forceDeleting: false
}

and this is when i call with my function override extended bazar:

App\Models\Bazar\Product {[#1298 ▼]()
  #appends: array:2 [[▼]()
    0 => "price"
    1 => "formatted_price"
  ]
  #attributes: array:10 [[▼]()
    "id" => 1
    "name" => "Blana naturala de vulpe polara alba"
    "slug" => "blana-vulpe-naturala"
    "description" => "<h4>Haina de blana naturala urs negru</h4><p> - blana este neagra neagra, iar la radacina este maro</p><p> - functie de lumina zilei sau a noptii, blitz-uri, bl[ ▶]()"
    "prices" => "{"ron":{"default":3000,"sale":2498.98},"usd":{"default":57.9,"sale":46.3},"eur":{"default":80.5,"sale":64.4}}"
    "properties" => "{"Size":["XS","S","M","L","XXL"],"Tags":["blana","urs negru","naturala"],"Colors":["negru","alb"]}"
    "inventory" => "{"files":[],"sku":"blana-vulpe-naturala-alb","width":300,"height":400,"length":200,"weight":2000,"quantity":5,"virtual":false,"downloadable":false}"
    "created_at" => "2022-02-07 14:03:39"
    "updated_at" => "2022-02-14 17:18:57"
    "deleted_at" => null
  ]
  #casts: array:4 [[▼]()
    "properties" => "json"
    "prices" => "Bazar\Casts\Prices"
    "inventory" => "Bazar\Casts\Inventory"
    "deleted_at" => "datetime"
  ]
  #fillable: array:6 [[▼]()
    0 => "name"
    1 => "slug"
    2 => "prices"
    3 => "inventory"
    4 => "properties"
    5 => "description"
  ]
  #table: "bazar_products"
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #original: array:10 [[▶]()]
  #changes: []
  #classCastCache: array:1 [[▶]()]
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #dispatchesEvents: []
  #observables: []
  #relations: array:3 [[▼]()
    "media" => Illuminate\Database\Eloquent\Collection {[#1297 ▼]()
      #items: []
      #escapeWhenCastingToString: false
    }
    "categories" => Illuminate\Database\Eloquent\Collection {[#1305 ▼]()
      #items: array:1 [[▶]()]
      #escapeWhenCastingToString: false
    }
    "variants" => Illuminate\Database\Eloquent\Collection {[#1314 ▼]()
      #items: array:1 [[▶]()]
      #escapeWhenCastingToString: false
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [[▶]()]
  #forceDeleting: false
}
bobitza commented 2 years ago
kodyxgen commented 2 years ago

@bobitza Did you try calling the media() method on the product model ? $product->media()

bobitza commented 2 years ago

@kodyxgen this is when i call $product->media() image

bobitza commented 2 years ago

I cannot find images in cart::items anywhere, i want to knew how to show images in my cart only by Cart::getItem()

bobitza commented 2 years ago

One more problem with discount if i use in my code where I render cart with this code, I get a random discount on every page refresh :

            Discount::register('custom-percent', function (Discountable $model) {
                return $model->getTotal() * 0.3;
            });
            Cart::getModel()->calculateDiscount();
bobitza commented 2 years ago

These are discounts with 5 page refresh, and do nothing else:

Refresh 1:
[2022-02-18 08:43:04] local.DEBUG: 6051.28  -> getTotal()
[2022-02-18 08:43:04] local.DEBUG: 1815.384  -> getTotal() * 0.3

Refresh 2:
[2022-02-18 08:43:17] local.DEBUG: 6049.56   -> getTotal()
[2022-02-18 08:43:17] local.DEBUG: 1814.868    -> getTotal() * 0.3

Refresh 3:
[2022-02-18 08:43:35] local.DEBUG: 6050.07   -> getTotal()
[2022-02-18 08:43:35] local.DEBUG: 1815.021    -> getTotal() * 0.3

Refresh 4:
[2022-02-18 08:43:36] local.DEBUG: 6049.92   -> getTotal()
[2022-02-18 08:43:36] local.DEBUG: 1814.976    -> getTotal() * 0.3

Refresh 5:
[2022-02-18 08:43:37] local.DEBUG: 6049.96   -> getTotal()
[2022-02-18 08:43:37] local.DEBUG: 1814.988    -> getTotal() * 0.3

And prices on items are fix: image

What i observed discount stay stabilized only after 5 page refreshes :

[2022-02-18 08:46:40] local.DEBUG: 6049.95  
[2022-02-18 08:46:40] local.DEBUG: 1814.985  
[2022-02-18 08:46:42] local.DEBUG: 6049.95  
[2022-02-18 08:46:42] local.DEBUG: 1814.985  
[2022-02-18 08:46:43] local.DEBUG: 6049.95  
[2022-02-18 08:46:43] local.DEBUG: 1814.985  
[2022-02-18 08:46:43] local.DEBUG: 6049.95  
[2022-02-18 08:46:43] local.DEBUG: 1814.985  
iamgergo commented 2 years ago

Well, I've never had this issue. Are you sure it's only the discount and not the product prices that are changing because of some reason?

bobitza commented 2 years ago

This is happening if i use discount::register in controller, if i put register in app service boot, how i get with that name and use custom?

iamgergo commented 2 years ago

how i get with that name and use custom?

I don't understand what do you mean here. Can you provide an example?

bobitza commented 2 years ago

Where i plant register, and how i use to make discount on all cart price? For example i make discount::register('disc-1') and then how i use it for example: discount::use('disc-1') and than discount to apply to my cart only what is name 'disc-1' not other Discount::register

bobitza commented 2 years ago

What is the logic tu use a named discount ? I had been thinking you make a discount::register('discount-20'...) then you use in cart, Discount::get('discount-20') and that logic apply to my cart

iamgergo commented 2 years ago

It's automatic. You register the discount, and you specify the discount logic in the given Closure. When adding an item to the cart, it will be applied automatically. You don't need to call it manually.

bobitza commented 2 years ago

Ok than, but how can i make discount for users, i make 30 discounts code registered somewhere, and user insert discount code recieved, and that discount to apply to him cart

iamgergo commented 2 years ago

I think you are talking about coupons and not discounts. They are two different things. Bazar does not come with coupons by default, by you can create your own coupon system and intergrate it with Bazar's discount system.

bobitza commented 2 years ago

Ok i will try, thanks

One more question, try this code:

Discount::register('custom-30', function (Discountable $model) {
                              return (float) $model->getTotal() * 0.2;
                        });
Cart::getModel()->calculateDiscount();

Discount fluctuates if don't have param calculateDiscount(false);