• Resolved marquestiondeliminate

    (@marquestiondeliminate)


    Hi everyone,

    I don’t know if it’s right to ask here.
    But I think, I haven’t found the right one from the various topics that I got by searching from my question.

    A brief description :
    I have website A.
    And want to send updates to website B when there is an update, delete or create a new post.

    website B gave me:
    url = https://api.domain.com/v2/bo/post/product-sync
    basic auth = basicauth:password.

    I created and added some code in function.php on my child theme.

    Here’s my code snippet.

    // Send update from Web A to Web B start HERE
    // register new function
    function get_course_prod_id($prod_id) {
    
        $course_args = array(
            'post_type' => 'courses',
            'meta_key' => '_tutor_course_product_id',
            'meta_value' => $prod_id
        );
    
        $course_loop = new WP_Query( $course_args ); 
    
        $coursereturnarray      = array();
        $mentorreturnarray      = array();
        $categoryreturnarray    = array();
    
        while ( $course_loop->have_posts() ) : $course_loop->the_post();
           
        $course_categories = get_tutor_course_categories();
    
        if(!empty($course_categories) && is_array($course_categories ) && count($course_categories)){
        
            $ind = 0;
        
            foreach ($course_categories as $course_category){
                $categoryreturnarray[]= $category_name['course_category'] = $course_category->name;
                $ind++;
            }
        }
    
        $coursereturnarray[] = array(
            'id'        => get_the_ID(),
            'name'      => get_the_title(),
            'url'       => get_the_permalink(),
            'mentor_id' => get_the_author_meta('id' , get_current_user_id()),
            'category'  => $categoryreturnarray
        );
    
        $mentorreturnarray[] = array(
         get_the_author_meta('id' , get_current_user_id())
         );
     
        endwhile;
    
        return array($coursereturnarray,$mentorreturnarray,$categoryreturnarray);
    
    }
    
    // register new action for trigger update,create, delete post data
    function get_action($create_data,$update_at,$status) {
        
        $action = "";
        
        if($status == "trash") {
            $action = "delete";
        }else {
            
            if($create_data == $update_at) {
                $action = "create";
            }else {
                $action = "update";
            }
        }
        return $action;
    }
    
    // register data when send json
    function unified_platform($parameter) {
    
        $per_page = $parameter['per_page'];
    
        $args = array(  
            'post_type'         => "product",
            'order'             => 'ASC',
            'post_status'       => array('publish', 'trash'),
            'posts_per_page'    => 200,
        );
        
        $loop = new WP_Query( $args ); 
        $datareturn = array();
    
        while ( $loop->have_posts() ) : $loop->the_post();
        $action = get_action(get_the_date('Y-m-d H:i:s'), get_the_modified_date('Y-m-d H:i:s'),get_post_status());
    
             $product_data = array(
               'platform'           => get_bloginfo(),
               'action'             => $action,
               'product_id'         => get_the_ID(),
               'product_name'       => get_the_title(),
               'product_slug'       => get_post_field('post_name', get_the_ID()),
               'product_permalink'  => get_the_permalink(),
               'product_url_image'  => wp_get_attachment_url(get_post_thumbnail_id(get_the_ID())),
               'price'              => get_post_meta( get_the_ID(), '_price', true),
               'regular_price'      => get_post_meta( get_the_ID(), '_regular_price', true),
               'status'             => get_post_status(),
               'stock_status'       => get_post_meta( get_the_ID(), '_stock_status', true),
               'stock_quantity'     => get_post_meta( get_the_ID(), '_stock', true),
               'total_sales'        => get_field('total_sales'),
             );
    
             $data = get_course_prod_id(get_the_ID());
    
             $courses = array('courses' => $data[0]);
    
             //$mentor = array('mentorIDs' => get_the_author_meta('id' , get_current_user_id()));
             //$mentor_user_login = get_the_author_meta('user_login' , get_current_user_id());
             $mentor = array('mentorIDs' => get_the_author_meta('user_login' , get_current_user_id()));
    
             $categories = array('categories' => $data[2]); 
    
             $published_date = array('published_at' => get_the_date('Y-m-d H:i:s'));
             
             $datareturn[] = array_merge($product_data,$courses,$mentor,$categories, $published_date);
    
        endwhile;
    
        wp_reset_query();
      if ( empty( $datareturn ) ) {
        return null;
      }
      return $datareturn;
    
    //Then send from website A to B Here
      $url = 'https://api.domain.com/v2/bo/post/product-sync';
      $arguments = array (
          'method'          => 'POST',
          'body'            => $datareturn,
          'Authorization'   => 'Basic<basicauth:password>',
          'Content-Type'    => 'application/json'
      );
    
      $response = wp_get_post ( $url, $arguments );
    
      if ( is_wp_erro( $response ) ) {
          $error_message = $response->get_error_message();
          echo "Something When Wrong: $error_message";
      } else {
      echo 'Response:<pre>';
      print_r( $response );
      echo '</pre>';
      }
    }
    
    add_action( 'rest_api_init', function () {
      register_rest_route( 'unified', '/new-product/', array(
        'methods' => 'GET',
        'callback' => 'unified_platform',
      ) );

    Is there something wrong with my code?
    I really appreciate it and hope someone can help me here.

    Thank you in advance ??

Viewing 1 replies (of 1 total)
  • Hi there @marquestiondeliminate ??

    Send data to external websites when there is an update or create a post

    Is there something wrong with my code?
    I really appreciate it and hope someone can help me here.

    Thanks for reaching out. From what I gather, with this custom code you are sharing, websiteB needs to be updated when there is a course change in websiteA.

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    Kindly keep in mind we are not developers and only offer support for existing functionality.

    Please see our Support Policy: https://www.woocommerce.com/support-policy/

    For assistance with customization or development with your site, we recommend that you seek help from:

    * A local web developer
    * Codeable.io
    * WooExperts
    * Stackexchange

    If you are comfortable coding yourself and have questions, I would also recommend that you consider:

    * WooCommerce developer Portal
    * WooCommerce Slack Community
    * Advanced WooCommerce Facebook group

    I hope that helps you to figure it out.

    Feel free to get back to us if you have further questions.

    • This reply was modified 2 years, 3 months ago by anastas10s. Reason: typo
Viewing 1 replies (of 1 total)
  • The topic ‘Send data to external websites when there is an update or create a post’ is closed to new replies.