Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Magento 2根据添加到购物车的商品属性定制结账布局_Php_Magento_Magento2.1 - Fatal编程技术网

Php Magento 2根据添加到购物车的商品属性定制结账布局

Php Magento 2根据添加到购物车的商品属性定制结账布局,php,magento,magento2.1,Php,Magento,Magento2.1,我有一个自定义模块,可以向magento 2签出添加自定义属性。但我无法指定在签出步骤中显示自定义属性的条件。我正在努力使它只显示在结帐的基础上,产品属性“是”设置为“是”。 下面是模块LayoutProcessorPlugin.php文件 namespace Bss\CheckoutCustomField\Block\Plugin\Checkout; use Magento\Framework\Json\Helper\Data as JsonHelper; class LayoutProc

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

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;
    }
}
现在,我可以通过下面的方法从布局中删除自定义订单属性名称“test”

unset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                    ['shippingAddress']['children']['shipping-address-fieldset']['children']['test']);
但我想基于产品属性“test”指定为“yes”的任何一个项来实现上述方法

我尝试了下面的代码

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

    // get cart items
    $items = $cart->getItems();
     $isyes="0";
    // get product attribute value of cart items
    foreach ($items as $item){
if(!$item->getData('test')=="yes"){
$isyes="1";
}
};
if($isyes=="1"){
unset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                    ['shippingAddress']['children']['shipping-address-fieldset']['children']['test']);
}

但我无法基于此条件从签出布局中删除自定义属性。请帮忙。谢谢

有什么更新吗?有什么更新吗?