0

I'm trying to override the module-catalog/Block/Product/View/Options/Type/Select.php class with a new version which includes a few other bits of custom php. I have done the below and then run the php bin/magento setup:upgrade and php cache:clean commands but the product page is showing a blank/white page.

I created a 'code' folder and placed it here:

/app/code/

I created a Vendor folder titled 'Mike' and placed it here:

/app/code/Mike/

Within the new Mike vendor folder, I created an etc folder: /app/code/Mike/etc/. Within this etc folder, I have added a di.xml file containing the below:

<?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>

I then created a Module folder titled NewSelect and placed it in the Mike vendor folder:

/app/code/Mike/NewSelect/

I then just copied over the directory structure (without the containing files, except for Select.php) of the core Select class and placed it in the NewSelect module folder, resulting in the below:

app/
├── Mike/
│   ├── NewSelect/
│   │   ├── Block/
│   │   │   ├── Product/
│   │   │   │   ├── View/
│   │   │   │   │   ├── Options/
│   │   │   │   │   │   ├── Type/
│   │   │   │   │   │   │   ├── Select.php
│   │   ├── etc/
│   │   │   ├── di.xml

Within my new Select class (app/code/Mike\NewSelect\Block\Product\View\Options\Type\Select.php), I have extended the core Select class with the following:

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)
etc etc...

In my Mike/MyTheme/Magento_Catalog/catalog_product_view.xml file, I have updated the calling block:

<block class="Magento\Catalog\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select_hamper.phtml"/>

and changed it to:

<block class="Mike\NewSelect\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select.phtml"/>

Can anyone see where I'm going wrong?

Thank you

1

We declare Dependency Injection

<preference for="Magento\Catalog\Block\Product\View\Options\Type\Select" type="Mike\NewSelect\Block\Product\View\Options\Type\Select" />

If someone ask for an instance of Magento\Catalog\Block\Product\View\Options\Type\Select, we 'always' give them an instance of Mike\NewSelect\Block\Product\View\Options\Type\Select. Your custom class 'automatically' overrides the core class. So, we don't need to change:

<block class="Mike\NewSelect\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select.phtml"/>

Should keep Magento\Catalog\Block\Product\View\Options\Type\Select

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.