1

I'm currently setting up a contact form in venia storefront and trying to get it to link to my rest api in magento 2. On button click the forms linking to the actions file and then throwing a not route found error when searching for the API, I've done the same method for other parts in the site such as the newsletter subscriptions and the add to cart functions etc.

Just hoping someone could point out where I've gone wrong this time as I've probably just overlooked something obvious.

My current API file:

<?php
namespace File\Custom\Api;

use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerExtensionInterface;
use Magento\Customer\Api\Data\CustomerExtensionFactory;

class ContactManagement implements \File\Custom\Api\ContactManagementInterface
{

  protected $_storeManager;
  protected $_transportBuilder;
  protected $_request;
  protected $storeManager;
  protected $customerRepository;

  public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager,
                              \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
                              \Magento\Framework\App\RequestInterface $request,
                              CustomerRepository $customerRepository
  )
  {
    $this->_request = $request;
    $this->storeManager = $storeManager;
    $this->customerRepository = $customerRepository;
    $this->_transportBuilder = $transportBuilder;
  }


  public function postContact(CustomerInterface $result, RequestInterface $request)
  {

    $store = $this->_storeManager->getStore()->getId();
    $transport = $this->_transportBuilder->setTemplateIdentifier('suttonsilver_custom_customer_admin_reseller_email')
      ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
      ->setTemplateVars(
        [
          'name' => $result->getFirstname().' '.$result->getLastname(),
          'email' => $result->getEmail(),
          'phone' => $result->getPhone(),
          'interest' => $result->getInterest(),
          'message' => $result->getMessage()
        ]
      )
      ->setFrom('general')
      // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
      ->addTo('email@test.com', 'Name')
      ->getTransport();

    $transport->sendMessage();

    return $result;

  }
}

My action setup in venia storefront

export const contactForm = payload =>
  async function thunk(...args) {
    const [dispatch, getState] = args;

    dispatch(actions.resetContactFormError.receive());
    dispatch(actions.resetContactFormSuccess.receive());


    try {
      const response = await request(
        '/rest/V1/contact',
        {
          method: 'POST',
          body: JSON.stringify(payload)
        }
      );

      await dispatch(getUserDetails())
      await dispatch(actions.contactFormSuccess.receive({message: response}));
    } catch (error) {
      dispatch(actions.contactFormError.receive({message: response}));
    }
  };

Webapi.xml link file

<route url="/V1/contact" method="POST">
    <service class="File\Custom\Api\ContactManagementInterface" method="postContact"/>
    <resources>
        <resource ref="self" />
    </resources>
    <data>
        <parameter name="customerId" force="true">%customer_id%</parameter>
    </data>
</route>
  • were you able to track down a solution for this? – circlesix Oct 31 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.