0

So I'm trying to add a custom 'checkbox-set' to the checkout page of a vanilla install of Magento 2.3.2, I have it working on the frontend view, though after changing some values in the InstallSchema.php and InstallData.php (as the old values were causing the checkout not to continue to next step) now the 'sales_order_address' table isn't being created on 'php magento/bin setup:upgrade'. I think my Data types are wrong for the checkbox-set.. I'm very confused at this point. Also note: The checkbox-set should only be able to have one of the two options selected (which is currently working on frontend view).

Here is my InstallSchema.php:

namespace JZWeb\CustomAddressType\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * Add the new column
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $this->addAddressTypeColumn($setup);

        $installer->endSetup();
    }

    /**
     * Add the column named address_type
     *
     * @param SchemaSetupInterface $setup
     *
     * @return void
     */
    private function addAddressTypeColumn(SchemaSetupInterface $setup)
    {
        $addressType = [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            'default' => 0,
            'nullable' => false,
            'comment' => 'Address Type'
        ];

        $setup->getConnection()->addColumn(
            $setup->getTable('sales_order_address'),
            'address_type',
            $addressType
        );

        $setup->getConnection()->addColumn(
            $setup->getTable('quote_address'),
            'address_type',
            $addressType
        );
    }
}

And here is my InstallData.php:

namespace JZWeb\CustomAddressType\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;

/**
 * Install Data
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->addAddressTypeAttribute($setup);
    }

    /**
     * Add the address address_type attribute
     *
     * @return void
     */
    protected function addAddressTypeAttribute(ModuleDataSetupInterface $setup)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        if (!$customerSetup->getAttributeId('customer_address', 'address_type')) {
            $customerSetup->addAttribute('customer_address', 'address_type', [
                'type' => 'int',
                'label' => 'Address Type',
                'input' => 'hidden',
                'required' => true  ,
                'visible' => true,
                'system' => 0,
                'visible_on_front' => false,
                'sort_order' => 101,
                'position' => 101
            ]);
        }
    }
}

And also my LayoutProcessor.php:

namespace JZWeb\CustomAddressType\Plugin\Block\Checkout;

class LayoutProcessor
{
    /**
     * Process js Layout of block
     *
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $subject, $jsLayout)
    {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['address_type'] = $this->processAddressTypeAddress('shippingAddress');

        return $jsLayout;
    }

    /**
     * Process provided address.
     *
     * @param string $dataScopePrefix
     * @return array
     */
    private function processAddressTypeAddress($dataScopePrefix)
    {
        return [
            'component' => 'Magento_Ui/js/form/element/checkbox-set',
            'config' => [
                'customScope' => $dataScopePrefix.'.custom_attributes',
                'customEntry' => null,
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/checkbox-set',
                'id' => 'address_type',
                'options' => [
                    [
                        'value' => '0',
                        'label' => 'Individual'
                    ],
                    [
                        'value' => '1',
                        'label' => 'Business'
                    ]
                ],
                'value' => '0' // default checked value
            ],
            'dataScope' => $dataScopePrefix.'.custom_attributes.address_type',
            'label' => __('Address Type'),
            'provider' => 'checkoutProvider',
            'validation' => [
               'required-entry' => true
            ],
            'sortOrder' => 201,
            'visible' => true,
            'imports' => [
                'initialOptions' => 'index = checkoutProvider:dictionaries.address_type',
                'setOptions' => 'index = checkoutProvider:dictionaries.address_type'
            ]
        ];
    }
}

Any help at all with the data types ('type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER etc) would be greatly appreciated.

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.