duncanmcclean / simple-commerce

A simple, yet powerful e-commerce addon for Statamic.
https://statamic.com/addons/duncanmcclean/simple-commerce
Other
147 stars 42 forks source link

Unable to retrieve the dispatched date from the status log #1055

Closed jamie-b5 closed 7 months ago

jamie-b5 commented 7 months ago

Description

We have upgraded to 6.1.0 but we are now unable to get the dispatched date from the status_log array.

We can display the array in the cp index field list (not great as it shows as an array).

Prior to update we used computed fields to pull out the item from the array

Collection::computed('orders', 'dispatched_date', function ($entry, $value) {

        $mydate = null;

       if(isset($entry->status_log['dispatched'])){
            $mydate = $entry->status_log['dispatched'];
        }

        return $mydate;
    });

This now throws an error Object of class DuncanMcClean\SimpleCommerce\Orders\StatusLogEvent could not be converted to string

On the migration guide https://simple-commerce.duncanmcclean.com/upgrade-guides/v5-x-to-v6-0#content-medium-changes-to-the-statuslog-method-on-orders

It explains how to use the new method for obtaining this information.

However when I replace my old code with the new method as per below

Collection::computed('orders', 'dispatched_date', function ($entry, $value) {

        $mydate = null;

        $mydate = $entry->statusLog()
        ->where('status', OrderStatus::Dispatched)
        ->map(fn ($statusLogEvent) => $statusLogEvent->data()->timestamp)
        ->last();

        //old code commented out
        //if(isset($entry->status_log['dispatched'])){
        //    $mydate = $entry->status_log['dispatched'];
        //}

        return $mydate;
    });

we then get the new error Call to undefined method Statamic\Entries\Entry::statusLog()

Steps to reproduce

  1. Upgrade to 6.1.0
  2. Create a computed field to hold the Status Log data item (dispatched date in our case)
  3. Implement the above new code in the AppServiceProvider.php to generate a return value for the computed field

Environment

Environment
Application Name: Sound Organisation
Laravel Version: 10.22.0
PHP Version: 8.1.10
Composer Version: 2.4.1
Environment: local
Debug Mode: ENABLED
URL: sound_org.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Simple Commerce
Currencies: GBP
Gateways: Stripe
Repository: Customer: DuncanMcClean\SimpleCommerce\Customers\EntryCustomerRepository
Repository: Order: DuncanMcClean\SimpleCommerce\Orders\EntryOrderRepository
Repository: Product: DuncanMcClean\SimpleCommerce\Products\EntryProductRepository
Shipping Methods: Free Overnight Shipping, Click and Collect
Tax Engine: DuncanMcClean\SimpleCommerce\Tax\Standard\TaxEngine

Statamic
Addons: 3
Antlers: runtime
Sites: 1
Stache Watcher: Disabled
Static Caching: Disabled
Version: 4.55.0 PRO

Statamic Addons
duncanmcclean/simple-commerce: 6.1.0
jezzdk/statamic-google-maps: 1.2.2
rias/statamic-data-import: 1.3.0

duncanmcclean commented 7 months ago

Can you try something like this instead?

use DuncanMcClean\SimpleCommerce\Facades\Order;
use DuncanMcClean\SimpleCommerce\Orders\OrderStatus;

$order = Order::find($entry->id());

return $order?->statusLog()->where('status', OrderStatus::Dispatched)->map->date()->last();
jamie-b5 commented 7 months ago

How do I combine that with computed fields?

jamie-b5 commented 7 months ago

Apologies I edited the original entry as I had put in the wrong error message

we then get the new error Call to undefined method Statamic\Entries\Entry::statusLog()

jamie-b5 commented 7 months ago

Bear with I think I can see how

jamie-b5 commented 7 months ago

Brilliant - solved!

duncanmcclean commented 7 months ago

How do I combine that with computed fields?

Oh sorry, should have included that. You can just put that code inside the computed closure. I copied it from Simple Commerce's service provider: https://github.com/duncanmcclean/simple-commerce/blob/6.x/src/ServiceProvider.php#L470-480