• Hello, I’m trying to move the “view cart” next to the search form. I found the code:
    <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">View Cart <?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

    But am unable to have the “view cart” display and cannot get the price to not display. Basically, I’m trying to get the following:
    view Cart (2) with the shopping cart icon. (and the two would be if someone had 2 items in their cart, only displaying the items in cart). I’ve tried a bunch of things but nothing seems to work. Any suggestions would be greatly appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter girdy74

    (@girdy74)

    So figured out that I need to add a custom hook within my child theme but when I do the parent theme’s (storefront) woocommerce hooks take precedence. I know I need to change:
    add_action( 'storefront_header', 'storefront_header_cart', 60 );
    to
    add_action( 'storefront_header', 'storefront_header_cart', 10 );
    and that moves my cart to where I can work with it better but the only way it lets me do this is by changing the parent theme, which is not ideal since it will be removed on an update. Can someone help me with how I would implement this? Do i need to remove the action and create my own? If so, how would I go about doing that..?

    Thread Starter girdy74

    (@girdy74)

    One other thing I’m trying to accomplish is to just have the item count and not the cart total.

    You can do this in your child theme, just make sure your add_action() and remove_action() is in a separate function, hooked into init. It won’t work if you’ve just placed the code directly into your functions.php file.

    To just display the item count you’ll need to plug the storefront_cart_link() function in your child theme.

    Thread Starter girdy74

    (@girdy74)

    I was able to get the “view cart” button in the correct area with your recommendation. Thank you!

    I’m still having trouble with the “plug the storefront_cart_link() function in your child theme”. I’m not exactly certain how to do this. Does this go into the functions.php file?

    Hi,

    As you can see here Storefront checks for the existence of this function before declaring it. That means that if you define that function in your child theme it will over rule the one in the parent theme. So to make changes, just copy paste this into your child themes functions.php file:

    function storefront_cart_link() {
    		?>
    			<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
    				<?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) );?></span>
    			</a>
    		<?php
    	}

    Then just make whatever changes you need to make there.

    Cheers

    Thread Starter girdy74

    (@girdy74)

    Thanks again jameskoster! I’ve placed this in my child them functions file but when I reload the page you can see my changes for a split second and then it reverts back to the parent theme’s storefront_cart_link code.

    Thread Starter girdy74

    (@girdy74)

    This is what I have in my child theme functions.php:

    add_action( 'storefront_header', 'removeheadercart', 10);
    function removeheadercart() {
    remove_action( 'storefront_header', 'storefront_header_cart', 60 );
    add_action( 'storefront_header', 'custom_storefront_header_cart', 30 );
    }
    function custom_storefront_header_cart() { ?>
    		<ul class="site-header-cart menu">
    			<li class="<?php echo esc_attr( $class ); ?>">
    				<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">View Cart (<span class="count"><strong><?php echo wp_kses_data( sprintf( _n( '%d', '%d', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) );?></span></strong>)
    			</a>
    			</li>
    		</ul>
    		<?php
    		}

    Something is overriding it but I can’t figure out for the life of my where it’s coming from. Even when I test changing the storefront>inc>woocommerce>template-tags.php to what I want it to be above it’s doing the same thing. When I reload the page, it will show my custom cart (as above) but then it will default to the “$0.00 0 items” text. I know not to edit the core files but it was just a test to see if that template-tag was overriding my child theme functions.php file.

    It might just be the old version stored in the cache. Try adding a product to the cart to see if that updates it.

    Thread Starter girdy74

    (@girdy74)

    Thanks jameskoster. Yep, tried that but it still reverts back to the original code from somewhere! I’ve tried tracking it down but just can’t seem to find it.

    That’s strange.

    You could just try plugging the storefront cart function. Replace your code with:

    function storefront_cart_link() {
    		?>
    			<a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'storefront' ); ?>">
    				<?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) );?></span>
    			</a>
    		<?php
    	}

    And make your edits there.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘View Cart button – move beside search bar’ is closed to new replies.