Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shop api create shop customer subscribed to newsletter #627

Open
luxaloli opened this issue Jan 28, 2020 · 8 comments
Open

Shop api create shop customer subscribed to newsletter #627

luxaloli opened this issue Jan 28, 2020 · 8 comments

Comments

@luxaloli
Copy link

luxaloli commented Jan 28, 2020

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": "[email protected]",
  "plainPassword": "password",
  "firstName": "Jon",
  "lastName": "Doe",
  "channel": "Test",
  "subscribedToNewsletter":1
}

Any idea of what is missing?

@mamazu
Copy link
Member

mamazu commented Jan 28, 2020

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

@luxaloli
Copy link
Author

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

@mamazu
Copy link
Member

mamazu commented Jan 28, 2020

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

@luxaloli
Copy link
Author

luxaloli commented Jan 28, 2020

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
Copy link
Member

mamazu commented Jan 28, 2020

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

@luxaloli
Copy link
Author

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

@mamazu
Copy link
Member

mamazu commented Jan 29, 2020

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

@DennisdeBest
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants