Custom products in WooCommerce and price calculations

Once in a while I got a request from a client who wants to sell products based on a calculation. For example pricing tables for curtains or holiday homes with different prices at different arrival dates with multiple durations.

Of course there are different modules solving all kind of situations, but you will always see the current case is always a little bit different..

For this reason I wrote a dynamic pricing skeleton-module. This module exists out of roughly three parts,

 

The module contains the following files,
price-calculator/price-calculator.php
price-calculator/hooks-frontend-product.php
price-calculator/hooks-admin-product.php
price-calculator/class-my-product-factory.php
price-calculator/class-product-calculate.php

Download price-calculator.zip for the example code



price-calculator/price-calculator.php
This file loads all the hook, class files and contains the module properties.


price-calculator/hooks-admin-product.php
This file contains the hooks for the admin. A new product_type is added by using the ‘product_type_selector’ filter. The ‘woocommerce_product_data_panels’ action is used to add a new panel for configuring the specific product settings. The ‘save_post’ action is used to save the settings.




price-calculator/hooks-frontend-product.php
This file contains the hook for the add-to-cart template on the product page. Because the dynamic price also needs dynamic input for price calculation, it has a custom add-to-cart template.

It also contains a hook that adds the product to the shopping cart. In practice, when the user adds a dynamic product to it’s shoppingcart, a new child-product is created with the selected price and properties. This might create a lot of junk in the database if you got a high-traffic site and may be implemented in another way. Feedback at the frontend that shows the price based on the given user input is done with javascript.





price-calculator/class-product-calculate.php

This file contains a My_Product_Calculate-class that extends the default WC_Product_Simple, for overriding the get_price_html() function.

price-calculator/class-my-product-factory.php
The default product factory, only creates WooCommerce product-classes. The ‘My_Product_Factory’ extends the default WC_Product_Factory and overrides the get_product() function. It checks if the requested product is of the new type My_Product_Calculate If that's the case it instantiates the newly created product-type, else it falls back to the default behavior.

 

 

 

 



Gerelateerd

27-09-2016 Migrating from Php 5 to Php 7
- Bent u opzoek naar een Php of Java programmeur voor uw website of applicatie? (freelance / detachering)
- Heeft u alleen enkele tickets, of gehele projecten in de planning?

Kijk hier voor meer informatie of neem contact op.
Sitemap | Op onze producten & diensten zijn onze algemene voorwaarden van toepassing