Sylius / ShopApiPlugin

Shop API for Sylius.
https://sylius.com
129 stars 89 forks source link

Shop api create shop customer subscribed to newsletter #627

Open luxaloli opened 4 years ago

luxaloli commented 4 years ago

I want to add the value of subscribedToNewsLetter from the create customer endpoint so i added this class

<?php

namespace App\Request;

use App\Command\RegisterDaturaCustomer;
use Sylius\ShopApiPlugin\Request\Customer\RegisterCustomerRequest;
use Sylius\ShopApiPlugin\Command\CommandInterface;

final class RegisterDaturaCustomerRequest extends RegisterCustomerRequest
{
    /** @var bool */
    protected $subscribedToNewsletter;

    protected function __construct(Request $request, string $channelCode)
    {
        parent::__construct($request, $channelCode);

        $this->subscribedToNewsletter = $request->request->getBoolean('subscribedToNewsletter') ?? false;
    }

    public function getCommand(): CommandInterface
    {
        return new RegisterDaturaCustomer(
                $this->subscribedToNewsletter,
                $this->email,
                $this->plainPassword,
                $this->firstName,
                $this->lastName,
                $this->channelCode
            );
    }
}

then as your doc says added this on _sylius_shop_api.yml

sylius_shop_api:
    request_classes:
        register_customer: \App\Request\RegisterDaturaCustomerRequest

and also override the command interface, so this is the override class

<?php

namespace App\Command;

use Sylius\ShopApiPlugin\Command\Customer\RegisterCustomer;

class RegisterDaturaCustomer extends RegisterCustomer
{
    /** @var bool */
    protected $subscribedToNewsletter;

    /**
     * RegisterDaturaCustomer constructor.
     * @param bool $subscribedToNewsletter
     * @param string $email
     * @param string $plainPassword
     * @param string $firstName
     * @param string $lastName
     * @param string $channelCode
     */
    public function __construct(?bool $subscribedToNewsletter, string $email, string $plainPassword, string $firstName, string $lastName, string $channelCode)
    {
        $this->subscribedToNewsletter = $subscribedToNewsletter ?? false;
        parent::__construct($email, $plainPassword, $firstName, $lastName, $channelCode);
    }

    public function subscribedToNewsletter(): bool
    {
        return $this->subscribedToNewsletter;
    }
}

it runs with no problem other than subscribedToNewsletter is always false, i am using this json for the api

{
  "email": "user@example.com",
  "plainPassword": "password",
  "firstName": "Jon",
  "lastName": "Doe",
  "channel": "Test",
  "subscribedToNewsletter":1
}

Any idea of what is missing?

mamazu commented 4 years ago

What does $request->request->getBoolean('subscribedToNewsletter') return? Does it return true?

luxaloli commented 4 years ago

Ammm it never reaches that spot, as if never uses that overriden class.

mamazu commented 4 years ago

Does it show up when running bin/console debug:config sylius_shop_api?

luxaloli commented 4 years ago

Yes, it is the first one

sylius_shop_api:
    request_classes:
        register_customer: \App\Request\RegisterDaturaCustomerRequest
        add_coupon: Sylius\ShopApiPlugin\Request\Cart\AddCouponRequest
        add_product_review_by_code: Sylius\ShopApiPlugin\Request\Product\AddProductReviewByCodeRequest
        add_product_review_by_slug: Sylius\ShopApiPlugin\Request\Product\AddProductReviewBySlugRequest
        address_order: Sylius\ShopApiPlugin\Request\Checkout\AddressOrderRequest
        assign_customer_to_cart: Sylius\ShopApiPlugin\Request\Cart\AssignCustomerToCartRequest
        change_item_quantity: Sylius\ShopApiPlugin\Request\Cart\ChangeItemQuantityRequest
        choose_payment_method: Sylius\ShopApiPlugin\Request\Checkout\ChoosePaymentMethodRequest
        choose_shipping_method: Sylius\ShopApiPlugin\Request\Checkout\ChooseShippingMethodRequest
        complete_order: Sylius\ShopApiPlugin\Request\Checkout\CompleteOrderRequest
        drop_cart: Sylius\ShopApiPlugin\Request\Cart\DropCartRequest
        generate_reset_password_token: Sylius\ShopApiPlugin\Request\Customer\GenerateResetPasswordTokenRequest
        pickup_cart: Sylius\ShopApiPlugin\Request\Cart\PickupCartRequest
        remove_coupon: Sylius\ShopApiPlugin\Request\Cart\RemoveCouponRequest
        remove_item_from_cart: Sylius\ShopApiPlugin\Request\Cart\RemoveItemFromCartRequest
        resend_verification_token: Sylius\ShopApiPlugin\Request\Customer\ResendVerificationTokenRequest
        send_reset_password_token: Sylius\ShopApiPlugin\Request\Customer\SendResetPasswordTokenRequest
        set_default_address: Sylius\ShopApiPlugin\Request\AddressBook\SetDefaultAddressRequest
        update_customer: Sylius\ShopApiPlugin\Request\Customer\UpdateCustomerRequest
        verify_account: Sylius\ShopApiPlugin\Request\Customer\VerifyAccountRequest
mamazu commented 4 years ago

Very odd. I can't test it at the moment I would need to get back to you at this issue.

luxaloli commented 4 years ago

I tis weird also because on the view class already has the subscribedToNewsletter entry

mamazu commented 4 years ago

Yeah, I think there is probably some configuration missing or cache or something like this.

DennisdeBest commented 3 years ago

This seems to be the same problem as mine, it never runs through the custom request class. I have tried it in different ways (use the service ID instead of the class, adding the request and command classes as well as the handler in services, ...) but nothing seems to work. I hope to find a solution soon, if I do I will also post it here.