Hook funktioniert nicht.

Hallo,

aktuell möchte ich mich an die Funktion convertConfiguratorPrice in der Klasse LegacyStructConverter anhängen. Die Funktion ist public und an sich müsste meine Bootstrap richtig sein.

Die install Funktion

 public function install() { $this-\>subscribeEvent( 'LegacyStructConverter::convertConfiguratorPrice::replace', 'onConvertConfiguratorPrice' ); return true; }

Meine Funktion zum überschreiben

 public function onConvertConfiguratorPrice(Enlight\_Event\_EventArgs $args){ die('Warum funktioniert es nicht?'); }

 

Das Plugin habe ich neu installiert und auch den Cash gelöscht. Es funktioniert weder beim replace noch beim after? Habe ich etwas falsch gemacht oder was kann ich ändern, um diese Funktion zu überschreiben?

Du suchst Service extensions

Viele Grüße

1 „Gefällt mir“

Hi,

vielen Dank für den Link!

Ich das Beispiel angepasst, jedoch wird dieses Funktion immer noch nicht überschreiben.

Die Bootstrap

subscribeEvent(
            'Enlight_Bootstrap_AfterInitResource_shopware_storefront.legacy_struct_converter',
            'registerListProductService'
        );

        return true;
    }

    public function afterInit()
    {
        $this->get('Loader')->registerNamespace(
            'ShopwarePlugins\SwagPluginSystem',
            $this->Path()
        );
    }



    public function registerListProductService()
    {
        Shopware()->Container()->set(
            'shopware_storefront.legacy_struct_converter',
            new OwnLegacyStructConverter()
        );
    }
}

Die Klasse

isSelectionSpecified()) {
            return [];
        }

        $data = [];

        $variantPrice = $product->getVariantPrice();

        if ($this->config->get('calculateCheapestPriceWithMinPurchase')) {
            $cheapestPrice = $product->getCheapestPrice();
        } else {
            $cheapestPrice = $product->getCheapestUnitPrice();
        }

        if (count($product->getPrices()) > 1 || $product->hasDifferentPrices()) {
            $data['priceStartingFrom'] = $this->sFormatPrice(
                $cheapestPrice->getCalculatedPrice()
            );
        }

        $data['price'] = $data['priceStartingFrom'] ? : $this->sFormatPrice(
            $variantPrice->getCalculatedPrice()
        );

        $data['sBlockPrices'] = [];

        return $data;
    }
}

 

Was mach ich falsch? Oder gibt es noch ausführlichere Tutorials zum Überschreiben von Services in Shopware? 

statt

'Enlight_Bootstrap_AfterInitResource_shopware_storefront.legacy_struct_converter'

müsste es heißen

'Enlight_Bootstrap_AfterInitResource_legacy_struct_converter'

und statt

'shopware_storefront.legacy_struct_converter'

muss es heißen

'legacy_struct_converter',

Oder allgemein gesagt: lass das ‘shopware_storefront.’ weg :wink:

Und ein Interface zu implementieren, was es nicht gibt, darf man natürlich auch nicht, also entweder nur 

class OwnLegacyStructConverter 

ggf. klappt auch 

class OwnLegacyStructConverter extends Shopware\Components\Compatibility\LegacyStructConverter

 

1 „Gefällt mir“

Vielen Dank für deine Antwort @hhmarco73‍,

ich habe jetzt alles ein bisschen abgeändert. Jedoch bekomme ich einen Fehler, den ich nicht nachvollziehen kann. Mein aktueller Code sieht so aus:

Bootstrap

subscribeEvent(
            'Enlight_Bootstrap_AfterInitResource_legacy_struct_converter',
            'registerListProductService'
        );

        return true;
    }

    public function afterInit()
    {
        $this->get('Loader')->registerNamespace(
            'ShopwarePlugins\SwagPluginSystem',
            $this->Path()
        );
    }




    public function registerListProductService()
    {
        $coreService = Shopware()->Container()->get('legacy_struct_converter');
        Shopware()->Container()->set(
            'legacy_struct_converter',
            new OwnLegacyStructConverter($coreService)
        );
    }
}

Die Klasse

Aus der Klasse habe ich alles rausgenommen, um Fehlerquellen auszuschließen.

In LegacyStructConverter bekomme ich folgenen Fehler: Call to undefined method Shopware\Components\Compatibility\LegacyStructConverter::get()

  $data['maxpurchase'] = $this->config->get('maxPurchase');

Wie kann es da zu Fehlern kommen, wenn ich nichts überschreibe?

$this->config->

wo steht das?

In LegacyStructConverter.php  Zeile 466 (Shopware 5.2.3). In meinem Plugin wird dieses nicht gesetzt oder überschreiben. 

class OwnLegacyStructConverter { 
    /** Shopware\Components\Compatibility\LegacyStructConverter */
    private $legacyStructConverter

    public function __construct(Shopware\Components\Compatibility\LegacyStructConverter $legacyStructConverter)

    {
        $this->legacyStructConverter = $legacyStructConverter

    }
    
    /**
     * @param StoreFrontBundle\Struct\ListProduct $product
     * @param StoreFrontBundle\Struct\Configurator\Set $set
     * @return array
     */
    public function convertConfiguratorPrice(
        StoreFrontBundle\Struct\ListProduct $product,
        StoreFrontBundle\Struct\Configurator\Set $set
    ) {
        /** hier Dein eigener Code */
    }
    
    /**
     * @param StoreFrontBundle\Struct\Country[] $countries
     * @return array
     */
    public function convertCountryStructList($countries)
    {
        return $this->legacyStructConverter->convertCountryStructList($countries)
    }

    /**
     * @param StoreFrontBundle\Struct\Country\State[] $states
     * @return array
     */
    public function convertStateStructList($states)
    {
        return $this->legacyStructConverter->convertStateStructList($states);
    }
    
    /** usw. mit allen weiteren public Methoden von LegacyStructConverter */

}

 

1 „Gefällt mir“

Super. Vielen Dank @hhmarco73‍,

das hat mir wirklich geholfen, durch das ganze durzusteigen. 

 

Ich habe jetzt auch alle Funktione überschreiben. Jedoch funktioniert es trotdem nicht, da anscheiend $this->legacyStructConverter nicht inizalisiert wurde. 

Folgende Fehlermeldung kommt:  Fatal error : Call to a member function convertProductStruct() on null in /vagrant/engine/Shopware/Plugins/Local/Frontend/SwagPluginSystem/StoreFrontBundle/OwnLegacyStructConverter.php  on line  131
503 Service Unavailable

Was muss ich anderst machen, damit es funktioniert?

legacyStructConverter = $legacyStructConverter;

    }

    /**
     * @param StoreFrontBundle\Struct\ListProduct $product
     * @param StoreFrontBundle\Struct\Configurator\Set $set
     * @return array
     */
    public function convertConfiguratorPrice(
        StoreFrontBundle\Struct\ListProduct $product,
        StoreFrontBundle\Struct\Configurator\Set $set
    ) {
        return '';
    }


    /**
     * @param StoreFrontBundle\Struct\Country[] $countries
     * @return array
     */
    public function convertCountryStructList($countries)
    {
        return $this->legacyStructConverter->convertCountryStructList($countries);
    }

    /**
     * @param StoreFrontBundle\Struct\Country $country
     * @return array
     */
    public function convertCountryStruct(StoreFrontBundle\Struct\Country $country){
        return $this->legacyStructConverter->convertCountryStruct($country);
    }


    /**
     * @param StoreFrontBundle\Struct\Country\State $state
     * @return array
     */
    public function convertStateStruct(StoreFrontBundle\Struct\Country\State $state)
    {
        return $this->legacyStructConverter->convertStateStruct($state);
    }

    /**
     * Converts a configurator group struct which used for default or selection configurators.
     *
     * @param StoreFrontBundle\Struct\Configurator\Group $group
     * @return array
     */
    public function convertConfiguratorGroupStruct(StoreFrontBundle\Struct\Configurator\Group $group)
    {
        return $this->legacyStructConverter->convertConfiguratorGroupStruct($group);
    }

    /**
     * @param StoreFrontBundle\Struct\Category $category
     * @return array
     * @throws \Exception
     */
    public function convertCategoryStruct(StoreFrontBundle\Struct\Category $category)
    {
        return $this->legacyStructConverter->convertCategoryStruct($category);
    }

    /**
     * @param StoreFrontBundle\Struct\Country\State[] $states
     * @return array
     */
    public function convertStateStructList($states)
    {
        return $this->legacyStructConverter->convertStateStructList($states);
    }

    /** usw. mit allen weiteren public Methoden von LegacyStructConverter */


    /**
     * @param StoreFrontBundle\Struct\ListProduct[] $products
     * @return array
     */
    public function convertListProductStructList(array $products)
    {
        return $this->legacyStructConverter->convertListProductStructList($products);
    }

    /**
     * Converts the passed ListProduct struct to a shopware 3-4 array structure.
     *
     * @param StoreFrontBundle\Struct\ListProduct $product
     * @return array
     */
    public function convertListProductStruct(StoreFrontBundle\Struct\ListProduct $product)
    {
        return $this->legacyStructConverter->convertListProductStruct($product);
    }

    /**
     * Converts the passed ProductStream struct to an array structure.
     *
     * @param StoreFrontBundle\Struct\ProductStream $productStream
     * @return array
     */
    public function convertRelatedProductStreamStruct(StoreFrontBundle\Struct\ProductStream $productStream)
    {
        return $this->legacyStructConverter->convertRelatedProductStreamStruct($productStream);
    }

    /**
     * @param StoreFrontBundle\Struct\Product $product
     * @return array
     */
    public function convertProductStruct(StoreFrontBundle\Struct\Product $product)
    {
        return $this->legacyStructConverter->convertProductStruct($product);
    }

    /**
     * @param StoreFrontBundle\Struct\Product\VoteAverage $average
     * @return array
     */
    public function convertVoteAverageStruct(StoreFrontBundle\Struct\Product\VoteAverage $average)
    {
        return $this->legacyStructConverter->convertVoteAverageStruct($average);
    }

    /**
     * @param StoreFrontBundle\Struct\Product\Vote $vote
     * @return array
     */
    public function convertVoteStruct(StoreFrontBundle\Struct\Product\Vote $vote)
    {
        return $this->legacyStructConverter->convertVoteStruct($vote);
    }


    /**
     * @param StoreFrontBundle\Struct\Product\Price $price
     * @return array
     */
    public function convertPriceStruct(StoreFrontBundle\Struct\Product\Price $price)
    {
        return $this->legacyStructConverter->convertVoteStruct($price);
    }

    /**
     * @param StoreFrontBundle\Struct\Media $media
     * @return array
     */
    public function convertMediaStruct(StoreFrontBundle\Struct\Media $media)
    {
        return $this->legacyStructConverter->convertMediaStruct($media);
    }

    /**
     * @param StoreFrontBundle\Struct\Product\Unit $unit
     * @return array
     */
    public function convertUnitStruct(StoreFrontBundle\Struct\Product\Unit $unit)
    {
        return $this->legacyStructConverter->convertUnitStruct($unit);
    }

    /**
     * @param StoreFrontBundle\Struct\Product\Manufacturer $manufacturer
     * @return string
     */
    public function getSupplierListingLink(StoreFrontBundle\Struct\Product\Manufacturer $manufacturer)
    {
        return $this->legacyStructConverter->getSupplierListingLink($manufacturer);
    }

    /**
     * Example:
     *
     * return [
     * 9 => [
     * 'id' => 9,
     * 'optionID' => 9,
     * 'name' => 'Farbe',
     * 'groupID' => 1,
     * 'groupName' => 'Edelbrände',
     * 'value' => 'goldig',
     * 'values' => [
     * 53 => 'goldig',
     * ],
     * ],
     * 2 => [
     * 'id' => 2,
     * 'optionID' => 2,
     * 'name' => 'Flaschengröße',
     * 'groupID' => 1,
     * 'groupName' => 'Edelbrände',
     * 'value' => '0,5 Liter, 0,7 Liter, 1,0 Liter',
     * 'values' => [
     * 23 => '0,5 Liter',
     * 24 => '0,7 Liter',
     * 25 => '1,0 Liter',
     * ],
     * ],
     * ];
     *
     * @param StoreFrontBundle\Struct\Property\Set $set
     * @return array
     */
    public function convertPropertySetStruct(StoreFrontBundle\Struct\Property\Set $set)
    {
        return $this->legacyStructConverter->convertPropertySetStruct($set);
    }

    /**
     * @param StoreFrontBundle\Struct\Property\Group $group
     * @return array
     */
    public function convertPropertyGroupStruct(StoreFrontBundle\Struct\Property\Group $group)
    {
        return $this->legacyStructConverter->convertPropertyGroupStruct($group);
    }

    /**
     * @param StoreFrontBundle\Struct\Property\Option $option
     * @return array
     */
    public function convertPropertyOptionStruct(StoreFrontBundle\Struct\Property\Option $option)
    {
        return $this->legacyStructConverter->convertPropertyOptionStruct($option);
    }


    /**
     * @param StoreFrontBundle\Struct\Product\Manufacturer $manufacturer
     * @return array
     */
    public function convertManufacturerStruct(StoreFrontBundle\Struct\Product\Manufacturer $manufacturer)
    {
        return $this->legacyStructConverter->convertManufacturerStruct($manufacturer);
    }

    /**
     * @param StoreFrontBundle\Struct\ListProduct $product
     * @param StoreFrontBundle\Struct\Configurator\Set $set
     * @return array
     */
    public function convertConfiguratorStruct(
        StoreFrontBundle\Struct\ListProduct $product,
        StoreFrontBundle\Struct\Configurator\Set $set
    ) {
        return $this->legacyStructConverter->convertConfiguratorStruct($product, $set);
    }


    /**
     * Creates the settings array for the passed configurator set
     *
     * @param StoreFrontBundle\Struct\Configurator\Set $set
     * @param StoreFrontBundle\Struct\ListProduct $product
     * @return array
     */
    public function getConfiguratorSettings(
        StoreFrontBundle\Struct\Configurator\Set $set,
        StoreFrontBundle\Struct\ListProduct $product
    ) {
        return $this->legacyStructConverter->getConfiguratorSettings($product, $set);
    }

    /**
     * Converts a configurator option struct which used for default or selection configurators.
     *
     * @param StoreFrontBundle\Struct\Configurator\Group $group
     * @param StoreFrontBundle\Struct\Configurator\Option $option
     * @return array
     */
    public function convertConfiguratorOptionStruct(
        StoreFrontBundle\Struct\Configurator\Group $group,
        StoreFrontBundle\Struct\Configurator\Option $option
    ) {
        return $this->legacyStructConverter->convertConfiguratorOptionStruct($group, $option);
    }

}

 

public function registerListProductService() 
{ 
    $coreService = Shopware()->Container()->get('legacy_struct_converter'); 
    Shopware()->Container()->set( 'legacy_struct_converter', new OwnLegacyStructConverter($coreService) ); 
}

sieht Dein listener den jetzt auch so aus? Oder übergibst Du den $coreService nicht?

1 „Gefällt mir“

Ok, Jetzt funktioniert es. Vielen Dank ich kann dir garnicht genug danken!