WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
Just to say thanks :-) (4 posts)

  1. Jason Judge
    Member
    Posted 1 year ago #

    The functionality of this plugin should be in core WordPress IMO, but either way, it has been great to use.

    We have recently launched it on this site running WooCommerce:

    http://www.soulbrother.com/

    Each album can have track samples linked to it, using Posts 2 Posts. We have created some actions so that as the tracks are loaded using a CSV loader, they are automatically linked to the albums (products) using the SKU. That works really smoothly (I can provide more details on how to do this if it is any use).

    The albums/products are also linked to "feature posts" that you see on the front page. These posts allow the store owner to put together lists of albums that are linked arbitrarily - albums of the week or month, top new releases/reissues etc.

    Hope this does not look like blogspam. It is more intended as a case study of what this plugin can do.

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. raider5
    Member
    Posted 1 year ago #

    Hi judgej,

    I'm looking in to using a CSV file to load "Posts to Posts" relationships too. If the offer to provide more details on this is still open I'd be really interested in how you tackled it?

    Thanks

  3. Jason Judge
    Member
    Posted 1 year ago #

    I'll try to explain, but this will not contain all the detail - hopefully it will give you a good start to work from

    First we have two hooks to catch the uploading of tracks to the WP site. Here is the first:

    function my_updated_post_meta($meta_id, $post_id, $meta_key, $meta_value)
    {
        if ($meta_key != 'sku') return;
        if (wp_is_post_revision($post_id)) return;
        if (get_post_type($post_id) != 'track') return;
        if (trim($meta_value) == '') return;
        link_product_track($post_id, trim($meta_value));
    }

    So that hook checks that we are setting the sku in the track being created, checks we are not catching an auto-save, checks we are posting to the post type "track" and that the sku is set. If all of these things are true, then we have the track post id and we have the sku, so pass these to list_product_track(). What that function does is find the product (the album) with that SKU, then links the track to that product using the posts-to-posts API.

    Here is the second hook that catches other instances of the track being created from a CSV file:

    function my_save_post($post_id) {
            global $post;
            if (!empty($post)) $post_id = $post->ID;
            if (!function_exists('p2p_type')) return;
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
            if (wp_is_post_revision($post_id)) return;
            if (get_post_type($post_id) != 'track') return;
            $sku = trim(get_post_meta($post_id, 'sku', true));
            if ($sku == '') return;
            link_product_track($post_id, $sku);
        }

    In this case me make sure posts-to-posts is enabled, are not doing an autosave, the post type is "track", and the track has an SKU. Again, if all this is true we call link_product_track to link this track post to the product post with this SKU.

    Here are the action declarations:

    add_action('save_post', 'my_save_post', 999);
    add_action('updated_post_meta', 'my_updated_post_meta', 10, 4);
    add_action('added_post_meta', 'my_updated_post_meta', 10, 4);

    The function to create the link is here:

    function link_product_track($track_post_id, $sku)
        {
            // Find the album in the products with the same SKU.
            $products = get_posts(array(
                'post_type' => 'product',
                'numberposts' => 10, // There should only ever be one.
                'meta_query' => array(
                    array(
                        'key' => '_sku',
                        'value' => $sku,
                    )
                ),
                'post_status' => 'publish',
                'suppress_filters' => true, // Just to keep the overhead down
            ));
            if (empty($products)) return;
    
            foreach($products as $product) {
                p2p_type('products_to_tracks')
                    ->connect($product->ID, $track_post_id, array('date' => current_time('mysql')));
            }
    }

    This code can go into the theme's functions.php, or a plugin, and I would recommend putting it all into a class. But do go through it carefully so you understand how it works and what we are trying to do. In my case, the WooCommerce products ("products" being a post type) have an "sku" metadata field. Each product gets a unique SKU. We then load up multiple tracks from a CSV file into the "tracks" post type. One of the meta fields on that post type is also "sku". When they load, we want to join the tracks to the products by their sku.

    Hope that helps!

    -- Jason

  4. Jason Judge
    Member
    Posted 1 year ago #

    Just a note: the sku is stored in out tracks as the "sku" meta field. WooCommerce defines the sku as "_sku" (with an underscore) within its own products post type. That underscore is just to indicate that it is a core WooCommerce meta field and not a custom one. It makes no real difference to how the above works, but thought I should mention it in case you are scratching your head about the underscore.

    Why we need to catch the updates in two different actions is something I cannot remember. All I know is that if we don't, then when importing the tracks from CSV, sometimes the linking would work and sometimes it wouldn't. With both hooks it all works great.

    It's all real code, but I've stripped out comments and class names just to simplify the technique to its basic implementation.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.