Auto Pricing Min Price
Available since version 1.0.11
The plugin offers a way to set the Auto Pricing Min Price on each product. The field can then be ingested by a feed plugin and output into the Google Merchant Center feed.
The main settings are:
- A settings field for saving an Auto Pricing Min Price manually on each product.
- For automatic calculation and batch processing, the plugin offers a filter that can be used to calculate an Auto Pricing Min Price.
- The automatic calculation happens each time a product is updated manually. But, if a shop manager chooses to upload product updates with an import plugin, the automatic calculation may not be triggered. In that case, the batch regeneration on all products of the Auto Pricing Min Price can be triggered manually or by activating a nightly, recurring batch process.
Manual Setting
Once enabled, you will see a new field in the pricing section of each product where you can set your Auto Pricing Min Price.
The manually set prices are saved in a meta field with the key _google_auto_min_price_man
. This is the field you can use in your feed plugin to retrieve the manually set prices.
Automatic Calculation
When you have dozens, hundreds, or thousands of products, automatically calculating the Auto Pricing Min Price may be more convenient.
For this case, the plugin offers a filter that can be used to calculate the Auto Pricing Min Price with any type of rule that you can imagine.
The automatic calculation is always triggered on a specific product when you manually update the product through the backend. However, if you upload product updates, such as prices, through some sort of import, the automatic calculation is not triggered. In that case, have a look at the scheduled and manual batch update features.
The automatically set prices are saved in a meta field with the key _google_auto_min_price_calc
. This is the field you can use in your feed plugin to retrieve the automatically set prices. If a manually set field is present for a particular product, it will override the calculation and be saved in the same _google_auto_min_price_calc
meta field. So you can use this field to retrieve all prices for your feed, the calculated prices and manual overrides.
Following, we provide several examples for the Auto Pricing Min Price calculation filter:
Example: How the filter works
/**
* The filter provides a default value, which is null, and the product object.
*
* As output you need to return either a int or float value, or null if no calculated price can be determined.
*
* This example subtracts 1 from the regular price and returns that as new calculated value.
**/
add_filter( 'sgadwc_google_auto_pricing_min_price_calculation', function ( $value, $product ) {
$regular_price = $product->get_regular_price();
return wc_format_decimal( $regular_price - 1, 2 );
}, 10, 2 );
Example: Return a value that is lowered by a specific percentage of the profit margin
/**
* This example takes the regular price and the COGS price that was saved by a different plugin in the
* meta key _alg_wc_cog_cost.
*
* If no value is available in the _alg_wc_cog_cost meta key, null is returned.
*
* It then calculates an Auto Pricing Min Price that reduces the regular price by 20% of the profit margin.
**/
add_filter( 'sgadwc_google_auto_pricing_min_price_calculation', function ( $value, $product ) {
$regular_price = $product->get_regular_price();
$cogs = $product->get_meta( '_alg_wc_cog_cost' );
// Safeguard in case no COGS is set for this product
if (empty( $cogs )) {
return null;
}
$margin = $regular_price - $cogs;
return wc_format_decimal( $regular_price - ( $margin * 0.2 ), 2 );
}, 10, 2 );
Example: Reduce the regular price to a fixed percent
/**
* This example takes the regular price and lowers it to a fixed percentage.
**/
add_filter( 'sgadwc_google_auto_pricing_min_price_calculation', function ( $value, $product ) {
$regular_price = $product->get_regular_price();
return wc_format_decimal( $regular_price * 0.8, 2 );
}, 10, 2 );
Example: Add a fixed amount to the COGS
/**
* This example takes COGS and adds a fixed amount to it.
**/
add_filter( 'sgadwc_google_auto_pricing_min_price_calculation', function ( $value, $product ) {
$cogs = $product->get_meta( '_alg_wc_cog_cost' );
// Safeguard in case no COGS is set for this product
if (empty( $cogs )) {
return null;
}
return wc_format_decimal( $cogs + 4.5, 2 );
}, 10, 2 );
Example: This is a complex example from a real webshop
/**
* This filter uses plenty of log outputs to help determine issues
* with calculations on products that are missing information.
**/
add_filter('sgadwc_google_auto_pricing_min_price_calculation', function ($value, $product) {
$logger_source = 'sgadwc-auto-min-price-calculation';
// If function get_product_condition does not exist, define it
if (!function_exists('get_product_condition')) {
function get_product_condition($product) {
$attributes = $product->get_attributes();
if (!isset($attributes['condition'])) {
return null;
}
if ($attributes['condition'] == 'New') {
return 'A';
}
if ($attributes['condition'] == 'Used') {
return 'U';
}
return null;
}
}
// Function to calculate the minimum price for a product
if (!function_exists('get_auto_min_price')) {
function get_auto_min_price($condition, $margin_percent, $regular_price) {
if ($condition == 'A') {
if ($margin_percent > 0.3) {
return $regular_price * 0.8;
} else {
return $regular_price * 0.85;
}
}
if ($condition == 'U') {
if ($margin_percent > 0.3) {
return $regular_price * 0.75;
} else {
return $regular_price * 0.85;
}
}
return null;
}
}
// Function to get the Cost of Goods Sold set by two different plugins
if (!function_exists('get_cogs')) {
function get_cogs($product) {
$cogs = $product->get_meta('_wc_cog_cost');
if (!empty($cogs)) {
return $cogs;
}
$cogs = $product->get_meta('_alg_wc_cog_cost');
if (!empty($cogs)) {
return $cogs;
}
return null;
}
}
// Get the COGS for this product
$cogs = get_cogs($product);
// Only continue to process if cost of goods sold is set, otherwise abort
if (empty($cogs)) {
wc_get_logger()->notice(
'COGS is empty or not defined for the product ' . $product->get_id(),
['source' => $logger_source]
);
return null;
}
// Get the regular price for the product
$regular_price = $product->get_regular_price();
// If no regular price is set, abort
if (empty($regular_price)) {
wc_get_logger()->notice(
'Regular price is empty for the product ' . $product->get_id(),
['source' => $logger_source]
);
return null;
}
// Calculate the margin on this product
$margin = $regular_price - $cogs;
// Calculate the margin percentage on this product
$margin_percent = $margin / $regular_price;
// If the margin is below 15%, abort
if ($margin_percent < 0.15) {
wc_get_logger()->notice(
'The margin is below 15% for the product ' . $product->get_id(),
['source' => $logger_source]
);
return null;
}
// Get the condition of the product
$condition = get_product_condition($product);
// If the condition is null, abort
if ($condition == null) {
$message = '';
// If the product is a variation, get the parent ID
if ($product->is_type('variation')) {
$parent_id = $product->get_parent_id();
$message = 'Condition is not defined for the variation ' . $product->get_id() . ' of the product ' . $parent_id;
} else {
$message = 'Condition is not defined for the product ' . $product->get_id();
}
wc_get_logger()->notice(
$message,
['source' => $logger_source]
);
return null;
}
// Calculate the auto min price
$auto_min_price = get_auto_min_price($condition, $margin_percent, $regular_price);
// If the auto min price is null, abort
if ($auto_min_price == null) {
wc_get_logger()->notice(
'Auto Min Price could not be calculated for product ' . $product->get_id(),
['source' => $logger_source]
);
return null;
}
// Always return a float with 2 decimals
return wc_format_decimal($auto_min_price, 2);
}, 10, 2);
Scheduled Regeneration
Once activated the plugin will run a batch update of the calculated Auto Pricing Min Price at 3:25 am (local time) every morning.
It requires the automatic calculation filter to be set.
The process is very resource efficient and reliable. It can run through thousands of products quickly while limiting memory use to 90% during execution.
Tests have shown that on an average server, it can process 10'000 products within 15 minutes.
Instant Regeneration
You can trigger the batch regeneration of all products anytime you need to. This may be convenient after you've done a manual import and update of product prices during the day.