WooCommerce Per Product Shipping Price Without Needing Extension or Plugin

I’m a big fan of WooCommerce, seriously.  I hate how much they nickel and dime for small features like this though.  It’s easier for me to move past it because I’m able to do a lot of tweaking in the code with hooks to get what I need done thereby saving a few dollars.  But sometimes, like this example, you can do it with no coding at all.

The shipping system in general can be pretty confusing so I’m going to walk you through how you can have custom, flat rate shipping prices for different products.

Shipping Zones & Methods

Under WooCommerce Settings click the Shipping tab.  Here you need to add a new zone.  For my example I call it US Flat Rate with a Flat Rate shipping method.

Now the confusing interface part.  In order to set details for flat rate you need to click on the words “Flat Rate” in your newly saved Shipping Zone row.  Let’s not do that yet though, click over to “Shipping Classes” option on this page.

Shipping Classes

This is where we will create our per item shipping prices.  Let’s say we have three products: T-Shirt, Mug and Hat.  We are going to create three shipping classes.

t-shirt-row

Now repeat for the other two items.

Back to Shipping Zones

Bounce back over to shipping zones, click on the “Flat Rate” link on your new shipping class row and let’s do some pricing.

Set the main cost to $0.00 then for each shipping class add the price for that item.  It’s that simple.  Go to your T-Shirt product and under shipping select the T-Shirt class.  Now it will  have that flat price added to the product for shipping on checkout.

Remove Item from PayPal Invoice with the REST API

I’m building a pretty crazy system right now around the PayPal Invoice REST API.  I want the owners of the site to be able to adjust all sorts of stuff on invoices whether the status is DRAFT or SENT.  If PAID, no go.  I’m going to give them the option to copy and cancel the original if that’s possible but I haven’t dove that far in yet.

But what I did need at this point was for them to allow users and admins to adjust an invoice by removing items.  So I google searched without much luck then finally looked through the API’s code to find what I needed.

PayPal Invoice Class

Inside the class for Invoice.php is a method removeItem().  Well, I tried this and kept getting a 400 error with no description of why it wouldn’t work.  I was using a foreach loop.

foreach ( $invoice->items as $item ) {
    if( blah blah ) {
        $invoice->removeItem( $item );
    }
}

This was just not working and there were no clues as to why.  I tried adding a count variable $cnt and using removeItem( $invoice->items[ $cnt ] ) but that was producing the same error.

Adjust The Quantity

So I finally settled on just changing the quantity to 0 and, low and behold, that worked just fine.  So end of day, the line item will still be there but you can set the quantity to 0 and it will charge nothing for it.  Final code was basically:

foreach ( $invoice->items as $item ) {
   if ( strpos( $item->name, 'Item ' . $id . ':' ) !== false ) {
      $item->setQuantity( 0 );
      $invoice->update( $api_context );
   }
}

WooCommerce Subscriptions Limit Total Quantity of Subscriptions or Payment Plans in Cart

We use WooCommerce Subscriptions to setup payment plans for our products.  Deposits was our first choice but it only sent “Invoices” for future payments and did not auto charge the credit card used at initial purchase.  Not ok as our products are very expensive and people tended to ignore those invoices.

We also need to limit these purchase to one per customer.  Easier said than done.  I found a WooCommerce extension that would do the trick but we already pay so much a year for extensions we need I try to avoid that for simple functionality.

Limit Subscription

This won’t work for my needs.  The option on a subscription product only limits users from having multiple subscriptions to that particular product.  We only want one subscript for ANY product.

Solution Limits

My solution works for one shopping cart session.  I’m working on preventing others which shouldn’t be too hard by just checking the database at checkout time for active subscriptions to that user.

The Hook & Function

I went with woocommerce_add_to_cart_validation after going through quite a few other hooks and filters.  The hardest thing about finding a solution is finding what all hooks and filters are available.  Takes some serious google-fu to find the right one quickly.

This filter allows you to run a function before an item is validated and sent to the cart.  We tell our add_filter that we need two of the parameters so we can get the product id as well as the validity of the admission.  This is done with the 4th parameter we set to “2.”

Detect Variations Too

If you are using variable Subscriptions you will need to plan ahead that some items in the cart may be variations.  These have a separate class.  We must first detect if the product being added is an instance of WC_Product_Subscription which is a “Simple Subscription” in WooCommerce.  Or is an instance of WC_Product_Variable_Subscription which is like it says a version of a subscription product with variations.

The odd part that caught me off guard is when we are checking items already in the cart.  We check for WC_Product_Subscription like normal but once in the cart the product is of the WC_Product_Subscription_Variation class meaning it’s just a variation of a product and not the full product variable class.

Multiple Overlapping Layers of Transparent PNG Images Using AbsoluteLayout in NativeScript

Say you have an image of a face in your app.  There are multiple quadrants you want a user to be able to select.  When they select, it should highlight that area.  You want the user to have the ability to select any combination of these quadrants.

Stacking Images In AbsoluteLayout

Let’s go over the xml layout first.  I accomplished this task using an AbsoluteLayout with four Image views inside it like so.

Notice I set the width to 100% but not height. In my full code the height is set in a GridLayout for row 3, but you can set height to 100% or a pixel amount.

I placed a plain face photo as the background for the AbsoluteLayout.  Then I have four images for different quadrants of the face.  These have their absolute position set to 0px top and 0px left.  My overlaying images are the exact same size as the background image of my AbsoluteLayout so I want them to fit exactly over top.

I set the width to the parent element width, this way I know it will translate to different screen sizes.  I get the parent width in my javascript file then set it using this variable parentWidth.

Each image can be turned on or off by setting the visibility to collapsed or visible.  This is controlled in the javascript by the binding object along with the width.  See below:

When the page is loaded I set my initial variables in pageData then turn that Observable into the binding data for the page.  So initially all images will be collapsed (turned off).

Gestures

Next I set gesture detection to my AbsoluteLayout that has been given the id “main-layout” and attached to the variable “quad.” I measure where the touch took place then if it’s in an images quadrant it toggles the collapsed/visible state.  The images are all positioned absolutely so they layer on top of each other.

 

SiteGround Is Fucking Awesome

Here’s my first affiliate linking post thingy ever.  I want you to sign up for hosting with SiteGround and I want you to do it through my link so I get some referral money: https://www.siteground.com/index.htm?afcode=29a69d074161ca9f9de2a9796e5229a8

Now, let me tell you why I love these guys.

SSL Through Let’s Encryptlets-encrypt-siteground-one-click

I found out while poking around in my admin that I can install a FREE Let’s Encrypt SSL certificate with a click of a button.  Go to cPanel, scroll down to the Security section and hit the Let’s Encrypt icon.  Fuck yea.

WordPress Friendly

SiteGround is setup to work great with WordPress.  It has easy installs and is configured perfectly with all correct file permissions and so on.  I had five sites on my own linode.com server slice and they were constantly getting hacked.  I moved them over even with some shitty hacked up themes & plugins on a couple of them.  Haven’t been hacked a single time.

Support

I had one issue with support that wasn’t very pleasant.  But when I replied with my frustration it was escalated to a higher support person and handled immediately and gracefully.

Easy Upgrades

I have five accounts with SiteGround for various clients and myself.  My primary job’s website had a huge surge in traffic.  Expanding memory temporarily was an easy click, done.

Beyond all these perks, the sites are fast and always up.  I’ve had 0 down time.  I can’t say enough about finally finding a host I’m going to stick with for everything.

BUY SITEGROUND HOSTING THROUGH MY AFFILIATE LINK 🙂

WooCommerce Bulk Past Due Invoice Reminders

Once WooCommerce sends an invoice there’s no real way to send out mass invoice reminders unless you do them one at a time.  I needed something better than that.

Woo Deposits & Follow Up Emails

My company needed a way to have people pay for our more expensive products over four months.  I saw Woo Deposits had a payment plan capability so we bought it, installed it and I setup the plans.   I screwed up though.

It’s not clear unless you carefully read through the whole plugin description/FAQ that it invoices for each payment and does not do recurring charges.  I should have noticed this and made sure but I didn’t.

Well, we realized a couple months after turning on and we had around 50 unpaid invoices.  I installed Follow Up Emails and sadly it doesn’t have a way to invoice pending payments.

Some Code To Send Reminders

So here’s a function  you can run to resend the invoice to anyone who is pending payment.  I have the timestamp set to send 7 days after missed payment.

You won’t want to run this all the time so maybe set to a cron job every 7 days or so.