ChannelAdvisor REST API – Create a product

This function creates a product in ChannelAdvisor for a SKU that is in our company’s system. We currently actually only send “-” for the title, but you can send other information in the fields array, including brand, manufacturer, etc. You can see more of them in the documentation.

public function createProduct($profileId, $sku, $title, $refreshToken) {
  $token = $this->authorize($refreshToken);
  if(!is_null($token)) {
    $endpoint = $this->url . 'Products?access_token=' . $token->access_token;
    $headers = [
      'Content-Type: application/json',
    ];
    $fields = [
      "ProfileID" => $profileId,
      "Sku" => $sku,
      "Title" => $title,
    ];
    $json = json_encode($fields, 2);
    // Send the information to CA in a post request.
    $product = $this->postRequestWithJson($endpoint, $headers, $json);

    // If we get the product, return the ID
    if ($product !== FALSE) {
      return $product->ID;
    }
    // Otherwise, return null
    return NULL;
  }
  return NULL;
}

We are usually only creating a few products at one go, so this process works for us. ChannelAdvisor recommends importing large numbers of products via file.

 

Where I began with Laravel

There are some standard places to start learning Laravel.

  • Laracasts has tons of videos about the framework and how to use it.
  • The Laravel documentation is also really good and generally provides examples of practical uses for the various functions.

While I used both of these in my journey into Laravel, I found that the videos could sometimes go too fast, or would discuss topics I wasn’t familiar with or didn’t yet have the vocabulary for, or would present on a slightly different version of the issue than I was struggling with.

My favorite tutorial is one that I went back to over and over to review the basics and make sure I was doing things right. Even now, after working with Laravel for the better part of two years, I still find myself referring to it.

I also spent a lot of time on Stack Overflow and the Laracasts’ forum. The Laracasts’ Slack group also provided a lot of help. These could be intimidating, especially as a newbie programmer. Just the Stack Overflow text on posting a question made me question multiple times if I was doing it wrong. However, the Laravel community is very supportive and encouraging, and I never faced backlash for any question. I love that about this group of people!

One thing asking questions has taught me is that it’s a good way to realize the problem myself. As I try to explain what I’m trying to do vs. what is actually happening, I frequently stumble over the answer.

As time has gone one, I’ve also realized I’m unlikely to be the first person to encounter a particular error or to want to do a particular task. My Google Fu is generally pretty good with just the bare bones of the error message and a rough stab at what I was trying to do. And packages are great! While they run the usual risk of anything open source eventually being abandoned, there are still useful packages with active and interested maintainers. I especially love Spatie’s suite of packages. They also periodically post “easy for new contributors” issues on Twitter for those looking to start contributing to open source software.

When one logout redirect isn’t enough.

Our application has several different “sites,” which need various degrees of customization, although they all use mostly the same flow for our application. One area of customization is the login page. If they’re associated with our generic site, they would go to ourapp.com/generic and see the login. If they just go to ourapp.com, they get a different view. The challenge is that, on logging out, Laravel by default just redirects everybody to the same page.

I found this SO post that was close, but (perhaps because a 5.2 change?) it wasn’t quite working. Instead, this code works for me:

public function logout() {
    $redirect = (Auth::user()->site->loginPage()) ? Auth::user()->site->loginPage() : '/';
    Auth::guard($this->getGuard())->logout();
    return redirect($redirect);
}

I actually had to spend some time building a settings model for site settings, since we hadn’t had to use those before. I’d just built them manually, so today I’m going to work on building out some views so that they can be user-editable.

Always sending a variable to the view

As we’re building our app, we’re trying to make it international. We are storing the user’s language preference in the Session, but we need to have it as a variable in the views. I stumbled upon this SO post with an easy solution.

class BaseController extends Controller
{
  public function __construct()
  {
    $lang = Session::get('lang');
    View::share('lang', $lang);
  }
}

This makes the variable $lang available in our views. Nice and easy. Just remember to add “use View;” and, in our case, “use Session;” to the controller as well.

Password Reset Token in Laravel

My current task is to implement an email to send to users when their account is created manually. We’re using the default Laravel auth set-up, but this is outside of that scope. I mostly wanted to copy the password reset email. I could figure out everything but how to get the password reset token. I both needed to create it and needed to send it to the email that we’re sending to the created users. This link got me close, but they appear to be using an older version of Laravel. (We’re on 5.2.) I did a bit of digging through the folder and found that a couple of things had been renamed. The code to use:

$token = app(‘auth.password.broker’)->createToken($user);

Simple, but it definitely took some digging.

Laravel left outer join – sort of

Earlier this week, I added some functionality to the online game I’m building to have administrators be able to add a toy to a user’s collection. I was feeling proud of myself, until I realized that the script didn’t weed out toys the user already had, which would end up with duplications. So I had to figure out how to do that. Not being exactly sure how to describe what I wanted, I nevertheless entered a rather vague description into Google and hoped for the best.

Fortunately, other people have been trying to do what I was, and I was able to find some answers pretty easily. I was looking for a left outer join, basically, but with a condition. I wrote up some SQL and was able to get it to run in the terminal. It looked like this:

SELECT patterns.id, patterns.name
FROM patterns
LEFT JOIN toys
ON patterns.id = toys.pattern_id AND toys.user_id = 1
WHERE toys.pattern_id IS NULL

Contrary to my expectations, this was actually the easy part. The next challenge was converting it into Laravel. I followed a couple of examples from StackOverflow, but I kept getting an empty result, and I’m still new enough to Laravel that I am not sure how to dig into the query it’s actually generating. I was eventually able to make it work. The code in the pattern model:

public function scopeUnknownPatterns($query, $userid){
    return $query
        ->select('patterns.id','patterns.name')
        ->leftJoin('toys',function($join) use ($userid){
            $join->on('patterns.id','=','toys.pattern_id');
            $join->on('toys.user_id','=',DB::raw($userid));
        })
        ->where('toys.pattern_id','=',NULL)
        ->orderBy('patterns.name','asc');
}

Then, from the toy controller, I just use:

$patterns = Pattern::unknownPatterns($user->id)->get();

I was tripping up on the ->get() part, as the other scopes I’ve put into the model are limited and … for some reason I don’t really get yet don’t need it. One of the challenges of teaching yourself things is when you can’t actually answer your own questions!

Adding resume

I’ve added my resume to my home/about page. This sounds simple, but it actually took a surprising amount of fiddling to make that happen. I decided to go with a WordPress plug-in. First I tried a LinkedIn plug-in, but the instructions on linking to the LinkedIn API weren’t great. So I went with Resume Builder, as recommended in this article.

The various sections titles weren’t particularly intuitive, but I got it figured out with a bit of fiddling. However, somewhere along the line of my theme and child theme and the resume theme, unordered lists lost their bullets. After some digging, banging my head against the wall, and taking a break to chat with my step-dad, I was able to figure out that I needed to put the resume-builder.css file into my parent theme’s directory and add ul elements to most of the description blocks. This isn’t ideal, as I’d rather have it in the child theme, which is, well, the point of child themes. But, short of that, this got it to work. Then, when I decided to change the color of the various section headers, I was actually able to do so easily, because I was more familiar with the CSS style.

Now if only I could figure out why Laravel is telling me this route doesn’t exist, when artisan tells me it does. . .

Redesign

I took a crack at re-designing the website. I built a child theme of the Twentytwelve theme. Strangely, I had to use the older method of importing the parent theme, rather than the newer method described on the WordPress website. In my search for help, I found other people that had the same problem. It’s always comforting to learn it isn’t just me.

I also messed around with the GIMP for a couple of hours to develop a new header image. It’s been a while since I did much with the GIMP. I’d wanted to use some sort of lens flare or lighting effect, but I wasn’t able to get it to look how I wanted it to. So, I fiddled around with a bunch of other options. I quite like the final result. Often images I mess around with end up feeling either under- or over-fiddled with, but, as I said, it’s been a while. Maybe my eye is improving!

Trucking along

Things are going well in “becoming a web developer” land.

I finished the conversion of my last class’s project into Laravel and even added some user authentication, which Laravel made quite easy. It doesn’t come natively with the ability to assign multiple roles, so I just did what a logged in person could see vs what an unregistered user would see. It looks like there are several packages already written that provide for user groups and specific permissions, so I will likely play around with those in supporting my next project, which will also be in Laravel.

My final class has started, so I’m going through the materials for the week, which focus on the need for a professional portfolio. It’s interesting reading and makes me think about how I might change things up on this site to be a better reflection of my professional brand.

Today is the last day of the Introduction to iOS programming class. It’s been a good way to get my feet wet on the app programming side. It, as well as the Laravel dive, have highlighted that an important aspect of learning a new skill is knowing what to pay attention to and focus on now and what to put on a shelf to revisit once the basics are under your belt. (Mixing metaphors is fun!) It’s also important for instructors to know when to say “That’s a good question but outside of the scope of this lesson.” I’ve been reflecting on some of the training I have done for others and recalling cases where I was ill-prepared or where I went deeply into a side topic that I didn’t need to. A needed dose of humility, I think. 🙂

This is better — for now

I am getting firmly wrapped up into the Laravel camp. After some fiddling around with it, not making much progress beyond a tutorial or two, I had the bright idea to try coding out my last class’s project into this framework. That limits the unknowns to the framework. It seems to be working out quite well. Not that there haven’t been rabbit holes and dead ends, but I feel like the periods of confusion and being lost are either getting fewer or are shortening, as I’m able to find the solution more easily. Although today I got my best help from my personal guru–my mom. Harvard graduates are good sources of information. 🙂

My final class starts up on Monday, so hopefully I can get everything done with this that I want. In the meantime, I read over the syllabus for the course and have some concerns. And confusions. No response to my questions yet. I have a lot of thoughts and vague notions about the challenges of teaching and learning web programming (and programming in general). And about teaching and education in general. But I don’t know what I’d do about those thoughts. It’s maybe mostly just something I’ll keep in mind as I seek additional educational opportunities.

My sleep schedule is getting all messed up, though. Was tired and migraine-y all day, but now I’m awake at 1:30am. Insomnia is boring, if anybody ever asks you.