Banner Logo

Be a part of the family: Connect, Receive Support,
Contribute, and Reap Abundant Rewards!

Updating product vendor programatically

Theme and Plugin Support

Resolved
Viewing 8 reply threads
  • Author
    Posts
    • #135849
      odedtalmon
      Participant

      Hello,
      Is it possible to update a product vendor programmatically? Do you have a built-in function that does that?
      This doesn’t work:

      update_post_meta($product_id,'choose_vendor',$value);
      Copy
      update_post_meta($product_id,'_choose_vendor',$value);
      Copy

      Thanks

    • #135893

      Hi,
      You can check by using this way

      wp_delete_object_term_relationships($product->ID, $WCMp->taxonomy->taxonomy_name);
      wp_set_object_terms($product->ID, (int) $term_id, $WCMp->taxonomy->taxonomy_name, true);
      Copy

      Thanks

    • #136036
      odedtalmon
      Participant

      Hello again,
      I don’t understand what needs to go in the $WCMp->taxonomy->taxonomy_name variable.
      The term id is this: $vendor_term_id = $vendor->get_term_id(); ?

      Thanks

    • #136113

      Hi,
      Taxonomy is the same as woocommerce. In our case it is dc_vendor_shop.
      2nd one is right
      Thanks

    • #136119
      odedtalmon
      Participant

      I tried to run this code:
      $product_id = 45227;
      $vendor = get_wcmp_vendor( 8096 );
      $vendor_term_id = $vendor->get_term_id();

      wp_delete_object_term_relationships( $product_id, ‘dc_vendor_shop’ );
      wp_set_object_terms( $product_id, $vendor_term_id, ‘dc_vendor_shop’, true );

      That didn’t change the vendor for the product. What should I do?

      Thanks.

    • #136156

      Hi,
      check this code https://github.com/wcmarketplace/dc-woocommerce-multi-vendor/blob/master/classes/class-wcmp-product.php#L965
      you might put the wrong term id. check that line. Do the same as mentioned above.
      Thanks

    • #136410
      odedtalmon
      Participant

      Hello,
      I still don’t understand what the term for that vendor actually is. I am using the built in function of the vendor class

      get_term_id()
      Copy

      , how could that be wrong? What is the correct term id that should be places in that function?
      In any case, I tried updating to code according to the example you sent and it’s still not working.
      Here’s the code:

      $vendor_term_id = $WCMp_Vendor->get_term_id();
      $term = get_term( $vendor_term_id, $WCMp->taxonomy->taxonomy_name );

      if( $term ) {
      wp_delete_object_term_relationships( $product_id, $WCMp->taxonomy->taxonomy_name );
      wp_set_object_terms( $product_id, (int) $term->term_id, $WCMp->taxonomy->taxonomy_name, true );
      }

      $vendor = get_wcmp_vendor_by_term( absint( $term->term_id ) );

      if( !wp_is_post_revision( $product_id ) && $vendor ) {
      $WCMp_Product = new WCMp_Product();
      // unhook this function so it doesn’t loop infinitely
      remove_action( ‘save_post’, ‘process_vendor_data’ );
      // update the post, which calls save_post again
      wp_update_post( array( ‘ID’ => $product_id, ‘post_author’ => $vendor->id ) );
      // re-hook this function
      add_action( ‘save_post’, array( $WCMp_Product, ‘process_vendor_data’, $product_id ) );
      }

    • #136411
      odedtalmon
      Participant
      This reply has been marked as private.
    • #136423
      This reply has been marked as private.
Viewing 8 reply threads

The topic ‘Updating product vendor programatically’ is closed to new replies.