WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Total number of items in cart (5 posts)

  1. Double-A-Ron
    Member
    Posted 1 year ago #

    Hi,

    What I'm looking to do is get the total number of items in the cart with a certain product attribute to stop people proceeding to checkout.

    So far I've managed to find out how to pull the total number of products in the cart and use this to hide the proceed to checkout button with the following code which works fine:

    $bottlesTotal = sprintf(_n('%d bottle', '%d bottles', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);

    But I now need to work out how to pull the total for items in the cart with the attribute of 'singleBottle' , this is the part which has got me totally stumped.

    Any help would be greatly appreciated .......... Aaron

    http://wordpress.org/extend/plugins/woocommerce/

  2. Double-A-Ron
    Member
    Posted 1 year ago #

    Hi there, little bit of progress. First off I'm no coding expert as you'll see but this is what I've managed to hack together so far:

    <?php

    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ){
    foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->exists() && $values['quantity'] > 0 && $_product->get_attribute( 'pa_product-type' ) == "singleBottle" ) {

    if ( $_product->is_sold_individually() ) {
    $product_quantity = '1';
    } else {
    $product_quantity = sprintf( '<input name="cart[%s][qty]" data-min="%s" data-max="%s" value="%s"/>', $cart_item_key, $data_min, $data_max, esc_attr( $values['quantity'] ) );
    }

    echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key );

    }
    }
    }

    ?>

    This basically gives me form inputs with the correct pre-filled cart quantities of all the products that have an attribute of 'singleBottle'.

    My next (and final) step which I'm really struggling with is how to get these quantities into a single array so I can total them up and use that figure as a variable to make the 'proceed to checkout' show/hide depending on the number of bottles in the cart.

    Again, sorry if this code doesn't look too fantastic and offends anyone but it's the best I can do with my (very) limited coding skills.

  3. varktech.com
    Member
    Posted 1 year ago #

    Hi Double,
    I think this plugin might do the trick: http://wordpress.org/extend/plugins/minimum-purchase-for-woocommerce/ . See what you think.

  4. Double-A-Ron
    Member
    Posted 1 year ago #

    Thanks for that Varktech.

    The pro version of your plugin was just what I was looking for. Managed to get me out of a pickle and meant I didn't have to spend the weekend on the problem.

  5. varktech.com
    Member
    Posted 1 year ago #

    Hey, glad that was what you needed. Cheers.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic