TransWikia.com

Hide action-edit on cart page if simple product

Magento Asked on December 7, 2021

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() == MagentoConfigurableProductModelProductTypeConfigurable::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.

2 Answers

You don't have to edit any template, you can simply do it with xml.

Add Magento_Checkout/layout/checkout_cart_item_renderers.xml to your theme:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.cart.item.renderers.simple.actions.edit" remove="true"/>
        <referenceBlock name="checkout.cart.item.renderers.grouped.actions.edit" remove="true"/>
    </body>
</page>

The xml above will remove the 'Edit' link block for simple and grouped products. This can be done for other product types as well (but for some you probably shouldn't):

<referenceBlock name="checkout.cart.item.renderers.downloadable.actions.edit" remove="true"/>
<referenceBlock name="checkout.cart.item.renderers.configurable.actions.edit" remove="true"/>
<referenceBlock name="checkout.cart.item.renderers.virtual.actions.edit" remove="true"/>
<referenceBlock name="checkout.cart.item.renderers.bundle.actions.edit" remove="true"/>
<referenceBlock name="checkout.cart.item.renderers.giftcard.actions.edit" remove="true"/>

Answered by wk_ on December 7, 2021

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() == MagentoConfigurableProductModelProductTypeConfigurable::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 MagentoCheckoutBlockCartItemRendererActionsEdit */
?>
<?php if ($product->getTypeId() == MagentoConfigurableProductModelProductTypeConfigurable::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.

Answered by Condor on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP