Close

October 23, 2018

Fixing Square POS Barcodes and Magento

Integrating Square and Magento usually requires the purchase of an intermediary system for synchronizing order and product level data. The largest issue we have faced is the case when the SKU of a product is not necessarily the same as the UPC barcode for a product. Variations of data and/or missing barcode data on certain products, can force a Magento store to use a different value for SKU numbers than the actual read-out from a UPC tag.

This is unfortunately one of Square’s largest discrepancy’s. While looking at the underlying API code of Square Connect, they actually list a special hidden field called “barcode”, it is unfortunately not used at all when searching for product’s using the Square Apple POS App system. The POS App will only search the POS system by matching up SKU numbers for a particular product, based on what the bar code reader finds. So if your products have a different sku than what your barcode is, it will never show up in the POS system.

Square Connect does have an alternative system to allow for multiple SKU’s for a single product. They call it a “product variation”. Within the Square Admin interface it neatly brings up a single product along with multiple variations of the same product. So you can have a single product with multiple SKU numbers. So in our particular case we could have single product with our custom SKU and another variation product that has the actual barcode sku in it.

Square Connect allows you to do it, however the documentation is a bit unclear. Once you understand their OOP design, it does make sense.

In our example below, we are going to use PHP API to establish our calls and clone our variant product. We did use Github’s C# documentation in several instances to discover the allowable variables in Square’s system.

Setting up API is outside the scope of this article.

1.) Setup and search for our existing product. We do NOT want to duplicate the object if we don’t have to.

<?php
	... setup access tokesn /etcs
      // Configure OAuth2 access token for authorization: oauth2
      SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);
      $api_instance = new SquareConnect\Api\CatalogApi();
      $body = new \SquareConnect\Model\SearchCatalogObjectsRequest(); // \SquareConnect\Model\SearchCatalogObjectsRequest | An object containing the fields to POST for the request.  See the corresponding object definition for field details.
      //we are doing an exact lookup only.  If we don't find the exact sku...skip it.
      $queryText=new \SquareConnect\Model\CatalogQueryExact();
      $queryText->setAttributeName("sku");
      $queryText->setAttributeValue("$sku");
      $query=new \SquareConnect\Model\CatalogQuery();
      $query->setExactQuery($queryText);
      $body->setQuery($query);
      try {
          $result = $api_instance->searchCatalogObjects($body);
          $itemcount=count($result);
          //ONLY EVER DO THIS ON ONE PRODUCT
          	$results=$result->getObjects();
          	if(count($results)==1){
		// Okay perfect.  We have one result.

2.) Go through all iterations and look for our special UPC code. If we find it…re-use the same object and update it with a new barcode (just in case in changed).

<?php
print "GOOD ONLY ONE";
          		$item=$results[0];
              $item_id=$item->getId();
 
              //check our variation data for our barcode.  If exists...update it.
              $vardata=$item->getItemVariationData();
              $varparentid=$vardata->getItemId();
              $meobject = $api_instance->retrieveCatalogObject($varparentid, true);
              $meobject=$meobject->getObject();     
              //go over iterations of parent
              $pits=$meobject->getItemData()->getVariations();
              $FOUND=false;
              foreach($pits as $pit){
                $tvardata=$pit->getItemVariationData();
                //check if we already have our barcode field
                if($tvardata->getName()==UPC_FIELD_NAME){
                  $FOUND=true;
                  print "\nFOUND IT!";
                  break;
                }
              }
              //clone from this item if NOT found.  Otherwise, reuse it
              if($FOUND==false){
                $clone=$item;
                $clone->setId("#TEMP123");
                print " CLONING ITEM...we didn't find match";
              }
              else{
                print " RE-using item.  We found a match";
                $clone=$pit;
              }

2.) Now we do a complicated dance to setup all of the required variables for our product variant. Through a lot of trial and error, we found out the best method for setting up the pricing/inventory as well.

<?php
             $clone->setType("ITEM_VARIATION");
          		$id=$clone->getId();
              $var_data=$clone->getItemVariationData();
              $var_data->setName(UPC_FIELD_NAME);
              $var_data->setSku($barcode);
              $var_data->setPricingType("VARIABLE_PRICING");
              $var_data->setTrackInventory(null);
              $var_data->setLocationOverrides(null); //got to remove location ovverides or it will track.
              $fakemoney= new \SquareConnect\Model\Money(); //dummy money to wipe out existing
          		$var_data->setPriceMoney(null);
              $clone->setItemVariationData($var_data);
 
              $upsert=new \SquareConnect\Model\UpsertCatalogObjectRequest();
              //set unique key
              $upsert->setIdempotencyKey(md5(time()));
              $upsert->setObject($clone);
 
              $update_result=$api_instance->upsertCatalogObject($upsert);

3.) Obviously this should be tested on a single product in Magento and verify that it works. Once the scripts are confirmed working properly with the actual Square POS App, we setup a cron cycle that updates SquareConnect every fifteen minutes with products from Magento that were updated in the past hour. This would be configured differently for sites with a large amount of products and/or products that are constantly being updated.

We are a full service Magento Consulting Shop.  If you would like to speak to us about our PageSpeed consulting services, please don’t hesitate to contact us!