尝试覆盖下面的/vendor/magento/module-swatches/Block/Product/Renderer/Configurable.php

时间:2018-07-24 11:20:30

标签: php magento magento2

我正在尝试覆盖下课。 /vendor/magento/module-swatches/Block/Product/Renderer/Configurable.php

因为我想在构造方法中注入一些类,所以可以在app / code / Companyname / Swatchconfigoption / view / frontend / templates / product / view / renderer.phtml中使用

这样我就创建了覆盖它的模块。下面是代码。

app / code / Companyname / Swatchconfigoption / etc / 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\Swatches\Block\Product\Renderer\Configurable" type="Companyname\Swatchconfigoption\Block\Rewrite\Product\Renderer\Configurable" />
</config>

app / code / Companyname / Swatchconfigoption / Block / Rewrite / Product / Renderer / Configurable.php

<?php
namespace Companyname\Swatchconfigoption\Block\Rewrite\Product\Renderer;

use Magento\Framework\View\Element\Template;

//use Magento\Catalog\Block\Product\Context;
//use Magento\Catalog\Helper\Product as CatalogProduct;
//use Magento\ConfigurableProduct\Helper\Data;
//use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
//use Magento\Customer\Helper\Session\CurrentCustomer;
//use Magento\Framework\Json\EncoderInterface;
//use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Catalog\Model\Product;
//use Magento\Framework\Stdlib\ArrayUtils;
use Magento\Store\Model\ScopeInterface;
//use Magento\Swatches\Helper\Data as SwatchData;
//use Magento\Swatches\Helper\Media;
use Magento\Swatches\Model\Swatch;
use Magento\Framework\App\ObjectManager;
//use Magento\Swatches\Model\SwatchAttributesProvider;

class Configurable extends \Magento\Swatches\Block\Product\Renderer\Configurable {

    /**
     * Path to template file with Swatch renderer.
     */
    const SWATCH_RENDERER_TEMPLATE = 'Companyname_Swatchconfigoption::product/view/renderer.phtml';

    /**
     * Path to default template file with standard Configurable renderer.
     */
    const CONFIGURABLE_RENDERER_TEMPLATE = 'Magento_ConfigurableProduct::product/view/type/options/configurable.phtml';

    /**
     * Action name for ajax request
     */
    const MEDIA_CALLBACK_ACTION = 'swatches/ajax/media';

    /**
     * @var Product
     */
    protected $product;

    /**
     * @var SwatchData
     */
    protected $swatchHelper;

    /**
     * @var Media
     */
    protected $swatchMediaHelper;

    /**
     * Indicate if product has one or more Swatch attributes
     *
     * @deprecated unused
     *
     * @var boolean
     */
    protected $isProductHasSwatchAttribute;

    /**
     * @var SwatchAttributesProvider
     */
    private $swatchAttributesProvider;


    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\ConfigurableProduct\Helper\Data $helper,
        \Magento\Catalog\Helper\Product $catalogProduct,
        \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        \Magento\ConfigurableProduct\Model\ConfigurableAttributeData $configurableAttributeData,
        \Magento\Swatches\Helper\Data $swatchHelper,
        \Magento\Swatches\Helper\Media $swatchMediaHelper,
        array $data = [],
        \Magento\Swatches\Model\SwatchAttributesProvider $swatchAttributesProvider = null
    ) {
        $this->swatchHelper = $swatchHelper;
        $this->swatchMediaHelper = $swatchMediaHelper;
        $this->swatchAttributesProvider = $swatchAttributesProvider
            ?: ObjectManager::getInstance()->get(\Magento\Swatches\Model\SwatchAttributesProvider::class);

        parent::__construct(
            $context,
            $arrayUtils,
            $jsonEncoder,
            $helper,
            $catalogProduct,
            $currentCustomer,
            $priceCurrency,
            $configurableAttributeData,
            $swatchHelper,
            $swatchMediaHelper,
            $data,
            $swatchAttributesProvider
        );
    }

... Configurable.php的其余代码在下面...

所以在上面的类中,我想注入一些类,但是在此之前,当我尝试执行di:compile时,它给了我下面这篇文章中给出的错误。

我的应用程序/代码/公司名称/Swatchconfigoption/view/frontend/templates/product/view/renderer.phtml在objectManager中使用了一些类。

请注意,我只想注入这些类,这样就不必使用对象管理器,而可以使用Configuration.php注入的对象中的对象实例。

但是在完成上述工作之后,当我尝试使用di:compile时,它在控制台中出现了以下错误。

编译过程中的错误:

公司名称\ Swatchconfigoption \ Block \ Rewrite \ Product \ Renderer \ Configurable 不兼容的参数类型:必需类型:\ Magento \ Swatches \ Helper \ Data。实际类型:数组;文件: app / code / Companyname / Swatchconfigoption / Block / Rewrite / Product / Renderer / Configurable.php

请帮助。

0 个答案:

没有答案