Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save themepaint/7c1bd367c3d4370f20e7 to your computer and use it in GitHub Desktop.

Select an option

Save themepaint/7c1bd367c3d4370f20e7 to your computer and use it in GitHub Desktop.

Revisions

  1. themepaint created this gist Feb 21, 2016.
    33 changes: 33 additions & 0 deletions WooCommerce – Check if product is already in cart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php
    /**
    * Change the add to cart text on single product pages
    */
    add_filter('woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text');
    function woo_custom_cart_button_text() {

    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];

    if( get_the_ID() == $_product->id ) {
    return __('Already in cart - Add Again?', 'woocommerce');
    }
    }

    return __('Add to cart', 'woocommerce');
    }
    /**
    * Change the add to cart text on product archives
    */
    add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );
    function woo_archive_custom_cart_button_text() {

    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];

    if( get_the_ID() == $_product->id ) {
    return __('Already in cart', 'woocommerce');
    }
    }

    return __('Add to cart', 'woocommerce');
    }