Add a ‘Remove from Cart’ button on dropdown cart

Question

I got a simple WP page using Avada theme.
In Avada theme we got a cart dropdown in the menu, where it currently doesn’t have any remove item button out for the items.

I’ve tried to copy the HTML tag from the cart page as entire, and just add it in as such in avada-functions.php:

if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
                        $items .= '<div class="fusion-menu-cart-item">';
                        $items .= '<a href="' . $product_link . '">';
                        $items .= get_the_post_thumbnail( $thumbnail_id, 'recent-works-thumbnail' );

                        // Check needed for pre Woo 2.7 versions only.
                        $item_name = method_exists( $_product, 'get_name' ) ? $_product->get_name() : $cart_item['data']->post->post_title;

                        $items .= '<div class="fusion-menu-cart-item-details">';
                        $items .= '<span class="fusion-menu-cart-item-title">' . $item_name . '</span>';

                        $product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        if ( '' !== $product_price ) {
                            $product_price = ' x ' . $product_price;
                        }
                        echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            'woocommerce_cart_item_remove_link',
                            sprintf(
                                '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
                                esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
                                esc_html__( 'Remove this item', 'woocommerce' ),
                                esc_attr( $product_id ),
                                esc_attr( $_product->get_sku() )
                            ),
                            $cart_item_key
                        );
                        $items .= '<span class="fusion-menu-cart-item-quantity">' . $cart_item['quantity'] . $product_price . '</span>' . '<th class="product-remove">&nbsp;</th>';
                        $items .= '</div>';
                        $items .= '</a>';
                        $items .= '</div>';
                    }

The whole if statement normally looks just like this.

if ( ! function_exists( 'avada_nav_woo_cart' ) ) {
    /**
     * Woo Cart Dropdown for Main Nav or Top Nav.
     *
     * @param string $position The cart position.
     * @return string HTML of Dropdown
     */
    function avada_nav_woo_cart( $position = 'main' ) {

        if ( ! class_exists( 'WooCommerce' ) ) {
            return '';
        }

        $woo_cart_page_link       = wc_get_cart_url();
        $cart_link_active_class   = '';
        $cart_link_active_text    = '';
        $is_enabled               = false;
        $main_cart_class          = '';
        $cart_link_inactive_class = '';
        $cart_link_inactive_text  = '';
        $items                    = '';
        $cart_contents_count      = WC()->cart->get_cart_contents_count();

        if ( 'main' === $position ) {
            $is_enabled               = Avada()->settings->get( 'woocommerce_cart_link_main_nav' );
            $main_cart_class          = ' fusion-main-menu-cart';
            $cart_link_active_class   = 'fusion-main-menu-icon fusion-main-menu-icon-active';
            $cart_link_inactive_class = 'fusion-main-menu-icon';

            if ( Avada()->settings->get( 'woocommerce_cart_counter' ) ) {
                if ( $cart_contents_count ) {
                    $cart_link_active_text = '<span class="fusion-widget-cart-number">' . $cart_contents_count . '</span>';
                }
                $main_cart_class .= ' fusion-widget-cart-counter';
            } elseif ( $cart_contents_count ) {
                // If we're here, then ( Avada()->settings->get( 'woocommerce_cart_counter' ) ) is not true.
                $main_cart_class .= ' fusion-active-cart-icons';
            }
        } elseif ( 'secondary' === $position ) {
            $is_enabled             = Avada()->settings->get( 'woocommerce_cart_link_top_nav' );
            $main_cart_class        = ' fusion-secondary-menu-cart';
            $cart_link_active_class = 'fusion-secondary-menu-icon';
            /* translators: Number of items. */
            $cart_link_active_text    = sprintf( esc_html__( '%s Item(s)', 'Avada' ), $cart_contents_count ) . ' <span class="fusion-woo-cart-separator">-</span> ' . WC()->cart->get_cart_subtotal();
            $cart_link_inactive_class = $cart_link_active_class;
            $cart_link_inactive_text  = esc_html__( 'Cart', 'Avada' );
        }

        $highlight_class = '';
        if ( 'bar' === Avada()->settings->get( 'menu_highlight_style' ) ) {
            $highlight_class = ' fusion-bar-highlight';
        }
        $cart_link_markup = '<a class="' . $cart_link_active_class . $highlight_class . '" href="' . $woo_cart_page_link . '"><span class="menu-text" aria-label="' . esc_html__( 'View Cart', 'Avada' ) . '">' . $cart_link_active_text . '</span></a>';

        if ( $is_enabled ) {
            if ( is_cart() ) {
                $main_cart_class .= ' current-menu-item current_page_item';
            }

            $items = '<li class="fusion-custom-menu-item fusion-menu-cart' . $main_cart_class . '">';
            if ( $cart_contents_count ) {
                $checkout_link = wc_get_checkout_url();

                $items .= $cart_link_markup;
                $items .= '<div class="fusion-custom-menu-item-contents fusion-menu-cart-items">';
                foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                    $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                    $product_link = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
                    $thumbnail_id = ( $cart_item['variation_id'] && has_post_thumbnail( $cart_item['variation_id'] ) ) ? $cart_item['variation_id'] : $cart_item['product_id'];

                    if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
                        $items .= '<div class="fusion-menu-cart-item">';
                        $items .= '<a href="' . $product_link . '">';
                        $items .= get_the_post_thumbnail( $thumbnail_id, 'recent-works-thumbnail' );

                        // Check needed for pre Woo 2.7 versions only.
                        $item_name = method_exists( $_product, 'get_name' ) ? $_product->get_name() : $cart_item['data']->post->post_title;

                        $items .= '<div class="fusion-menu-cart-item-details">';
                        $items .= '<span class="fusion-menu-cart-item-title">' . $item_name . '</span>';

                        $product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        if ( '' !== $product_price ) {
                            $product_price = ' x ' . $product_price;
                        }
                        $items .= '<span class="fusion-menu-cart-item-quantity">' . $cart_item['quantity'] . $product_price . '</span>';
                        $items .= '</div>';
                        $items .= '</a>';
                        $items .= '</div>';
                    }
                }

                $items .= '<div class="fusion-menu-cart-checkout">';
                $items .= '<div class="fusion-menu-cart-link"><a href="' . $woo_cart_page_link . '"><span class>' . esc_html__( 'View Cart', 'Avada' ) . '</span></a></div>';
                $items .= '<div class="fusion-menu-cart-checkout-link"><a href="' . $checkout_link . '"><span>' . esc_html__( 'Checkout', 'Avada' ) . '</span></a></div>';
                $items .= '</div>';
                $items .= '</div>';
            } else {
                $items .= '<a class="' . $cart_link_inactive_class . $highlight_class . '" href="' . $woo_cart_page_link . '"><span class="menu-text" aria-label="' . esc_html__( 'View Cart', 'Avada' ) . '">' . $cart_link_inactive_text . '</span></a>';
            }
            $items .= '</li>';
        }
        return $items;
    }
}

Nothing appears. I know this is not best practise to hardcode in themefiles, I was simply testing out, if I was on any path.

How could I do this in a more efficient and actually working way?

Thank you.

EDIT: The button actually appeared. Just way out of the tag it supposed to be in and actually out of the menu itself, all up in the header. It worked like a charm. I just need some help to how to make this best practise, so the theme can be updated with no issues.

0
Jannick 4 months 0 Answers 18 views 0

Leave an answer