Sylius: Customer's Shipping and Billing address not hydrated during checkout/adressing

Created on 21 Aug 2016  路  7Comments  路  Source: Sylius/Sylius

Hi

The addressing step does not provide the current user/customer shipping and billing address, so the addressing form is always empty, even when the customer has both addresses stored.

This is not a problem of user refresh since logging out then in doesn't change anything.

Or is there a walkaround for this ?
Thanks!

Feature

Most helpful comment

Yes I can modify the view but then the templates are not DRY and it's not a good way of doing this. Should really be the default in Sylius I think.

All 7 comments

You can modify the view to achieve this, but yes i feel like it should be the default. You would want the default value to be the customers previously entered values. I would do the code i just don't know where to begin or where to make the changes. I feel it should either be in the form type or the controller but not 100% sure

Yes I can modify the view but then the templates are not DRY and it's not a good way of doing this. Should really be the default in Sylius I think.

BTW guys how did you do the modification in the view ? Thanks

I didn't, and I'm now using Sylius default way with the address book. I just added a bit of JS to have the defautl address filled in when the page loads

I added a form listener that prepopulates the data based on the default address.

In my site i removed the shipping step. But here is my code below

```

namespace AdsUpBundleLedBundleFormEventSubscriber;

use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentFormFormEvent;
use SymfonyComponentFormFormEvents;
use AdsUpBundleLedBundleEntityCustomer;

class AddDefaultCheckoutAddressValuesOnAddressFormSubscriber implements EventSubscriberInterface
{
/**
* @var Customer
*/
private $customer;

private $addressFactory;

/**
 * @param Customer $customer
 */
public function __construct($customer, $addressFactory)
{
    $this->customer = $customer;
    $this->addressFactory = $addressFactory;
}
/**
 * {@inheritdoc}
 */
public static function getSubscribedEvents()
{
    return [
        FormEvents::PRE_SET_DATA => 'preSetData',
    ];
}

/**
 * @param FormEvent $event
 */
public function preSetData(FormEvent $event)
{
    $order = $event->getData();

    if (null == $order->getBillingAddress() && $this->customer->getDefaultAddress()) {
        $newAddress = clone $this->customer->getDefaultAddress();

        $order->setBillingAddress($newAddress);
    }

    if (null == $order->getShippingAddress() && $this->customer->getDefaultAddress()) {
        $newAddress = clone $this->customer->getDefaultAddress();

        $order->setShippingAddress($newAddress);
    }

    $event->setData($order);
}

}
```

Thanks, i will try this.

Closing this now that the address book has been revamped

Was this page helpful?
0 / 5 - 0 ratings