Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hanicker/63e6e80454493a2cd8d7ce7c8927fe20 to your computer and use it in GitHub Desktop.

Select an option

Save hanicker/63e6e80454493a2cd8d7ce7c8927fe20 to your computer and use it in GitHub Desktop.

Revisions

  1. @bekarice bekarice revised this gist Aug 17, 2016. 1 changed file with 6 additions and 9 deletions.
    15 changes: 6 additions & 9 deletions wc-prevent-checkout-for-cart-with-specific-category.php
    Original file line number Diff line number Diff line change
    @@ -36,19 +36,16 @@ function sv_wc_prevent_checkout_for_category() {
    * @return bool - true if the cart only contains the given category
    */
    function sv_wc_is_category_alone_in_cart( $category ) {

    // holds checks for all products in cart to see if they're in our category
    $category_checks = array();

    // check each cart item for our category
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    $product = $cart_item['data'];
    $product_in_cat = has_term( $category, 'product_cat', $product->id );

    array_push( $category_checks, $product_in_cat );
    // if a product is not in our category, bail out since we know the category is not alone
    if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
    return false;
    }
    }

    // return true if there are no other categories in the cart
    return ! in_array( false, $category_checks, true );
    // if we're here, all items in the cart are in our category
    return true;
    }
  2. @bekarice bekarice revised this gist Aug 16, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions wc-prevent-checkout-for-cart-with-specific-category.php
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ function sv_wc_prevent_checkout_for_category() {

    // check if this category is the only thing in the cart
    if ( sv_wc_is_category_alone_in_cart( $category ) ) {

    // render a notice to explain why checkout is blocked
    wc_add_notice( sprintf( 'Hi there! Looks like your cart only contains products from the %1$s category – you must purchase a product from another category to check out.', $category_name ), 'error' );
    }
    @@ -45,8 +45,8 @@ function sv_wc_is_category_alone_in_cart( $category ) {

    $product = $cart_item['data'];
    $product_in_cat = has_term( $category, 'product_cat', $product->id );
    array_push( $category_checks, $product_in_cat );
    array_push( $category_checks, $product_in_cat );
    }

    // return true if there are no other categories in the cart
  3. @bekarice bekarice created this gist Aug 16, 2016.
    54 changes: 54 additions & 0 deletions wc-prevent-checkout-for-cart-with-specific-category.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    <?php // only copy this line if needed

    /**
    * Renders a notice and prevents checkout if the cart
    * only contains products in a specific category
    */
    function sv_wc_prevent_checkout_for_category() {

    // set the slug of the category for which we disallow checkout
    $category = 'clothing';

    // get the product category
    $product_cat = get_term_by( 'slug', $category, 'product_cat' );

    // sanity check to prevent fatals if the term doesn't exist
    if ( is_wp_error( $product_cat ) ) {
    return;
    }

    $category_name = '<a href="' . get_term_link( $category, 'product_cat' ) . '">' . $product_cat->name . '</a>';

    // check if this category is the only thing in the cart
    if ( sv_wc_is_category_alone_in_cart( $category ) ) {

    // render a notice to explain why checkout is blocked
    wc_add_notice( sprintf( 'Hi there! Looks like your cart only contains products from the %1$s category &ndash; you must purchase a product from another category to check out.', $category_name ), 'error' );
    }
    }
    add_action( 'woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category' );


    /**
    * Checks if a cart contains exclusively products in a given category
    *
    * @param string $category the slug of the product category
    * @return bool - true if the cart only contains the given category
    */
    function sv_wc_is_category_alone_in_cart( $category ) {

    // holds checks for all products in cart to see if they're in our category
    $category_checks = array();

    // check each cart item for our category
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    $product = $cart_item['data'];
    $product_in_cat = has_term( $category, 'product_cat', $product->id );

    array_push( $category_checks, $product_in_cat );
    }

    // return true if there are no other categories in the cart
    return ! in_array( false, $category_checks, true );
    }