CartAddress.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Webkul\Checkout\Models;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Factories\Factory;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Webkul\Checkout\Contracts\CartAddress as CartAddressContract;
  9. use Webkul\Checkout\Database\Factories\CartAddressFactory;
  10. use Webkul\Core\Models\Address;
  11. /**
  12. * Class CartAddress
  13. *
  14. *
  15. * @property int $cart_id
  16. * @property Cart $cart
  17. */
  18. class CartAddress extends Address implements CartAddressContract
  19. {
  20. use HasFactory;
  21. /**
  22. * Define the address type shipping.
  23. */
  24. public const ADDRESS_TYPE_SHIPPING = 'cart_shipping';
  25. /**
  26. * Define the address type billing.
  27. */
  28. public const ADDRESS_TYPE_BILLING = 'cart_billing';
  29. /**
  30. * @var array default values
  31. */
  32. protected $attributes = [
  33. 'address_type' => self::ADDRESS_TYPE_BILLING,
  34. ];
  35. /**
  36. * The "booted" method of the model.
  37. */
  38. protected static function boot(): void
  39. {
  40. static::addGlobalScope('address_type', static function (Builder $builder) {
  41. $builder->whereIn('address_type', [
  42. self::ADDRESS_TYPE_BILLING,
  43. self::ADDRESS_TYPE_SHIPPING,
  44. ]);
  45. });
  46. parent::boot();
  47. }
  48. /**
  49. * Get the shipping rates for the cart address.
  50. */
  51. public function shipping_rates(): HasMany
  52. {
  53. return $this->hasMany(CartShippingRateProxy::modelClass());
  54. }
  55. /**
  56. * Get the cart record associated with the address.
  57. */
  58. public function cart(): BelongsTo
  59. {
  60. return $this->belongsTo(CartProxy::modelClass());
  61. }
  62. /**
  63. * Create a new factory instance for the model
  64. */
  65. protected static function newFactory(): Factory
  66. {
  67. return CartAddressFactory::new();
  68. }
  69. }