Magento 2根据添加到购物车的商品属性自定义结帐布局

时间:2017-11-11 15:33:21

标签: php magento magento2.1

我有一个自定义模块,可以为magento 2 checkout添加自定义属性。但我无法指定自定义属性应在结帐步骤中显示的条件。我正在努力使它只在结帐时显示基于产品属性" isyes"设置为"是"。 下面是模块LayoutProcessorPlugin.php文件

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
    <title>Selected Dropdown</title>
</head>
<body>
    <select name="product_select" id="product_select">
        <option value="">--SELECT--</option>
        <option value="acer">acer</option>
        <option value="samsung">samsung</option>
        <option value="lenovo">lenovo</option>
    </select><br/><br/><br/><br/><br/><br/>
    <input type="text" name="product_inputfield" id="product_inputfield" readonly/>
</body>
<script>

</script>
</html>

现在我可以删除自定义订单属性名称&#34; test&#34;从布局下面的方法

namespace Bss\CheckoutCustomField\Block\Plugin\Checkout;

use Magento\Framework\Json\Helper\Data as JsonHelper;

class LayoutProcessorPlugin
{
    protected $storeManager;

    protected $attribute;

    protected $attributeOptions;

    protected $jsonHelper;

    protected $helper;

    protected $customerSession;

    protected $customer;

    const DISPLAY_SHIPPING_ADDRESS = 0;
    const DISPLAY_REVIEW_PAYMENT = 1;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Bss\CheckoutCustomField\Model\Attribute $attribute,
        \Bss\CheckoutCustomField\Model\AttributeOption $attributeOption,
        \Bss\CheckoutCustomField\Helper\Data $helper,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Customer $customer,
        JsonHelper $jsonHelper
    ) {
        $this->storeManager = $storeManager;
        $this->attribute = $attribute;
        $this->attributeOption = $attributeOption;
        $this->helper = $helper;
        $this->customerSession = $customerSession;
        $this->customer = $customer;
        $this->jsonHelper = $jsonHelper;
    }

    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array $jsLayout
    ) {
        if(!$this->helper->moduleEnabled())
        {
            return $jsLayout;
        }
        $attributes = $this->attribute->getCustomFieldChekout();
        $types = [
            'text' => 'Magento_Ui/js/form/element/abstract',
            'textarea' => 'Magento_Ui/js/form/element/textarea',
            'select' => 'Magento_Ui/js/form/element/select',
            'boolean' => 'Magento_Ui/js/form/element/select',
            'multiselect' => 'Bss_CheckoutCustomField/js/form/element/checkboxes',
            'date' => 'Magento_Ui/js/form/element/date'
        ];
        $elementTmpl = [
            'text' => 'ui/form/element/input',
            'textarea' => 'ui/form/element/textarea',
            'select' => 'Bss_CheckoutCustomField/form/element/radio',
            'boolean' => 'ui/form/element/select',
            'multiselect' => 'Bss_CheckoutCustomField/form/element/checkboxes',
            'date' => 'ui/form/element/date'
        ];
        $customerHasAddress = false;
        $customerId = $this->customerSession->getCustomerId() ? $this->customerSession->getCustomerId() : 0;
        if($customerId){
            $customerData = $this->customer->load($customerId);
            $customerHasAddress = (count($customerData->getAddresses()) > 0);
        }
        foreach ($attributes as $attribute) {
            $storeId = $this->storeManager->getStore()->getId();
            $stores = explode(',', $attribute->getStoreId());
            if(!in_array($storeId, $stores))
                continue;
            $labels = $this->jsonHelper->jsonDecode($attribute->getFrontendLabel());
            $label = !empty($labels[$storeId]) ? $labels[$storeId] : $labels[0];
            $validation = [];
            if($attribute->getFrontendInput() == 'multiselect'){
                $name = 'bss_custom_field['.$attribute->getAttributeCode().'][]';
            }else{
                $name = 'bss_custom_field['.$attribute->getAttributeCode().']';
            }

            if($attribute->getIsRequired() == 1)
            {
                if($attribute->getFrontendInput() == 'multiselect'){
                    $validation['validate-one-required'] = true;
                    $validation['required-entry'] = true;
                }else{
                    $validation['required-entry'] = true;
                }                
            }
            $validation[$attribute->getFrontendClass()] = true;

            $options = $this->getOptions($attribute);

            if ($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect') {
                $default = $this->attributeOption->getOptions($attribute->getAttributeId());
                $default = $this->attributeOption->getDefaultValue($default[0]);
            } else {
                $default = $attribute->getDefaultValue();
            }

            if ($attribute->getShowInShipping() == self::DISPLAY_SHIPPING_ADDRESS) {
                if(!$customerHasAddress) {
                    $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                    ['shippingAddress']['children']['shipping-address-fieldset']['children'][$attribute->getAttributeCode()] = [
                        'component' => $types[$attribute->getFrontendInput()],
                        'config' => [
                            'customScope' => 'shippingAddress',
                            'template' => 'ui/form/field',
                            'elementTmpl' => $elementTmpl[$attribute->getFrontendInput()],
                            'id' => $attribute->getAttributeCode(),
                            'rows' => 5
                        ],
                        'dataScope' => 'shippingAddress.bss_custom_field['.$attribute->getAttributeCode().']',
                        'label' => $label,
                        'options' => $options,
                        'caption' => 'Please select',
                        'provider' => 'checkoutProvider',
                        'visible' => true,
                        'validation' => $validation,
                        'sortOrder' => $attribute->getSortOrder() + 200,
                        'id' => 'bss_custom_field['.$attribute->getAttributeCode().']',
                        'default' => $default,
                    ];
                }else{
                    $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                    ['shippingAddress']['children']['before-form']['children']['before-form-child']['children'][$attribute->getAttributeCode()] = [
                        'component' => $types[$attribute->getFrontendInput()],
                        'config' => [
                            'customScope' => 'shippingAddressLogin',
                            'template' => 'ui/form/field',
                            'elementTmpl' => $elementTmpl[$attribute->getFrontendInput()],
                            'id' => $attribute->getAttributeCode(),
                            'rows' => 5
                        ],
                        'dataScope' => 'shippingAddressLogin.bss_custom_field['.$attribute->getAttributeCode().']',
                        'label' => $label,
                        'options' => $options,
                        'caption' => 'Please select',
                        'provider' => 'checkoutProvider',
                        'visible' => true,
                        'validation' => $validation,
                        'sortOrder' => $attribute->getSortOrder() + 200,
                        'id' => 'bss_custom_field['.$attribute->getAttributeCode().']',
                        'default' => $default,
                    ];
                }
            }

            //show in payment & review
            if ($attribute->getShowInShipping() == self::DISPLAY_REVIEW_PAYMENT) {
                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                    ['payment']['children']['beforeMethods']['children'][$attribute->getAttributeCode()] = [
                        'component' => $types[$attribute->getFrontendInput()],
                        'config' => [
                            'customScope' => 'paymentBeforemethods',
                            'template' => 'ui/form/field',
                            'elementTmpl' => $elementTmpl[$attribute->getFrontendInput()],
                            'id' => $attribute->getAttributeCode()
                        ],
                        'options' => $options,
                        'caption' => 'Please select',
                        'dataScope' => 'paymentBeforemethods.'.$name,
                        'label' => $label,
                        'provider' => 'checkoutProvider',
                        'visible' => true,
                        'validation' => $validation,
                        'sortOrder' => $attribute->getSortOrder() + 200,
                        'id' => 'bss_custom_field['.$attribute->getAttributeCode().']',
                        'default' => $default,
                    ];
            }
        }
        return $jsLayout;
    }

    protected function getOptions($attribute)
    {
        if ($attribute->getFrontendInput() == 'date') {
            $options = [
                'dateFormat' => 'm/d/Y',
                "timeFormat" => 'hh:mm',
                "showsTime" => true
            ];
        } elseif ($attribute->getFrontendInput() == 'boolean') {
            $options = [
                ['value' => '0', 'label' => 'No'],
                ['value' => '1', 'label' => 'Yes']
            ];
        } else {
            $options = $this->attributeOption->getAttributeOptions($attribute->getAttributeId());
        }

        return $options;
    }
}

但我想基于具有产品属性&#34; test&#34;的任何一个项目来实现上述方法。分配给&#34;是&#34;。

我尝试了下面的代码

unset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                    ['shippingAddress']['children']['shipping-address-fieldset']['children']['test']);

但我无法根据此条件从结帐布局中删除自定义属性。请帮助。感谢

0 个答案:

没有答案