• Is there a way to get the parent category and/or multiple categories for a woocommerce product to be added to the array in the datalayer? I am only getting one category listed when others are selected.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi,

    Thanks for your question.

    This is currently not possible without additional coding on your side.
    Looping through all parent categories is a bit resource consuming especially on pages where lots of products are being shown therefore I was always on the opinion that if someone needs the full category path, this should be implemented individually.

    Maybe an extra option on the admin backend could solve this: letting most users to use the functionality as it works now and allowing others to add the full path by informing them that this could slow down the site if lots of users are on the pages concurrently.

    Thomas

    Performance issues aside, if someone wanted to include all categories for a product, it sounds like they should be separated by a ‘/’, correct? (ref: https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce).

    To do this, I was thinking it would be possible to replace lines 38-39 in woocommerce.php with:

    $product_cat = implode('/', wp_list_pluck($_product_cats, 'name'));

    This seems to almost work, but the resulting output is:

    “category”:”Parent\/Child”

    I don’t believe in this case the / character should be escaped. Do you agree, and is there a possible solution for this?

    As a side note, an option to turn this on would be a helpful feature in some cases. In our particular case we need to be able to group traffic by the parent category.

    Thanks!

    Tim

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi Tim,

    I’ve added this thread to my issue tracker so that I can review your suggestions and add this new feature to the plugin:
    https://github.com/duracelltomi/gtm4wp/issues/32

    Currently the plan is to implement this in v1.8 but this can change depending on what other requests come in and with what priority.

    Thomas

    Thanks!

    One question. I’m looking to implement this myself using a filter (or even modding the plugin, temporarily, if necessary). I have this working:

    function eec_product_array_func($data) {	
    	if (isset($data['category'])) {
    		$_product_cats = get_the_terms($post->ID, 'product_cat');
    		if (!empty($_product_cats)) { 
    			$product_cat = implode('/', wp_list_pluck($_product_cats, 'name'));	
    			$data['category'] = $product_cat;	
    		}
    	}
    	
    	return $data;
    }
    add_filter(GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, 'eec_product_array_func');

    NOTE: This is not final code, as it only works correctly on the product page and not all instances of this filter. Included for an example right now.

    The problem is esc_js still causes the separators to come out \/ between category names. Do you have a recommendation on how to solve this, by chance?

    Thanks,

    Tim

    • This reply was modified 7 years, 8 months ago by Tim.

    It seems the slashes can be fixed by changing line 593 in frontend.php to:

    $dl_json_data = json_encode( $gtm4wp_datalayer_data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES );

    A filter then can be used as mentioned above, although I still need to work out a version that works on category pages, etc. I also assume something would have to be worked out for PHP < 5.4.0 (line 595).

    Do you see any harm in using JSON_UNESCAPED_SLASHES in your experience?

    Thanks,

    Tim

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi Tim,

    Since JSON_UNESCAPED_SLASHES was added in PHP 5.4.0, I would expect some problems while releasing this in a new plugin version. I know PHP5.x is quite an old branch but you would be surprised how many error reports I’ve got when I was adding a feature that required at least PHP 5.3 :-))

    Even Godaddy is still running some servers with PHP 5.2

    I could imagine this in two steps: first to release a new plugin version where an admin notice will be shown that the next version will require PHP 5.4 and the next plugin version could include changes like this one.

    Thomas

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Woocommerce Parent Category Not Parsing’ is closed to new replies.