3

I want to override Magento core block how can I do that vendor\magento\module-catalog\view\frontend\templates\product\view\options\type\Select.php.

I want to remove -- before and after Please Select text as shown in the image below

enter image description here

So Far I have tried to create a Module to override the block in app/code, app/code/Abcd/Xyz but when i have enabled it it just removed the whole select box as shown in below image

[https://i.sstatic.net/gnJiP.png[2]

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Aug 17, 2016 at 7:13

3 Answers 3

3

you can create you own module that contains a translation file i18n/en_US.csv with this content:

"-- Please Select --","Please Select"
answered Aug 17, 2016 at 7:37
3
  • Ya, I thought that but now, I want to do something Like this insted of Please Select I want Select Size, Select Color So I have to override the view block. Commented Aug 17, 2016 at 8:33
  • that is a totally different questions. please don't mix them. Commented Aug 17, 2016 at 8:57
  • I followed you advice and create a new question for it magento.stackexchange.com/questions/131762/… Commented Aug 17, 2016 at 9:44
1

need to override the below block

<magento-root>/vendor/magento/module-catalog/Block/Product/View/Options/Type/Select.php

in the above file change the below line

 $select->setName('options[' . $_option->getid() . ']')->addOption('', __('-- Please Select --')); 

TO

 $select->setName('options[' . $_option->getid() . ']')->addOption('', __('Please Select'));

for override the block refer this answer

note: I am not tested but I hope it works

answered Aug 17, 2016 at 7:35
3
  • Hi, I have done the same. I have override the Magento\Catalog\Block\Product\View\Options\Type\Select Block in my module, but doing so, Removed the select box from the view as in the above image[Refer Question] Commented Aug 17, 2016 at 8:36
  • Do I have to override template file as well in the module vendor\magento\module-catalog\view\frontend\templates\product\view\options\type\select.phtml Commented Aug 17, 2016 at 8:38
  • for trial just directly change the text in vendor/magento/module-catalog/Block/Product/View/Options/Type/Select.php. after that clear cache then load page. If it worked, definitely you make mistake in override. Commented Aug 17, 2016 at 8:50
1

given that Marius' solution is likely the most effective one, I take the chance to offer an alternative to avoid block rewrite. It is based on the usage of a plugin.

Assuming you know how to define your own module, you can declare the plugin in the <your_module_dir>/etc/di.xml file as follows:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <type name="Magento\Framework\View\Element\Html\Select">
 <plugin name="vendorname_view_element_html_select"
 type="VendorName\ModuleName\Plugin\View\Element\Html\SelectPlugin" />
 </type>
</config>

Note: change VendorName and ModuleName according to your needs.

Then define the plugin in <your_module_dir>/Plugin/View/Element/Html/SelectPlugin.php:

<?php
namespace VendorName\ModuleName\Plugin\View\Element\Html;
class SelectPlugin
{
 public function beforeAddOption($subject, $value, $label) 
 {
 if ($value == '') {
 $label = __("Whatever you want");
 }
 return [$value, $label];
 }
}

Hope it helps.

answered Aug 18, 2016 at 13:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.