Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save darookee/9d89766c720691861516679d036f15fb to your computer and use it in GitHub Desktop.
Save darookee/9d89766c720691861516679d036f15fb to your computer and use it in GitHub Desktop.
Show event title and date on WooCommerce cart page
<?php
add_filter( 'woocommerce_cart_item_name', 'krbu_event_title', 10, 3 );
function krbu_event_title($title, $values, $cart_item_key) {
$ticket_meta = get_post_meta($values['product_id']);
$event_id = absint($ticket_meta['_tribe_wooticket_for_event'][0]);
$venue_id = tribe_get_venue_id($event_id);
if ($event_id) {
$eventLink = get_permalink($event_id);
if (!stristr($title, $eventLink)) {
$title = sprintf( '%s for <a href="%s" target="_blank"><strong>%s</strong></a> on %s', $title, $eventLink, get_the_title($event_id), tribe_get_start_date($event_id));
if ($venue_id) {
$venue = tribe_get_venue_details($venue_id);
$title = sprintf('%s at %s<br /><small>%s</small>', $title, $venue['linked_name'], $venue['address']);
}
}
}
return $title;
}
@darookee
Copy link
Author

darookee commented Jul 4, 2023

When using 'Avada' the $title is also used inside the aria-label of the 'Remove' x and is not properly escaped. The original code looks like this:

<?php // ThemeFusion edit for Avada theme: customize item removal. ?>
    <td class="product-remove">
      <?php
        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 ) ),
            sprintf( esc_attr__( 'Remove %s', 'woocommerce' ), $product_name ),
            esc_attr( $product_id ),
            esc_attr( $_product->get_sku() )
          ),
          $cart_item_key
        );
      ?>
    </td>

To fix this one could replace this line:

sprintf( esc_attr__( 'Remove %s', 'woocommerce' ), $product_name ),

with this

sprintf( esc_attr__( 'Remove %s', 'woocommerce' ), $_product->get_name() ),

which will insert the original product name into the label.

@darookee
Copy link
Author

darookee commented Jul 4, 2023

Alternatively one could use the woocommerce_cart_item_remove_link filter to fix this:

add_filter( 'woocommerce_cart_item_remove_link', 'krbu_event_title_remove_link', 10, 3 );

function krbu_event_title_remove_link($remove_link, $cart_item_key) {
    if (null === WC()->cart) {
        wc_load_cart();
    }
    if (null === WC()->cart) {
        return $remove_link;
    }
    $cart_item = WC()->cart->get_cart_item( $cart_item_key );
    $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

    return 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 ) ),
            sprintf( esc_attr__( 'Remove %s', 'woocommerce' ), $_product->get_name() ),
            esc_attr( $product_id ),
            esc_attr( $_product->get_sku() )
          );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment