How to extend Shopware customer - plugins

I need to write a Shopware plugin that extends customer model with external_id field. This field should be editable through the admin UI and API.
I've found, that I can add attribute to user like so:
public function addAttributes() {
$this->Application()->Models()->addAttribute(
's_user_attributes',
'bla',
'externalId',
'INT(11)',
true,
null
);
$this->Application()->Models()->generateAttributeModels(array(
's_user_attributes'
));
}
What should I do to show this field in the UI and API?
PS: Shopware 5

addAttribute is deprecated in Shopware 5.
Use shopware_attribute.crud_service from the Shopware()-container instead.
$service = Shopware()->Container()->get('shopware_attribute.crud_service');
$service->update('s_user_attributes', 'bla', 'integer', [
'label' => '',
'supportText' => '',
'helpText' => '',
'translatable' => false,
'displayInBackend' => true,
'position' => 1
]);
Shopware()->Models()->generateAttributeModels(
array(
's_user_attributes'
)
);
If you set displayInBackend to true, you can edit it in the Backend, where the user is.
More at the Shopware Developer Guide.

For the backend you have to to do some ExtJs coding.
For the API you have to extend the REST-API endpoint for the user:
Shopware API erweitern

Related

Backpack for Laravel 'phone' field not working

I am using Backpack for Laravel to create an admin panel, however I am unable to make the 'phone' field show up as it is in the docs.
I have the Pro version, everything is up to date:
LARAVEL VERSION:
v9.45.0
BACKPACK VERSION:
5.4.11
Here is the field definition in the controller:
CRUD::addField([
'name' => 'telephone',
'label' => 'Telephone',
'type' => 'phone',
'config' => [
'onlyContries' => ['bd', 'cl', 'in', 'lv', 'pt', 'ro'],
'initialCountry' => 'cl',
'separateDialCode' => true,
'nationalMode' => false,
'autoHideDialCode' => false,
],
'wrapper' => [
'class' => 'col-md-4',
],
]);
The 'telephone' column in the database is varchar(20). I also tried with a phone column unsignedBigInt but it also resulted in the exact same behaviour.
Furthermore the above field does not go through the request validation, if that could be a hint as to what might be happening.
there is probably some misconfiguration that is breaking the field on your side, as I've just tested and everything seems to be working:
Possible causes that I could think from the top of my head are:
Don't have the minimum required PRO version: composer show backpack/pro should give you the installed version, the minimum required for phone field is 1.4.
Need to re-publish backpack assets. php artisan backpack:publish-assets
Field is not in fillable property of the model and/or isn't a fake field.
Let me know if you were able to solve it.
Cheers

Magento 2 - Programatically Add Credit Card into Vault (BrainTree)

I need to Add Credit Card details in to Vault Programmatically (BrainTree) in Magento 2.1.5
Basically what i want is After LoginIn there will be a separate section for Saved Cards . In that Customer is used to Add/edit/delete All his Credit card details.
the Below Code is used to list all the Credit Card saved by the Customer
use Magento\Vault\Api\PaymentTokenManagementInterface;
use Magento\Customer\Model\Session;
...
// Get the customer id (currently logged in user)
$customerId = $this->session->getCustomer()->getId();
// Card list
$cardList = $this->paymentTokenManagement->getListByCustomerId($customerId);
Now what i want is how to Add the Card Details to the Vault ?
Below is the Code to Add card in core php
$result = Braintree_Customer::create(array(
'firstName' => 'first name',
'lastName' => 'last name',
'company' => 'company',
'email' => 'xxxx#gmail.com',
'phone' => '1234567890',
'creditCard' => array(
'cardholderName' => 'xxx xxx',
'number' => '4000 0000 0000 0002 ',
'expirationMonth' => '10',
'expirationYear' => 2020,
'cvv' => '123',
'billingAddress' => array(
'firstName' => 'My First name',
'lastName' => 'My Last name'
)
)
));
But how can i do this same process in magento 2.
Thanks for the help
First, you have to create a payment token from the card data:
use Magento\Vault\Model\CreditCardTokenFactory;
...
$paymentToken = $this->creditCardTokenFactory->create();
$paymentToken->setExpiresAt('Y-m-d 00:00:00');
$paymentToken->setGatewayToken('card_112371K7-28BB-4O3X-CCG9-1034JHK27D88');
$paymentToken->setTokenDetails([
'type' => 'Visa',
'maskedCC' => '1111',
'expirationDate' => '06/2019'
]);
$paymentToken->setIsActive(true);
$paymentToken->setIsVisible(true);
$paymentToken->setPaymentMethodCode('your_payment_method_code');
$paymentToken->setCustomerId($customerId);
$paymentToken->setPublicHash($this->generatePublicHash($paymentToken));
Then you can save the payment token:
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
...
$this->paymentTokenRepository->save($paymentToken);
This is just an example you can start with. In a real world situation, you would also want to check that the token doesn't already exist, an also try a payment authorisation on the card to make sure it's actually usable and valid.
In order to check if a payment token exists or not, you can use this:
use Magento\Vault\Api\PaymentTokenManagementInterface;
...
$this->paymentTokenManagement->getByPublicHash( $paymentToken->getPublicHash(), $paymentToken->getCustomerId() );
You can have a look at the core Magento 2 classes mentioned here to know more about the functions available for payment token handling.
Good luck!
Replace objectManager when use in projects
<?php
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Vault\Model\AccountPaymentTokenFactory;
use Magento\Vault\Model\PaymentToken;
use Magento\Vault\Model\PaymentTokenRepository;
/** #var EncryptorInterface $encryptor */
$encryptor = $objectManager->get(EncryptorInterface::class);
/** #var PaymentToken $paymentToken */
$paymentToken = $objectManager->create(PaymentToken::class);
$paymentToken
->setCustomerId($customer->getId())
->setPaymentMethodCode('payflowpro')
->setType(AccountPaymentTokenFactory::TOKEN_TYPE_ACCOUNT)
->setGatewayToken('mx29vk')
->setPublicHash($encryptor->hash($customer->getId()))
->setTokenDetails(json_encode(['payerEmail' => 'john.doe#example.com']))
->setIsActive(true)
->setIsVisible(true)
->setExpiresAt(date('Y-m-d H:i:s', strtotime('+1 year')));
/** #var PaymentTokenRepository $tokenRepository */
$tokenRepository = $objectManager->create(PaymentTokenRepository::class);
$tokenRepository->save($paymentToken);

Add Entity Reference to Drupal 8 Form Field?

How can users add products to a simple plugin config using an autocomplete field?
I tried to use Config Entity but it looks the same as Form API (and I can't use entity fields there).
I was able to do this in Drupal 8 using the form API and the entity_autocomplete type.
$form['stories'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#title' => $this->t('Stories'),
'#description' => $this->t('Select some stories.'),
'#default_value' => $default_entities,
'#tags' => TRUE,
'#selection_settings' => array(
'target_bundles' => array('page', 'article'),
),
'#weight' => '0',
];
Use webform - there is an entity reference field.
This won't be useable for your purpose .. but you can check the source for sake.

How to prevent an empty form submission into CakePHP 2.5.2

I am new to CakePHP, how can i use javascript to prevent a form being submitted empty, i mean with all fields empty ?
The user just hit the submit button
I am using CakePHP 2.5.2
You don't need Javascript for such minor blank validation stuff. Just go through the validation section.
Check out this example below:
Model: User.php has the following validation code for email field.
class User extends AppModel {
public $validate = array(
'email' => array(
'required' => true,
'allowEmpty' => false,
'message' => 'Email cannot be empty.'
)
);
Setting required to true, and allowEmpty to false does the job for you. Also, the "message" field acts as the icing on the cake which indicates the error message to be shown when the validation fails.
Peace! xD

not getting email for custom field woocommerce

I am using woocommerce (free plugin).. I am trying to add one custom field to the billing fields..
here it is:
// ADDED HOW YOU GOT TO KNOW ABOUT OUR SERVICE FIELD
add_filter( 'woocommerce_checkout_fields' , 'About_Our_Service' );
// Our hooked in function - $fields is passed via the filter!
function About_Our_Service( $fields ) {
$fields['billing']['billing_meat'] = array(
'label' => __('How you Got to Know About Our Service?', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'google-ads' => __('Google', 'woocommerce' ),
'google-search' => __('Yahoo', 'woocommerce' ),
'warrior-forum' => __('Bing', 'woocommerce' ),
'facebook' => __('Facebook', 'woocommerce' ),
'other' => __('Other', 'woocommerce' ),
)
);
return $fields;
}
The problem is: I am not getting the value in my mail for the custom field which was added to the billing fields.. Anyone who already used woocommerce can help me on this... ?
I already created some more custom fields which was added to the checkout (BUT these're not added along with the core fields), for these fields i'm able to get values in my mail..
By the ay, i checked this thread: but didn't much info related to mail..
please kindly someone look into this..
For future readers, custom billing/shipping fields are saved as post meta for the order post. So in general, you can retrieve them with the typical WordPress get_post_meta() function.
But in WooCommerce 2.2, you don't need to as you can pass the field name directly to an array of fields that WC will show as a list in the email:
// pre-WooCommerce 2.3
function kia_email_order_meta_keys( $keys ) {
$keys['Some field'] = '_some_field';
return $keys;
}
add_filter('woocommerce_email_order_meta_keys', 'kia_email_order_meta_keys');
This method has been deprecated in version 2.3, probably so translation can be better. As of 2.3 you will need to target a different filter and send slightly different data.
// WooCommerce 2.3+
function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['some_field'] = array(
'label' => __( 'Some field', 'my-plugin-textdomain' ),
'value' => get_post_meta( $order->id, '_some_field', true );
);
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_keys', 10, 3 );
I wrote a tutorial on Customizing WooCommerce Checkout Fields
I believe this answer, in the codex is specifically meant for this purpose:
http://wcdocs.woothemes.com/snippets/add-a-custom-field-in-an-order-to-the-emails
I haven't implemented this myself but it's probably your best shot.