duncanmcclean / simple-commerce

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

Cart View throwing Call to a member function id() on null #930

Closed jamie-b5 closed 10 months ago

jamie-b5 commented 10 months ago

Description

We upgraded to Simple Commerce 5.6 to take advantage of the filed conditions in variant products.

We now receive the following error when trying to advance to the checkout page:

Call to a member function id() on null

The debug info highlights the error is occurring at DoubleThreeDigital\SimpleCommerce\Tax\Standard\TaxEngine: 141

image

Steps to reproduce

Upgrade to Simple Commerce 5.4 or higher (happens in 5.4 and 5.6 from my testing)

Add a product to the basket

Proceed to checkout.

After selecting the shipping method the above error displays.

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, PayPal Repository: Customer: DoubleThreeDigital\SimpleCommerce\Customers\EntryCustomerRepository Repository: Order: DoubleThreeDigital\SimpleCommerce\Orders\EntryOrderRepository Repository: Product: DoubleThreeDigital\SimpleCommerce\Products\EntryProductRepository Shipping Methods: Free Overnight Shipping, Click and Collect Tax Engine: DoubleThreeDigital\SimpleCommerce\Tax\Standard\TaxEngine

Statamic Addons: 3 Antlers: runtime Stache Watcher: Enabled Static Caching: Disabled Version: 4.21.0 PRO

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

duncanmcclean commented 10 months ago

Hey 👋

Which version are you upgrading from?

Can you zip up your content/simple-commerce directory and upload it here or send it to duncan@doublethree.digital.

jamie-b5 commented 10 months ago

Hi Duncan

We upgraded from v 4.5.8 to 5.4 on Sept 12 (related to this issue: https://github.com/duncanmcclean/simple-commerce/issues/913) - unfortunately whilst that resolved the issue with the conditional fields on variants it seems to have caused this cart/tax engine issue.

I'll email you the files/folders you requested.

Thanks for looking at this so quickly!

duncanmcclean commented 10 months ago

Thanks! Are you also able to paste the contents of your Simple Commerce config (config/simple-commerce.php)?

jamie-b5 commented 10 months ago

`<?php

return [

/*
|--------------------------------------------------------------------------
| Sites
|--------------------------------------------------------------------------
|
| You may configure a different currency & different shipping methods for each
| of your 'multi-site' sites.
|
| https://simple-commerce.duncanmcclean.com/multi-site
|
*/

//\DoubleThreeDigital\SimpleCommerce\Shipping\StandardPost::class => [],
//\App\ShippingMethods\FreeOvernightShippingMethod::class => [],

'sites' => [
    'default' => [
        'currency' => 'GBP',

        'shipping' => [
            'methods' => [
                \App\ShippingMethods\FreeOvernightShippingMethod::class => [],
                \App\ShippingMethods\FreeCollectionShippingMethod::class => [],

            ],
        ],
    ],
],

/*
|--------------------------------------------------------------------------
| Payment Gateways
|--------------------------------------------------------------------------
|
| This is where you configure the payment gateways you wish to use across
| your site. You may configure as many as you like.
|
| https://simple-commerce.duncanmcclean.com/gateways
|
*/

/*
//getting order data for Stripe
if(isset($order->data['billing_postal_code']) && !empty($order->data['billing_postal_code'])){
    $post_code = $order->data['billing_postal_code'];
} else {
    $post_code = $order->data['shipping_postal_code'];
}*/

'gateways' => [
    \DoubleThreeDigital\SimpleCommerce\Gateways\Builtin\StripeGateway::class => [
        'key' => env('STRIPE_KEY'),
        'secret' => env('STRIPE_SECRET'),
        'payment_intent_data' => function ($order) {

            return [
                'metadata' => [
                    //'product_ids' => $order->lineItems()->pluck('product')->join(', '),
                    //'postalCode' => $post_code,
                ],
                'automatic_payment_methods' => [
                    'enabled' => true,
                ],

                'setup_future_usage' => null,

            ];
        },
    ],

    \DoubleThreeDigital\SimpleCommerce\Gateways\Builtin\PayPalGateway::class => [
        'client_id' => env('PAYPAL_CLIENT_ID'),
        'client_secret' => env('PAYPAL_CLIENT_SECRET'),
        'environment' => env('PAYPAL_ENVIRONMENT', 'sandbox'), //sandbox or production
        'mode' => 'onsite', // Either: offsite OR onsite
    ],

    /*\DoubleThreeDigital\SimpleCommerce\Gateways\Builtin\DummyGateway::class => [
        'display' => 'Card',
    ],*/

],

/*'gateways' => [
    \DoubleThreeDigital\SimpleCommerce\Gateways\Builtin\DummyGateway::class => [
        'display' => 'Card',
    ],
],*/

/*
|--------------------------------------------------------------------------
| Notifications
|--------------------------------------------------------------------------
|
| Simple Commerce is able to send notifications after certain 'events' happen,
| like an order being marked as paid. You may configure these notifications
| below.
|
| https://simple-commerce.duncanmcclean.com/notifications
|
*/

'notifications' => [
    'order_paid' => [
        \App\Notifications\OrderPaidNotification::class => [
            'to' => 'customer',
        ],

        \DoubleThreeDigital\SimpleCommerce\Notifications\BackOfficeOrderPaid::class => [
            'to' => env('ORDER_EMAIL'),
        ],
    ],

    'order_shipped' => [
        \App\Notifications\OrderShippedNotification::class => ['to' => 'customer'],
    ],
],

/*
|--------------------------------------------------------------------------
| Field Whitelist
|--------------------------------------------------------------------------
|
| You may configure the fields you wish to be editable via front-end forms
| below. Wildcards are not accepted due to security concerns.
|
| https://simple-commerce.duncanmcclean.com/tags#field-whitelisting
|
*/

'field_whitelist' => [
    'orders' => [
        'shipping_name', 'shipping_address', 'shipping_address_line1', 'shipping_address_line2', 'shipping_city',
        'shipping_region', 'shipping_postal_code', 'shipping_country', 'shipping_note', 'shipping_method',
        'use_shipping_address_for_billing', 'billing_name', 'billing_address', 'billing_address_line2',
        'billing_city', 'billing_region', 'billing_postal_code', 'billing_country',
    ],

    'line_items' => [],

    'customers' => ['name', 'email'],
],

/*
|--------------------------------------------------------------------------
| Tax
|--------------------------------------------------------------------------
|
| Configure the 'tax engine' you'd like to be used to calculate tax rates
| throughout your site.
|
| https://simple-commerce.duncanmcclean.com/tax
|
*/

'tax_engine' => \DoubleThreeDigital\SimpleCommerce\Tax\Standard\TaxEngine::class,

'tax_engine_config' => [
    // Basic Engine
    'rate' => 20,
    'included_in_prices' => false,

    // Standard Tax Engine
    'address' => 'billing',

    'behaviour' => [
        'no_address_provided' => 'default_address',
        'no_rate_available' => 'prevent_checkout',
    ],

    'default_address' => [
        'address_line_1' => '',
        'address_line_2' => '',
        'city' => '',
        'region' => '',
        'country' => '',
        'zip_code' => '',
    ],
],

/*
|--------------------------------------------------------------------------
| And some more settings, that probably don't need much describing..
|--------------------------------------------------------------------------
*/

// Low Stock Threshold

'low_stock_threshold' => 10,

// Content Repositories

'content' => [
    'customers' => [
        'repository' => \DoubleThreeDigital\SimpleCommerce\Customers\EntryCustomerRepository::class,
        'collection' => 'customers',
    ],

    'orders' => [
        'repository' => \DoubleThreeDigital\SimpleCommerce\Orders\EntryOrderRepository::class,
        'collection' => 'orders',
    ],

    'products' => [
        'repository' => \DoubleThreeDigital\SimpleCommerce\Products\EntryProductRepository::class,
        'collection' => 'products',
    ],
],

]; `

jamie-b5 commented 10 months ago

Anything else - let me know. Thanks.

duncanmcclean commented 10 months ago

When you upgraded, it seems like the CreateShippingTaxCategory upgrade script didn't run which means you're missing an extra tax rate & tax category.

Add this to your routes/console.php file:

Artisan::command('create-missing-shipping-stuff', function () {
    (new \DoubleThreeDigital\SimpleCommerce\UpdateScripts\v5_0\CreateShippingTaxCategory('doublethreedigital/simple-commerce'))->update();
});

Then run php artisan create-missing-shipping-stuff

jamie-b5 commented 10 months ago

Brilliant that seems to have done the trick! Thanks again for the fab customer service!

I also noticed an additional (unrelated possibly) issue with our use of the Stripe payment gateway.

The _stripe.antlers.html file uses the now wrong variable names

var stripe = Stripe('{{ gateway-config:key }}'); var cart_total = {{sc:cart:raw_grand_total}}; var clientSecret = '{{ client_secret }}'; var returnUrl = 'http://sound_org.test/checkout/complete';

It looks like these need to be updated to

var stripe = Stripe('{{ stripe:config:key }}'); var cart_total = {{sc:cart:raw_grand_total}}; var clientSecret = '{{ stripe:client_secret }}'; var returnUrl = 'http://sound_org.test/checkout/complete';

But a minor fix! Thanks again.

Jamie

duncanmcclean commented 10 months ago

No worries!

If you haven't already, I'd recommend reviewing the v5 upgrade guide - the change in variables was documented in there.

jamie-b5 commented 10 months ago

I also just spotted the requirement for adding

'mode' => 'card_elements',

into the Stripe config too! Thanks again