Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create order #1930

Closed
infinitodk opened this issue Sep 1, 2024 · 8 comments · May be fixed by #1933
Closed

Unable to create order #1930

infinitodk opened this issue Sep 1, 2024 · 8 comments · May be fixed by #1933
Assignees
Labels
bug Something isn't working unconfirmed

Comments

@infinitodk
Copy link

  • Lunar version: 1.0-Beta
  • Laravel Version: 11
  • PHP Version: 8.2.1
  • Database Driver & Version:

Expected Behaviour:

Creating order from basket should work

Actual Behaviour:

Getting Shipping breakdown must be instance of Lunar\Base\ValueObjects\Cart\ShippingBreakdown.

Works in previously versions.

Steps To Reproduce:

// Create a new cart for the user
$cart = Cart::create([
'currency_id' => Currency::first()->id,
'channel_id' => Channel::first()->id,
'user_id' => $user->id,
]);

        $cart->lines()->create([
            'purchasable_type' => ProductVariant::class,
            'purchasable_id' => $this->product->variants()->first()->id,
            'quantity' => 1,
        ]);

        // Set the billing address for the cart
        $cart->setBillingAddress([
            'first_name' => $user->name,
            'last_name' => $user->name,
            'line_one' => '123',
            'city' => 'Greenified',
            'postcode' => 2600,
            'country_id' => 61,
        ]);

        // Create the order from the cart
        $order = $cart->createOrder();
@infinitodk infinitodk added bug Something isn't working unconfirmed labels Sep 1, 2024
@infinitodk infinitodk reopened this Sep 1, 2024
@infinitodk
Copy link
Author

public function boot(): void
{
$this->app['db.schema']->morphUsingUuids();

    ModelManifest::replace(ProductContract::class, \App\Models\Product::class);
    ModelManifest::replace(OrderContract::class, \App\Models\Order::class);
    ModelManifest::replace(CartContract::class, \App\Models\Cart::class);
}

removing these makes it work, all of the abovementioned models extern their respective Lunar counterparts

@theimerj
Copy link
Contributor

theimerj commented Sep 2, 2024

Hello, I also came across this problem and already spent hours debugging this problem already and I think I might have a solution as drafted here: #1932 and implemented here: #1933

@theimerj
Copy link
Contributor

theimerj commented Sep 3, 2024

@infinitodk can you try if it fixes your issues as well?

"require": {
    "lunarphp/lunar": "dev-update-container-resolution as 1.0.0-beta.1",
},
"repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/theimerj/lunar.git"
    }
]

@infinitodk
Copy link
Author

Will try, came to the same conclusion as you, that a serialization happened to early.

I also noticed that it references the classes wrong in polymorphic relationships aswell.

@theimerj
Copy link
Contributor

theimerj commented Sep 3, 2024

Will try, came to the same conclusion as you, that a serialization happened to early.

I also noticed that it references the classes wrong in polymorphic relationships aswell.

You are right, addressed by this commit: edb019f

@nathanjansen
Copy link

I have the same problem and installing your repository indeed solves the problem for now.

@alecritson
Copy link
Collaborator

We have a patch #1944 which should solve this issue in the next release 🤞

@theimerj
Copy link
Contributor

Unfortunatelly this does not solve all the problems introduced with the updated model extending as shown here: theimerj#2. I made a branch out of 1.x and updated the tests so they are run with all models extended.

@alecritson alecritson self-assigned this Sep 12, 2024
glennjacobs pushed a commit that referenced this issue Sep 17, 2024
This PR looks to solve some anomalies that have been discovered with
model extending.

## Problem A

When replacing a Lunar model, there are instances where the model
provided by Lunar is still used for example, if you replace the model
like so

```php
\Lunar\Facades\ModelManifest::replace(
    \Lunar\Models\Contracts\Order::class,
    \App\Models\Order::class
);
```

Later in the Lunar core, we are still referencing
`Lunar\Models\Order::first()` or via relationships
`$orderLine->order()`. We attempted to resolve these queries to their
concrete replacements by overriding the `newModelQuery` method, however
there were issues with castable attributes which caused them to be cast
multiple times, resulting in errors.

### Solution

This has been rewritten so Lunar doesn't try to `fill` the extended
instance of the model from the Lunar base class anymore and instead just
populates the attributes as they are. Since this should just be a simple
class swap it should no longer result in duplicate calls to attribute
casters.

There are some additional checks to see if we are actually working with
a model that hasn't been extended to ensure we are not getting into a
situation where we try and rehydrate with the same class.

### Affected issues

This change should resolve #1942 #1930 

## Problem B

If your own custom model was named something other than it's
counterpart, for example:

```php
\Lunar\Facades\ModelManifest::replace(
    \Lunar\Models\Contracts\Order::class,
    \App\Models\MyCustomOrder::class
);
```

This would result in the table name and subsequent foreign key naming to
be incorrect i.e. `my_custom_order` and `my_custom_order_id`. This would
mean developers would need to add their own methods to override this in
order for the naming to resolve properly, which is a bit of a
maintenance burden and easily missed when encountering errors.


### Solution

The `HasModelExtending` trait now provides its own `getTable` and
`getForeignKey` methods which will check which class within Lunar we are
extending and return the appropriate table name and foreign key.

## How this slipped through testing

Looks like there was an oversight and although there were tests for
extending models, the tests themselves didn't use any extending when
performing more complex tasks, like creating orders from carts.

This PR has now added some model extending to tests which were affected
by the issues referenced above to hopefully keep this in check and make
for a more "real world" test environment.

---------

Co-authored-by: alecritson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants