Viewing 7 reply threads
  • Author
    Posts
    • #8994
      levipe
      Participant

      Hi,

      I’ve just migrated woocommerce to 3.3 and piklist to 0.11.1

      I have a couple of related fields that were working fine before that migration.

      Since migration, its impossible for me to link products to articles in content-types I’ve created. They do appear on the admin product but ticking the boxes doesn’t help creating the relationships. They are not saved.

    • #9034
      levipe
      Participant

      up

    • #9040
      Steve
      Keymaster

      Please post your field code here: https://gist.github.com/

    • #9138
      levipe
      Participant

      So,

      I got it working… half.

      The parts-code looks like this:

      piklist('field', array(
          'type' => 'checkbox',
          'label' => 'Regions',
          'choices' => piklist( get_posts( array(
              'suppress_filters' => false,
              'post_status' => publish,
              'numberposts' => -1,
              'post_type' => 'region' )),
              array( 'ID', 'post_title' )
          ),
          'relate' => array( 'scope' => 'post' ),
          'attributes' => array( 'class' => 'multiple' )
      ));

      and yes, the relationships are stored, like this:

      post_ID: 41
      meta_key: __relate_post
      meta_value: 1707

      post_ID is a post of post_type region and meta_value is a post holding a product.

      But, the symmetric relationship is not stored!

      post_ID: 1707
      meta_key: __relate_post
      meta_value: 41

      is not present in the DB.

      This hasn’t consequences on my layout (working with has_post and post_belongs as need be), but the queries in WooCommerce don’t work anymore because of the lacking symmetric relationship post. In this instance, I cannot query on ‘region’.

    • #9141
      levipe
      Participant

      So, this is what I added to the function relate() in class-piklist-form.php to establish the 2-way relationship

                    add_metadata($object['scope_to'], $from, $meta_key, $to);
                    // added CG 22/7/2018
                    add_metadata($object['scope_to'], $to, $meta_key, $from);

      But, it’d be better if Steve could add that to the core code

    • #9153
      Steve
      Keymaster

      We updated the relate code recently. The docs go over how to use relate in detail:
      https://piklist.github.io/docs/tutorials/relationships/working-with-field-relationships/

    • #9154
      levipe
      Participant

      Yes Steve,

      I understand the code has been altered.

      But, as I wrote, this creates issues with the filter in WooCommerce.
      A related field in a product cannot be filtered with WooCommerce anymore.

      That’s why I suggested the above modification to the code.

      C

    • #9159
      Steve
      Keymaster

      If you look in the tutorial towards the end it says this:
      “So if you look at the book’s meta, you won’t see anything added about the chapter. Why? Because it’s not designed to put anything there. Look at the chapter’s meta. You should find it there. For this reason, you don’t need to specify a field, as relate fields are a special type which store in their own way. In short, the owned object stores the meta.”

      You need to use 'relate' => 'belongs' and 'relate' => 'has'

Viewing 7 reply threads
  • You must be logged in to reply to this topic.