Search code examples
phpwordpresswoocommercecarthook-woocommerce

Empty Array on first argument for woocommerce_add_cart_item_data filter hook


I am getting an empty array from the woocommerce_add_cart_item_data filter when a product is added to the shopping cart. Example added to functions.php of WordPress theme;

function TEST_post_filter($a,$b,$c) {

    print_r($a); //THIS IS EMPTY ARRAY ie returns Array()

    return $a;
}

add_filter('woocommerce_add_cart_item_data', 'Test_post_filter',10,3);

Any idea why this might be? I have found no reference to this issue anywhere. I have tried both basket behaviours ie Redirect to the basket page after successful addition and/ or Enable AJAX add to basket buttons on archives. I can't get my head around it. Plugins I have activated are WooCommerce, and WooCommerce Stripe Gateway.

UPDATE - Code which adds product options to product screen

    function option_add_to_product() {

        global $product;

        //get pizza categories
        $categories = get_terms( 'product_cat', array(
                'hide_empty' => false,
        ));

        $use_product = [];

        for($i=0;$i<count($categories);$i++) {

            if(strtolower($categories[$i]->slug) === 'bespoke_product')
                array_push($use_product,$categories[$i]->term_id);
        }

       $include_product = false;

        if(! count($use_product))
           return false;

        for($i=0;$i<count($use_product);$i++) {

            $this_product_categories = $product->get_category_ids();

            for($ii=0;$ii<count($this_product_categories);$ii++) {

                if($this_product_categories[$ii] === $use_prouduct[$i]) {
                    $include_product = true;
                    break;
                }
            }
        }

        if(! $include_product)
            return false;

        //template to add product option
        require plugin_dir_path(__FILE__) . 'templates/add_product_option.php';
    }

You can see the html from add_product_option.php will be added to the product if the product has a a category with the slug "bespoke_product". Note, this method of getting a match for a product is temporary (just technical debt for now).

When the filter woocommerce_add_cart_item_data is called the data posted from the product form is available to the corresponding function. Unsanitised data might read as follows:

        [extra_option_one] => 0
        [extra_option_two] => 0
        [extra_options_three] => 1
        [extra_option_four] => 0
        [order_note] => 

Each of those options will have a lookup table with a related price e.g. extra_options_three might be £1.50. This cost of this option and any others selected needs to up the price of the specific item being added to the cart. It should not be represented separately. The definition of the option added should go into the product field everywhere that purchased item is shown. I'm working through step by step, but it's not easy at the point I am at! I'm guessing the next is tha I'm going to be able to update the item in this order?


Solution

  • The woocommerce_add_cart_item_data hook is used to add custom data to the cart item on simple add to cart submission, like additional fields included in the <form> where "add to cart" button is located…

    So if you are not submitting extra custom data from custom fields on add to cart event on (from single product pages), the first function argument is going to be an empty array

    See those related answer threads using this hook.