Created
July 16, 2021 08:24
-
-
Save digitalchild/126b64c45f0fcf0e5bce54b34d913821 to your computer and use it in GitHub Desktop.
Add a div wrapper around the price and add to cart elements on the product loop for WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Author: Jamie Madden | |
// Link: https://wcvendors.com | |
// Remove product price from the loop | |
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); | |
// New overrides | |
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); | |
add_filter( 'woocommerce_get_price_html', 'add_opening_wrapper', 10, 2 ); | |
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_price_no_span', 9 ); | |
// Add opening <div> before price. Take span wrapper from loop/price.php template into price html | |
function add_opening_wrapper( $price_html, $product ){ | |
return '<div class="price_add_to_cart_same_line"><span class="price">' . $price_html . '</span>'; | |
} | |
// Override the product price html output in the loop | |
function woocommerce_template_loop_price_no_span(){ | |
global $product; | |
if ( $price_html = $product->get_price_html() ){ | |
echo $price_html; | |
} | |
} | |
// Add extra closing </div> after the add to cart aherf | |
add_filter( 'woocommerce_loop_add_to_cart_link', 'add_closing_wrapper', 10, 3 ); | |
function add_closing_wrapper( $html, $product, $args ){ | |
return sprintf( | |
'<a href="%s" data-quantity="%s" class="%s" %s>%s</a></div>', | |
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() ) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment