简体   繁体   中英

Extending Shopware customers API to include all customer's addresses

I am new with Shopware plugin development, please how to call all addresses related to a single customer?

Shopware v5.4.*

You can find all addresses of a customer using the AddressRepository :

// or you can inject the session and models services
$userId = $this->container->get('session')->get('sUserId');
$addressRepository = $this->container->get('models')->getRepository(Shopware\Models\Customer\Address::class);

$addresses = $addressRepository->findBy(['customer' => $userId]);

$adresses will be an array of Address objects.

After some research and inspiration from Paweł Napierała , I found what I was searching for, here is the final result:

public function getOne($id)
{
    $result = parent::getOne($id);
    $addresses = $this->getManager()
            ->getRepository(\Shopware\Models\Customer\Address::class)
            ->findBy(['customer'=>$id]);
    $resultArray = $this->getManager()->toArray($addresses);
    $result ['addresses'] = $resultArray;
    return $result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM