Friday, July 21, 2017

Magento 2 Enable Product on Import

Problem:
How to enable (or explicitly disable) a product using Magento 2 out-of-the-box product import.

Solution:
Include column product_online in the import and set values (1=enable, 0=disable) accordingly. Note that we'd expect this column to be called status instead since this is the attribute name. But adding column status instead does not work.

Friday, May 26, 2017

Magento 2.1 SOAP API Basics

Version: Magento 2.1+

The Magento SOAP API is made up of many services. Each service contains API endpoints.

Services that return sensitive data are secure and require a security token to access e.g. salesOrderRepositoryV1GetList. Services that do not return sensitive information do not require a token and are publicly available e.g. directoryCountryInformationAcquirerV1GetCountriesInfo

List of anonymous guest services is here: /soap/default?wsdl_list=1

Magento admin > Configuration > Services > Web API Security > Allow Anonymous Guest Access = Yes will allow additional anonymous guest services to be accessible. These services may return somewhat sensitive data e.g. cmsPageRepositoryV1.

Specify the service in the WSDL url. Specify multiple services in the WSDL url as needed. e.g. /soap/default?wsdl&services=customerCustomerRepositoryV1,salesOrderRepositoryV1

A token can be obtained by creating in Magento admin > System > Integrations > Create New Integration. Use the Access Token in the header of SOAP calls. e.g. $opts = ['http' => ['header' => "Authorization: Bearer " . $token]];

A token can also be obtained by creating a Magento admin user and then requesting a token based on the username/password e.g. $token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"myusername", "password"=>"mypassword"));

Calling SOAP APIs from a PHP script may cache WSDL files on the client. For example, it was not possible to call protected APIs with a token. After deleting client wsdl files in /tmp folder the issue was resolved.

Wednesday, May 24, 2017

Magento Custom Multi Select Customer Attribute

Version: Magento 1.x

Problem:
Create a simple custom multi select customer attribute programmatically via upgrade script.

Solution:

$attributeCode = 'my_attribute';
$installer->addAttribute('customer', $attributeCode, array(
    'type' => 'varchar',
    'label' => 'My Attribute',
    'input' => 'multiselect',
    'source' => 'eav/entity_attribute_source_table',
    'backend' => '',
    'visible' => false,
    'required' => false,
    'unique' => false,
    'option' => array (
        'value' => array(
            'Option1' => array('Option1'),
            'Option2' => array('Option2')
        )
    )
));

$attribute = Mage::getSingleton('eav/config')
    ->getAttribute('customer', $attributeCode);
$attribute
    ->setData('used_in_forms', array('customer_account_edit'))
    ->setData('is_used_for_customer_segment', true)
    ->setData('is_system', false)
    ->setData('is_user_defined', true)
    ->setData('is_visible', false)
    ->setData('sort_order', 100);
$attribute->save();

Monday, April 3, 2017

Problem:
Encode js strings in PHP.

Solution:
$encodedStr = addslashes(htmlspecialchars($str));