• Hi there. First time posting here. I’m actually in the middle of creating a custom importer from the inventory software my company uses to woocommerce.

    Your plugin has been great and with the little code snippets i’ve found lying around the internet i’ve been able to put together a fantastic way to use it.

    Unfortunately, i’ve hit a snag. As we use woocommerces latest REST API to dynamically adjust what attributes we have i’ll be needing to pick a colour for use of each term. EG: I’d like to have a colour picker on my uploader that use a term and will then push that to woocommerce attaching the colour to the term.

    As far as i’m aware, i can’t really pull any of the finer details of terms from woocommerces rest api. I can only pull a count of how many there are for the one labeled “colours”.

    Do you have any ideas on how i would go about doing this?

Viewing 1 replies (of 1 total)
  • I had the same problem, but with image swatches. I don’t think it’s possible with the WooCommerce API only to update all that is needed since you have to mess about with “term_meta” and term_taxonomies and etc. After wrestling with authentication difficulties using the wordpress REST API, I decided to go with old-faithful xmlrpc. This code could be re-written as a plugin (my plan) but for now it can just be put up on your server somewhere. I call this code from our custom db solution via CURL. It’s rough, but, might give anyone else wondering about this a starting point. It requires multiple calls to retrieve, delete/update/add those parts of the term_meta that can’t be set via the WooCommerce API. WordPress xmlrpc documentation is your friend.

    <?
    if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off"){ echo "ur doin it wrong https is better"; exit(); }
    
    $protocol='https://';
    
    $username=$_POST['username'];
    $password=$_POST['password'];
    $blogid=1; //Post ID
    $xmlrpcurl=$protocol.'URL-TO-YOUR-BLOG.com/xmlrpc.php';
    
    if($_POST['filename'] && $_POST['action']=="wp.uploadFile"){
            $item=$_POST['filename'];
            $prefix=$_POST['prefix'];
            $file=file_get_contents('https://URL-TO-YOUR-IMAGES.com/'.$filename.'.jpg');
            $filetype = "image/jpeg";
            $filename = $filename.".jpg";
            xmlrpc_set_type($file,'base64'); // <-- required!
    
            $params = array($blogid,$username,$password,
                            array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
            $request = xmlrpc_encode_request('wp.uploadFile',$params);
    }elseif($_POST['postid'] && $_POST['action']=="wp.deletePost"){
            $postid=$_POST['postid'];
            $params = array($blogid,$username,$password, $postid);
            $request = xmlrpc_encode_request('wp.deletePost',$params);
    }elseif($_POST['termid'] && $_POST['action']=="wp.editTerm"){
            $taxonomy=$_POST['taxonomy'];
            $termid=$_POST['termid'];
            $meta[]=array('key'=>$_POST['key'],'value'=>$_POST['value']);
            $metaParam=array('custom_fields'=>$meta, 'taxonomy'=>$taxonomy);
            $params = array($blogid,$username,$password, $termid,$metaParam);
            $request = xmlrpc_encode_request('wp.editTerm',$params);
    }elseif($_POST['taxonomy'] && $_POST['action']=="wp.getTerms"){
            $taxonomy=$_POST['taxonomy'];
            $params = array($blogid,$username,$password,$taxonomy);
            $request = xmlrpc_encode_request('wp.getTerms',$params);
    }elseif($_POST['taxonomy'] && $_POST['termid'] && $_POST['action']=="wp.deleteTerm"){
            $termid=$_POST['termid'];
            $taxonomy=$_POST['taxonomy'];
            $params = array($blogid,$username,$password,$taxonomy, $termid);
            $request = xmlrpc_encode_request('wp.deleteTerm',$params);
    }else{
            echo '{"error":"no requests"}';
    }
    
    if($request){
            $result = go($request,$xmlrpcurl);
            $json_result = json_encode($result);
    
            echo $json_result;
    
    }else{
            echo '{"error":"no data"}';
    }
    function go($request,$xmlrpcurl){
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_URL,$xmlrpcurl);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$request );
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            $result = curl_exec($ch);
            if($result){
                    return xmlrpc_decode($result);
            }else{
                    echo '{"error":"no url or data encoded"}';
            }
    }
    
Viewing 1 replies (of 1 total)

The topic ‘Using with WooCommerce REST API’ is closed to new replies.