Hide Action-Edit on Cart Page for Simple Products in Magento 2

cartcheckoutmagento2

I'm really struggling with the following scenario:

On the checkout page, you have an action toolbar for each product:
– Add to wishlist
– Edit
– Remove

Now, I'm trying to hide the 'Edit' function from the frontend when the product that has been added to the cart is an simple product. The edit button has no function for these particular products, so I'd love to see it not being there.

I've been trying to modify the default.phtml, with the following code:

 <?php if (!$product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE): ?>
                <script type="text/javascript">window.alert('test alert')</script>
                <style type="text/css">.action.action-edit{display:none;}</style>
 <?php endif;?>

It verifies if the product is not an configurable product, and if not (so it is an simple product), it hides the action-edit. But of course, this will remove it for every item in cart.

I could not figure out where else to put the code or how to do it differently. If someone is able to help, I'd be grateful.

Best Answer

Okay, I finally figured it out. I found the .phtml responsible for rendering the edit button:

Magento_Checkout/view/frontend/templates/cart/item/renderer/actions/edit.phtml

I copied the file to:

app/design/frontend/Vendor/module/Magento_Checkout/templates/cart/item/renderer/actions

Inside this code I added the following variables:

$_item = $block->getItem();
$product = $_item->getProduct();

Next, I changed:

<?php if ($block->isProductVisibleInSiteVisibility()): ?>

To

<?php if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE): ?>

So the total looks like this:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

// needed variables for checking if product is configurable or not
$_item = $block->getItem();
$product = $_item->getProduct();
/** @var $block \Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit */
?>
<?php if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE): ?>
    <a class="action action-edit"
       href="<?= /* @escapeNotVerified */ $block->getConfigureUrl() ?>"
       title="<?= $block->escapeHtml(__('Edit item parameters')) ?>">
        <span>
            <?= /* @escapeNotVerified */ __('Edit') ?>
        </span>
   </a>
<?php endif ?>

And now the edit button only shows when the product in the cart page is a configurable product. Hope this will help others in the future too.