Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm developing a small plugin for WooCommerce and I've been trying to find out how I can get the number of items submitted from the quantity field on a single-product page after the add-to-cart action has completed.

I have a custom popup for up-sells that appears after a customer has added an item to their cart and I would like to display information about the item they've just added (a success message) and their current cart totals as well as the up-sell products. I can get the product details (e.g. image, title, price etc.) and I have the cart item count and totals but what I can't seem to get is the number of items that were just submitted.

Any help please?

I have tried the $_REQUEST php super-global but no luck, WooCommerce must empty it once it has the data from the form submit. I have also tried looking in some of the WooCommerce globals too but can't find the relevant data!

I really appreciate any help or suggestions you can offer.

Thanks

share|improve this question
    
Never used WooCommerce but I don't see why you can't set your own $_SESSION (or $_POST) data when the form is submitted then retrieve that after. There may be a more WooCommerce way to do it but that should work. –  BandonRandon May 14 '13 at 8:09
    
I would but I don't know what hooks to use to intercept the form data before the WC function is called. I would need a pass-through function that jumps in to the execution queue before WC's "add_to_cart" function but passes the data on 'untouched' afterwards. I do not want to edit any of WC's files directly because this will cause problems with updating. –  Chris May 14 '13 at 8:57
add comment

2 Answers

Take a look at this function in woocommerce-ajax.php:

function woocommerce_ajax_add_to_cart() {
        global $woocommerce;

        $product_id        = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
        $quantity          = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

        if ( $passed_validation && $woocommerce->cart->add_to_cart( $product_id, $quantity ) ) {

            do_action( 'woocommerce_ajax_added_to_cart', $product_id );

            if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
                woocommerce_add_to_cart_message( $product_id );
                $woocommerce->set_messages();
            }

            // Return fragments
            woocommerce_get_refreshed_fragments();

        } else {...

This is the (ajax) add_to_cart function called from any product archive page. So at least the $product_id is passed to the action 'woocommerce_ajax_added_to_cart'. I suppose you can get the quantity by inspecting the cart within that action for the given product_id. But you'll probably only get the total quantity, not what was just added.

share|improve this answer
    
thanks @alvaro.d - I have looked at this function but I couldn't see a way to get at the raw $_POST data :( -- I also tried another function in the WC_Cart object but with no luck: docs.woothemes.com/wc-apidocs/… –  Chris May 14 '13 at 20:01
add comment

After it is added to the cart you can access the cart object, which will have all the items and their quantities.

global $woocommerce;
$cart_items =  $woocommerce->cart->get_cart();

I usually var_dump the cart on a hook on the cart page, though there are probably better ways to debug.

There's also a get_cart_item_quantities() method in the cart class. Check out the WC_Cart class in the includes\class-wc-cart.php file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.