Aleš Sýkora / February 15, 2024 / 0 comments

WooCommerce Block Cart: Extend product name

Post summary: Add emoji before and after product name in Cart.

Add emoji before and after product name in Cart.

<?php
function custom_woocommerce_checkout_modifications() {
    if ( function_exists( 'is_woocommerce' ) && ( is_cart() || is_checkout() ) ) {
        ?>
        <script type="text/javascript">
            jQuery( document ).ready( function( $ ) {
                const { registerCheckoutFilters } = window.wc?.blocksCheckout || {};
                if ( !registerCheckoutFilters ) return;

                const modifyItemName = ( defaultValue, extensions, args ) => {
                    const isCartContext = args?.context === 'cart';
                    if ( !isCartContext ) {
                        return defaultValue;
                    }
                    return `🪴 ${defaultValue} 🪴`;
                };

                registerCheckoutFilters( 'example-extension', {
                    itemName: modifyItemName,
                });
            });
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'custom_woocommerce_checkout_modifications' );

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts