마젠토 2:블록 클래스 재정의 - 출력을 생성하지 않음
-
29-09-2020 - |
문제
나는 module-catalog/Block/Product/View/Options/Type/Select.php
몇 가지 다른 사용자 정의 PHP 비트가 포함된 새 버전의 클래스입니다(포함된 DIV에 HTML 클래스 및 ID 추가).새 모듈을 만들어 이를 달성하려고 합니다.안타깝게도 이 새 모듈이 활성화되면 선택 옵션이 제품 페이지에 렌더링되지 않습니다.페이지의 나머지 부분은 선택(체크박스 및 라디오 버튼) 옵션 없이도 괜찮습니다.나는 모든 것이 괜찮아 보이고 제대로 작동할 것이라고 생각하는데, 왜 그런 일이 일어나는지 이해할 수 없습니다. Select.php
선택 옵션을 반환하지 않습니다.
디렉토리 구조:
app/
├── Mike/
│ ├── Newselect/
│ │ ├── Block/
│ │ │ ├── Product/
│ │ │ │ ├── View/
│ │ │ │ │ ├── Options/
│ │ │ │ │ │ ├── Type/
│ │ │ │ │ │ │ ├── Select.php
│ │ ├── etc/
│ │ │ ├── di.xml
│ │ │ ├── module.xml
│ │ ├── registration.php
│ │ ├── composer.json
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\View\Options\Type\Select" type="Mike\Newselect\Block\Product\View\Options\Type\Select" />
</config>
module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mike_Newselect" setup_version="1.0.0"/>
</config>
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mike_Newselect',
__DIR__
);
composer.json
{
"name": "Mike/Newselect",
"description": "Update the Select module to add HTML classes and IDs",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/module-store": "100.0.*",
"magento/module-eav": "100.0.*",
"magento/module-cms": "100.0.*",
"magento/module-indexer": "100.0.*",
"magento/module-customer": "100.0.*",
"magento/module-theme": "100.0.*",
"magento/module-checkout": "100.0.*",
"magento/module-backend": "100.0.*",
"magento/module-widget": "100.0.*",
"magento/module-wishlist": "100.0.*",
"magento/module-tax": "100.0.*",
"magento/module-msrp": "100.0.*",
"magento/module-catalog-inventory": "100.0.*",
"magento/module-directory": "100.0.*",
"magento/module-catalog-rule": "100.0.*",
"magento/module-product-alert": "100.0.*",
"magento/module-url-rewrite": "100.0.*",
"magento/module-catalog-url-rewrite": "100.0.*",
"magento/module-page-cache": "100.0.*",
"magento/module-quote": "100.0.*",
"magento/module-config": "100.0.*",
"magento/module-media-storage": "100.0.*",
"magento/framework": "100.0.*",
"magento/module-configurable-product": "100.0.*",
"magento/module-ui": "100.0.*"
},
"suggest": {
"magento/module-cookie": "100.0.*",
"magento/module-catalog-sample-data": "Sample Data version:100.0.*"
},
"type": "magento2-module",
"version": "100.0.5",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\Catalog\\": ""
}
}
}
Select.php
(이것은 네임스페이스와 핵심 클래스 확장을 제외하고 아직 업데이트되지 않은 핵심 Select.php 파일의 복사본입니다.)
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Product options text type block
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Mike\Newselect\Block\Product\View\Options\Type;
class Select extends \Magento\Catalog\Block\Product\View\Options\Type\Select
{
/**
* Return html for control element
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getValuesHtml()
{
$_option = $this->getOption();
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
$store = $this->getProduct()->getStore();
$this->setSkipJsReloadPrice(1);
// Remove inline prototype onclick and onchange events
if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DROP_DOWN ||
$_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_MULTIPLE
) {
$require = $_option->getIsRequire() ? ' required' : '';
$extraParams = '';
$select = $this->getLayout()->createBlock(
'Magento\Framework\View\Element\Html\Select'
)->setData(
[
'id' => 'select_' . $_option->getId(),
'class' => $require . ' product-custom-option admin__control-select'
]
);
if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DROP_DOWN) {
$select->setName('options[' . $_option->getid() . ']')->addOption('', __('-- Please Select --'));
} else {
$select->setName('options[' . $_option->getid() . '][]');
$select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option');
}
foreach ($_option->getValues() as $_value) {
$priceStr = $this->_formatPrice(
[
'is_percent' => $_value->getPriceType() == 'percent',
'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
],
false
);
$select->addOption(
$_value->getOptionTypeId(),
$_value->getTitle() . ' ' . strip_tags($priceStr) . '',
['price' => $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false)]
);
}
if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_MULTIPLE) {
$extraParams = ' multiple="multiple"';
}
if (!$this->getSkipJsReloadPrice()) {
$extraParams .= ' onchange="opConfig.reloadPrice()"';
}
$select->setExtraParams($extraParams);
if ($configValue) {
$select->setValue($configValue);
}
return $select->getHtml();
}
if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_RADIO ||
$_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_CHECKBOX
) {
$selectHtml = '<div class="options-list nested" id="options-' . $_option->getId() . '-list">';
$require = $_option->getIsRequire() ? ' required' : '';
$arraySign = '';
switch ($_option->getType()) {
case \Magento\Catalog\Model\Product\Option::OPTION_TYPE_RADIO:
$type = 'radio';
$class = 'radio admin__control-radio';
if (!$_option->getIsRequire()) {
$selectHtml .= '<div class="field choice admin__field admin__field-option">' .
'<input type="radio" id="options_' .
$_option->getId() .
'" class="' .
$class .
' product-custom-option" name="options[' .
$_option->getId() .
']"' .
($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
' value="" checked="checked" /><label class="label admin__field-label" for="options_' .
$_option->getId() .
'"><span>' .
__('None') . '</span></label></div>';
}
break;
case \Magento\Catalog\Model\Product\Option::OPTION_TYPE_CHECKBOX:
$type = 'checkbox';
$class = 'checkbox admin__control-checkbox';
$arraySign = '[]';
break;
}
$count = 1;
foreach ($_option->getValues() as $_value) {
$count++;
$priceStr = $this->_formatPrice(
[
'is_percent' => $_value->getPriceType() == 'percent',
'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
]
);
$htmlValue = $_value->getOptionTypeId();
if ($arraySign) {
$checked = is_array($configValue) && in_array($htmlValue, $configValue) ? 'checked' : '';
} else {
$checked = $configValue == $htmlValue ? 'checked' : '';
}
$selectHtml .= '<div class="field choice admin__field admin__field-option' .
$require .
'">' .
'<input type="' .
$type .
'" class="' .
$class .
' ' .
$require .
' product-custom-option"' .
($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
' name="options[' .
$_option->getId() .
']' .
$arraySign .
'" id="options_' .
$_option->getId() .
'_' .
$count .
'" value="' .
$htmlValue .
'" ' .
$checked .
' price="' .
$this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false) .
'" />' .
'<label class="label admin__field-label" for="options_' .
$_option->getId() .
'_' .
$count .
'"><span>' .
$_value->getTitle() .
'</span> ' .
$priceStr .
'</label>';
$selectHtml .= '</div>';
}
$selectHtml .= '</div>';
return $selectHtml;
}
}
}
catalog_product_view.xml
<block class="Magento\Catalog\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select.phtml"/>
해결책
가장 먼저, 사용자 정의 선택 클래스에 생성자 주입 방법을 추가해야 합니다. (편집하다:생성자 주입을 추가할 필요가 없고 시퀀스 모듈을 추가해야 합니다.)
<?php
namespace Mike\Newselect\Block\Product\View\Options\Type;
class Select extends \Magento\Catalog\Block\Product\View\Options\Type\Select
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
\Magento\Catalog\Helper\Data $catalogData,
array $data = []
)
{
parent::__construct($context, $pricingHelper, $catalogData, $data);
}
.....
다음을 추가해야 합니다.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mike_Newselect" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
http://devdocs.magento.com/guides/v2.1/extension-dev-guide/build/module-load-order.html
둘째, 복사 vendor/magento/module-catalog/view/frontend/templates/product/view/options/type/select.phtml
우리의 맞춤 모듈에 app/code/Mike/Newselect/view/frontend/templates/product/view/options/type/select.phtml