У меня есть пользовательский модуль, который добавляет пользовательские атрибуты для проверки Magento 2. Но я не могу назначить условия, при которых пользовательские атрибуты должны отображаться на этапе оформления заказа. Я работаю над тем, чтобы он отображался только при оформлении заказа на основе атрибута продукта «isyes», установленного на «да».
ниже находится модуль 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;
}
}
Прямо сейчас я могу удалить имя атрибута пользовательского заказа «тест» из макета по методу ниже
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']);
}
Но я не могу удалить пользовательский атрибут из макета оформления заказа на основе этого условия. Пожалуйста, помогите. Спасибо
Задача ещё не решена.
Других решений пока нет …