0

In Magento 2

I want to override Magento\Catalog\Block\Product\AbstractProduct

I just want to pass my custom add to cart url before passing it to magento.

this function getAddToCartUrl($product, $additional = [])

So far I've done in 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\Catalog\Block\Product\AbstractProduct" type="Vendor\Module\Block\Product\AbstractProduct" />
</config>

And in app\code\Vendor\Module\Block\Product\AbstractProduct.php I've following content.

namespace Vendor\Module\Block\Product;

class AbstractProduct extends Magento\Catalog\Block\Product\AbstractProduct
{
    public function getAddToCartUrl($product, $additional = [])
    {
       echo 'test';
       exit;
    }
}

Any suggestions if I want to process something before product is been added to magento cart.

0

You should try with Plugin:

app/code/Company/Module/etc/frontend/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">

    <type name="Magento\Catalog\Block\Product\AbstractProduct">
        <plugin name="customParams"
                type="Company\Module\Model\Plugin\Block\Product"/>
    </type>
</config>

app/code/Company/Module/Model/Plugin/Block/Product.php

<?php

namespace Company\Module\Model\Plugin\Block;

class Product
{
    public function beforeGetAddToCartUrl(
        \Magento\Catalog\Block\Product\AbstractProduct $subject,
        $product, $additional = []
    )
    {
        //Your custom code here, can edit $additional param

        return [$product,  $additional];
    }
}
  • Okay let me try... – Kaushal Suthar Feb 10 '17 at 12:47
  • @KaushalSuthar are you able to resolve this? – Deeps Apr 26 '18 at 14:46
0

You can't override classes having "Abstract" keyword in Magento. Alternatively, you can override the class \Magento\Catalog\Block\Product\View as it is the child class of \Magento\Catalog\Block\Product\AbstractProduct, it should possesses all the property's and functions of it's base class.

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.