Woo-commerce - Add To Cart loop - How to add html to button

Briany Hearne :

The Woocommerce template add-to-cart.php contains this code:

echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
    esc_url( $product->add_to_cart_url() ),
    esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
    esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
    isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
    esc_html( $product->add_to_cart_text() )
),
$product, $args );

I need to add this markup to the tag

<div class="icw"><i class="a3"></i></div>

I tried adding it directly but the new element was added after the actual link.

This is what I tried;

sprintf( '<a href="%s" data-quantity="%s" class="%s" %s><div class="icw"><i class="a3"></i></div>%s</a>',

Can anyone explain to me how I go about adding this element?

Many thanks.

7uc1f3r :

Read this before you continue

Is putting a div inside an anchor ever correct?


https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/loop/add-to-cart.php

This template can be overridden by copying it to yourtheme/woocommerce/loop/add-to-cart.php.


This seems to work without problems

echo apply_filters(
    'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
    sprintf(
        '<a href="%s" data-quantity="%s" class="%s" %s><i class="a3"></i>%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
        esc_html( $product->add_to_cart_text() )
    ),
    $product,
    $args
);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301813&siteId=1