0

I have added a new field as "is_seller" in registration page (same as "is_subscribed). If the checkbox is clicked the customer Id will be saved to another table with some additional values.

It was done through the event. But using v1/customers how can I achieve this. When I tried to add it thorugh the parameters as below

"customer": {
        "email": "[email protected]",
        "firstname": "test,
        "lastname": "test",
        "store_id": 1,
        "website_id": 1,
        "is_seller":1
    },
     "password": "Password1"

I am getting the following error

Message: Property "IsSeller" does not have accessor method "getIsSeller" in class "Magento\Customer\Api\Data\CustomerInterface".

I tried using extension attributes also, but it is not working.

1
  • any soluction on this
    – Devidas
    Commented Jul 30, 2020 at 5:03

2 Answers 2

0

You need to create a module to override "Magento\Customer\Api\Data\CustomerInterface" interface and add your methods in it "getIsSeller" and "setIsSeller". Once this is done, override the model "Magento\Customer\Model\Data\Customer" and add the definition of these methods in it. once this is done, you can set and get this value to save and fetch from DB.

Don't forget the add the preferences for these files in di.xml file to use your module's files instead of original.

2
  • I have create a module and created extension_attribute.xml as <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface"> <attribute code="is_seller" type="Test\Selling\Data\SellerManagementInterface"/> </extension_attributes> and created interface with getIsSeller and setIsSeller functions. After that created a plugin following this article store.fooman.co.nz/blog/…. Now I am getting the following error "Fatal Error: 'Uncaught Error: Cannot instantiate interface Test\\Selling\\Api\\Data\\SellerManagementInterface
    – Kavi
    Commented Aug 26, 2019 at 5:06
  • Check the value of "type". API word is missing in the type path. Correct it and try once. Commented Aug 26, 2019 at 5:14
0

You have to implement extension_attribute for is_seller attribute then you have to enable to add is_seller to the database.

First, read the article https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html

First, you have to implement the service contract for your custom table to save the data.

Assume that

your data interface class is

StackExchange\ServiceContactTest\Api\Data\CustomInterface

And RepositoryInterface is StackExchange\ServiceContactTest\Api\CustomRepositoryInterface

First, you have to add is_seller as extension_attribute for data interface Magento\Customer\Api\Data\CustomerInterface.

Create extension_attributes.xml at app/code/{Vendor}/{ModuleName}/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="is_seller"  type="StackExchange\ServiceContactTest\Api\Data\CustomInterface" />
    </extension_attributes>
</config>

After that Using plugin on Magento\Customer\Api\CustomerRepositoryInterface, you need to save this custom table data.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Api\CustomerRepositoryInterface">
        <plugin name="save_seller_table" type="StackExchange\ServiceContactTest\Plugin\OrderSave"/>
    </type>
</config>

Plugin Class

<?php

namespace StackExchange\ServiceContactTest\Plugin;

use Magento\Framework\Exception\CouldNotSaveException;

class OrderSave
{

...

    public function afterSave(
        \Magento\Customer\Api\CustomerRepositoryInterface $subject,
        \Magento\Customer\Api\Data\CustomerInterface $resultOrder
    ) {
        $resultOrder = $this->saveFoomanAttribute($resultOrder);

        return $resultOrder;
    }

    private function saveFoomanAttribute(\Magento\Customer\Api\Data\CustomerInterface $customer)
    {
        $extensionAttributes = $customer->getExtensionAttributes();
        if (
            null !== $extensionAttributes &&
            null !== $extensionAttributes->getIsSeller()
        ) {
            $foomanAttributeValue = $extensionAttributes->getIsSeller()->getValue();
            try {
                // The actual implementation of the repository is omitted
                // but it is where you would save to the database (or any other persistent storage)
                $this->foomanExampleRepository->save($customer->getEntityId(), $foomanAttributeValue);
            } catch (\Exception $e) {
                throw new CouldNotSaveException(
                    __('Could not add attribute to order: "%1"', $e->getMessage()),
                    $e
                );
            }
        }
        return $customer;
    }
1
  • When I tried to print $extensionAttributes = $customer->getExtensionAttributes(); in the plugin I am getting null value. Following method is used for creating customer { "customer": { "email": "[email protected]", "firstname": "test", "lastname": test", "store_id": 1, "website_id": 1, "extension_attributes": { "is_seller": true } }, "password": "Password1" }
    – Kavi
    Commented Aug 27, 2019 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.