Php Magento 2:允许客户编辑购物车上的自定义选项
我正在尝试添加允许客户编辑选项的功能 我已经创建了一个模块供应商/合作伙伴。 这是加载自定义选项并选择,但不是获取用户选择的选项 我想知道如何接收选定的选项,并检查该选项是否被选中。 目前Select.php中的这一行没有收到任何内容,变量$checked为nullPhp Magento 2:允许客户编辑购物车上的自定义选项,php,magento,module,magento2,Php,Magento,Module,Magento2,我正在尝试添加允许客户编辑选项的功能 我已经创建了一个模块供应商/合作伙伴。 这是加载自定义选项并选择,但不是获取用户选择的选项 我想知道如何接收选定的选项,并检查该选项是否被选中。 目前Select.php中的这一行没有收到任何内容,变量$checked为null $configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
在供应商/COptions\view\frontend\templates\cart\item\default.phtml中:
<?php $selectedOptions = $block->getSelectedOptionList(); ?>
<?php //$_options = Mage::helper('core')->decorateArray($block>getOptions())
$_options = $block->getOptions() ?>
<?php if ($_options AND count($_options)):?>
<dl>
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php endforeach; ?>
</dl>
<?php endif; ?>
在供应商/COptions\Block\Rewrite\Catalog\Product\View\Options\Type\Select.php中:
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\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE
) {
$require = $_option->getIsRequire() ? ' required' : '';
$extraParams = '';
$select = $this->getLayout()->createBlock(
\Magento\Framework\View\Element\Html\Select::class
)->setData(
[
'id' => 'select_' . $_option->getId(),
'class' => $require . ' product-custom-option admin__control-select'
]
);
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::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\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) {
$extraParams = ' multiple="multiple"';
}
if (!$this->getSkipJsReloadPrice()) {
$extraParams .= ' onchange="opConfig.reloadPrice()"';
}
$extraParams .= ' data-selector="' . $select->getName() . '"';
$select->setExtraParams($extraParams);
if ($configValue) {
$select->setValue($configValue);
}
return $select->getHtml();
}
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::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\Api\Data\ProductCustomOptionInterface::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() .
']"' .
' data-selector="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\Api\Data\ProductCustomOptionInterface::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' : '';
}
$dataSelector = 'options[' . $_option->getId() . ']';
if ($arraySign) {
$dataSelector .= '[' . $htmlValue . ']';
}
$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 .
' data-selector="' . $dataSelector . '"' .
' 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;
}
}
公共函数getValuesHtml()
{
$\u option=$this->getOption();
$configValue=$this->getProduct()->getPreconfiguredValues()->getData('options/'。$\u option->getId());
$store=$this->getProduct()->getStore();
$this->setSkipJsReloadPrice(1);
//删除内联原型onclick和onchange事件
如果($\u option->getType()=\Magento\Catalog\Api\Data\ProductCustomOption接口::选项类型下拉列表||
$\u option->getType()=\Magento\Catalog\Api\Data\ProductCustomOption接口::选项\u类型\u多个
) {
$require=$\u option->getIsRequire()?“必需”:“”;
$extraParams='';
$select=$this->getLayout()->createBlock(
\Magento\Framework\View\Element\Html\Select::class
)->设置数据(
[
'id'=>'选择.$\u选项->获取id(),
'class'=>require.'product custom option admin\uu control-select'
]
);
如果($\u option->getType()=\Magento\Catalog\Api\Data\ProductCustomOptionInterface::option\u TYPE\u DROP\u DOWN){
$select->setName('options['.$\u option->getid().]')->addOption('''.'请选择--');
}否则{
$select->setName('options['.$\u option->getid().][]');
$select->setClass($multiselect admin\uu control-multiselect'.$require.'product custom option');
}
foreach($\u选项->getValues()作为$\u值){
$priceStr=$this->\u格式价格(
[
'is_percent'=>$\u value->getPriceType()='percent',
'定价\u值'=>$\u值->getPrice($\u值->getPriceType()=='percent'),
],
假的
);
$select->addOption(
$\u value->getOptionTypeId(),
$\u value->getTitle()。'.strip_标记($priceStr)。'',
['price'=>$this->pricingHelper->currencyByStore($\u value->getPrice(true),$store,false)]
);
}
if($\u option->getType()=\Magento\Catalog\Api\Data\ProductCustomOptionInterface::option\u TYPE\u MULTIPLE){
$extraParams='multiple=“multiple”';
}
如果(!$this->getSkipJsReloadPrice()){
$extraParams.='onchange=“opConfig.reloadPrice()”;
}
$extraParams.='data selector=“”。$select->getName()。”;
$select->setExtraParams($extraParams);
如果($configValue){
$select->setValue($configValue);
}
返回$select->getHtml();
}
如果($\u option->getType()=\Magento\Catalog\Api\Data\ProductCustomOption接口::option\u TYPE\u收音机||
$\u option->getType()=\Magento\Catalog\Api\Data\ProductCustomOption接口::选项\u类型\u复选框
) {
$selectHtml='';
$require=$\u option->getIsRequire()?“必需”:“”;
$arraySign='';
开关($\u选项->getType()){
案例\Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION\u类型\u收音机:
$type=‘radio’;
$class='无线电管理控制无线电';
如果(!$\u选项->getIsRequire()){
$selectHtml.=''。
'' .
__("无"),;
}
打破
case\Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION\u TYPE\u复选框:
$type='checkbox';
$class='checkbox admin_uucontrol-checkbox';
$arraySign='[]';
打破
}
$count=1;
foreach($\u选项->getValues()作为$\u值){
$count++;
$priceStr=$this->\u格式价格(
[
'is_percent'=>$\u value->getPriceType()='percent',
'定价\u值'=>$\u值->getPrice($\u值->getPriceType()=='percent'),
]
);
$htmlValue=$\u value->getOptionTypeId();
如果($arraySign){
$checked=is_数组($configValue)和&in_数组($htmlValue,$configValue)?“checked”:“;
}否则{
$checked=$configValue==$htmlValue?'checked':'';
}
$dataSelector='options['.$\u option->getId().']';
如果($arraySign){
$dataSelector.='['.$htmlValue.]';
}
$selectHtml.=''。
'' .
'' .
$\u value->getTitle()。
' ' .
$priceStr。
'';
$selectHtml.='';
}
$selectHtml.='';
返回$selectHtml;
}
}
您是否找到了解决方案?
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\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE
) {
$require = $_option->getIsRequire() ? ' required' : '';
$extraParams = '';
$select = $this->getLayout()->createBlock(
\Magento\Framework\View\Element\Html\Select::class
)->setData(
[
'id' => 'select_' . $_option->getId(),
'class' => $require . ' product-custom-option admin__control-select'
]
);
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::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\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) {
$extraParams = ' multiple="multiple"';
}
if (!$this->getSkipJsReloadPrice()) {
$extraParams .= ' onchange="opConfig.reloadPrice()"';
}
$extraParams .= ' data-selector="' . $select->getName() . '"';
$select->setExtraParams($extraParams);
if ($configValue) {
$select->setValue($configValue);
}
return $select->getHtml();
}
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::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\Api\Data\ProductCustomOptionInterface::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() .
']"' .
' data-selector="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\Api\Data\ProductCustomOptionInterface::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' : '';
}
$dataSelector = 'options[' . $_option->getId() . ']';
if ($arraySign) {
$dataSelector .= '[' . $htmlValue . ']';
}
$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 .
' data-selector="' . $dataSelector . '"' .
' 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;
}
}