Stand with Ukraine. Fight for freedom and democracy

Donate
Troubleshooting //
Cannot Save Data Store IDs to Custom Content Type

Cannot Save Data Store IDs to Custom Content Type

Question

I have CPT products. There is an “Add to wish list” button on the website, and users can add the products to the wish list. I want to save those wish list items in CCT to show them to the admin or send via email. How to save data store IDs to Custom Content Type?

Answer

It can be easily done with the help of JetEngine and a bit of a custom code.

To enable saving the user’s wishlist to the CCT, head to WordPress Dashboard > Appearance > Theme Editor. Open the Theme Functions tab of your currently active WordPress theme.

Paste the following custom code to the functions.php file:

<?php
/**
 * Allows to update required CCT items on change posts in selected user meta data store
 */
add_filter( 'jet-engine/data-stores/ajax-store-fragments', function( $fragments, $store ) {
         // Replace 'quoted-list' with your actual Data Store slug
         $my_store = 'quoted-list';
         // Replace 'users_wishlist' with you actual CCT slug
        $my_cct = 'users_wishlist';
        
        // Replace 'post_ids' with your actual field name for the posts from the CCT
        $cct_posts_field = 'post_ids';
        // Replace 'user_id' with your actual field name for the user from the CCT
        $cct_user_field = 'user_id';
	if ( $my_store === $store->get_slug() ) {
             $posts   = $store->get_store();
             $cct     = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types( $my_cct );
                     $user_id = get_current_user_id();
                  	if ( $cct ) {
                           	$handler = $cct->get_item_handler();
                                         	$exists  = $cct->db->get_item( $user_id, $cct_user_field );
			$item_id = ! empty( $exists ) ? $exists['_ID'] : false;
			$handler->update_item( array(
				'_ID'            => $item_id,
				$cct_user_field  => $user_id,
				$cct_posts_field => $posts,
                             ) );
                   	}
	}
	// Do not remove this code to keep data stores working
	return $fragments;
}, 10, 2 );
save datastore id custom code in functions.php

Please, pay attention to these lines:

      // Replace ‘quoted-list’ with your actual Data Store slug
      $my_store = ‘quoted-list’;

       // Replace ‘users_wishlist’ with you actual CCT slug
      $my_cct = ‘users_wishlist’;

      // Replace ‘post_ids’ with your actual field name for the posts from the CCT
      $cct_posts_field = ‘post_ids’;

      // Replace ‘user_id’ with your actual field name for the user from the CCT
      $cct_user_field = ‘user_id’;

The code won’t work if you don’t replace the default values with your actual ones.

Once you add a code and the user saves the items to the wishlist, new CCT posts will appear in the dashboard containing the user ID and selected post IDs.

Was this article helpful?
YesNo
Still need help?
Submit a request