Problems, need help? Have a tip or advice? Post it here.
2 posts Page 1 of 1
Setting variants through the default Couch does not seem to have provision to manage stock. I am setting up a shop with limited stock in available sizes.

I need to be able to display this type of thing:
T-Shirt: Stock: Small [2] Medium [4] Large [0] XLarge [2]
The corresponding drop down would either display large with 0 or not display it at all.
…and when someone buys a small t-shirt the available qty would be reduced to 1 etc etc

Does that make sense?
Has anyone done this with COUCH?
Hi @PixelAce!

As of now, Couch has no native stock management system, but you could scaffold your own using clonable templates and either Couch’s PayPal integration or other third-party payment provider webhooks.

If you’re using Couch’s PayPal integration, you can persist inventory adjustments within <cms:paypal_processor> upon a successful payment. You could also use a third party payment provider like SnipCart or Stripe Checkout, but you’d need to set up your own webhooks to listen for payment confirmation. <cms:content_type ‘application/json’ /> and Couch arrays will come in handy if you go down that route.

A simple way to adjust inventory via PayPal IPN might go like this:

PayPal processor page:
Code: Select all

<cms:paypal_processor>

  <cms:if k_paypal_success>

    <cms:pages masterpage=‘products.php’ id=pp_item_number>
     
      <cms:db_persist
        _masterpage = ‘products.php’
        _mode = ‘edit’
        _page_id = k_page_id
        inventory = “<cms:sub inventory pp_quantity />”
      >
        <cms:if k_success>
          // successful inventory adjustment, notification, log file, etc.
        </cms:if>
      </cms:db_persist>

      // successful payment receipt, send customer email, log file, etc.

    </cms:pages>

  <cms:else_if k_paypal_error />

    // handle PayPal error, send email notification, log file, etc.

  </cms:if>

</cms:paypal_processor>


As far as displaying the product inventory, assuming each product has its own record, you could apply logic like the following (minimal for brevity’s sake):

Code: Select all

<cms:pages masterpage=‘products.php’>

  <div class=‘product’>
    <cms:show k_page_title /> (<cms:show inventory /> remaining)
  </div>

</cms:pages>


Now... technically this runs the risk of two or more people checking out with the same item around the same, and then paying for it and running the inventory below 0 (since the logic for decreasing the inventory happens after the payment confirmation, which could take place several minutes after the customer leave Couch to pay via PayPal), which may or may not be a problem for your application.

To mitigate this, you could use Stripe Elements to begin a payment intent right there on the page, run some logic to check inventory, and - if good - process the payment and persist inventory changes all in one go (no webhooks, PayPal IPNs, payment status limbo, etc). You could do this within a <cms:php> tag. Otherwise, you’d need to keep track of “shelves”, “active cart sessions”, sweeping “abandoned carts” back onto the shelves, etc - a lot of work, but maybe useful in some circumstances.

The nice thing is Couch is flexible enough to handle any of these options; you just need to decide which one best fits your needs.

Hope that helps.

Matthew
2 posts Page 1 of 1