Skip to main content

Developers

Global Functions

With the following function you can check if a product is currently in an Automated Discounts session. This is helpful if you use third party plugins that need to know if a product is currently discounted.

if (function_exists( 'sgadwc_is_product_in_discount_session' )) {

$product_already_discounted = sgadwc_is_product_in_discount_session( $product_id );

// do something
}

sgadwc_get_product_cogs

info

Available since version 1.6.12

Retrieve the cost of goods sold (COGS) for a specific product or variation. This function checks supported COGS plugins (SkyVerge, WPFactory), falls back to postmeta, and respects custom meta keys set via the sgadwc_custom_cogs_meta_key filter. The result can be overridden via the sgadwc_product_cogs filter.

Parameters:

ParameterTypeDescription
$productWC_Product|intA WooCommerce product object or product ID.

Return: float|null — The cost of goods sold, or null if not found.

/wp-content/themes/child-theme/functions.php
if (function_exists( 'sgadwc_get_product_cogs' )) {

// Pass a product ID
$cogs = sgadwc_get_product_cogs( $product_id );

// Or pass a WC_Product object
$cogs = sgadwc_get_product_cogs( $product );

if ( ! is_null( $cogs ) ) {
// Use the COGS value
}
}
tip

For variations, if no COGS is set on the variation itself, the function automatically falls back to the parent product's COGS.

Filters

sgadwc_sanitize_payload_product_id

info

Available since version 1.6.10

Sanitize or transform the payload product ID before validation. Use this filter to handle custom product ID formats from third-party feed plugins that are not natively supported by the plugin.

Parameters:

ParameterTypeDescription
$payload_product_idstringThe product ID from the ad payload (after built-in prefix stripping for woocommerce_gpf_ and gla_).
$current_product_idintThe current WooCommerce product ID being validated against.

Return: string|int — The sanitized product ID (can be a post ID, SKU, or cleaned identifier).

Example 1: Strip a custom prefix and return the SKU

If your feed plugin uses a format like customprefix_ABC123, you can strip the prefix and let the plugin's built-in SKU matching handle the rest:

/wp-content/themes/child-theme/functions.php
add_filter( 'sgadwc_sanitize_payload_product_id', function( $payload_id, $current_id ) {
// Transform customprefix_ABC123 → ABC123 (SKU)
if ( preg_match( '/^customprefix_(.+)$/', $payload_id, $matches ) ) {
return $matches[1];
}
return $payload_id;
}, 10, 2 );

Example 2: Convert custom format to WooCommerce Post ID

If you need to look up the product by SKU and return the actual WooCommerce Post ID:

/wp-content/themes/child-theme/functions.php
add_filter( 'sgadwc_sanitize_payload_product_id', function( $payload_id, $current_id ) {
// Transform customprefix_ABC123 → WooCommerce Post ID
if ( preg_match( '/^customprefix_(.+)$/', $payload_id, $matches ) ) {
$sku = $matches[1];
$product_id = wc_get_product_id_by_sku( $sku );
if ( $product_id ) {
return $product_id;
}
}
return $payload_id;
}, 10, 2 );
tip

Use Example 1 if your SKUs are unique across all products including variations. Use Example 2 if you need more precise matching or if your variations share SKUs with parent products.

sgadwc_custom_cogs_meta_key

info

Available since version 1.6.12

Specify a custom postmeta key for retrieving the cost of goods sold (COGS). Use this filter if your COGS data is stored in a custom meta field that is not natively supported by the plugin.

By default, the plugin checks the following meta keys (in order):

  1. Custom meta key (set via this filter)
  2. _wc_cog_cost (WooCommerce Cost of Goods by SkyVerge)
  3. _alg_wc_cog_cost (Cost of Goods for WooCommerce by WPFactory)

Parameters:

ParameterTypeDescription
$meta_keynullThe custom meta key. Default null.

Return: string|null — The custom postmeta key to use for COGS lookup, or null to skip.

Example: Use a custom COGS meta key

/wp-content/themes/child-theme/functions.php
add_filter( 'sgadwc_custom_cogs_meta_key', function( $meta_key ) {
return '_my_custom_cogs_field';
} );

sgadwc_product_cogs

info

Available since version 1.6.12

Override or modify the resolved COGS value for a product. This filter is applied after all COGS retrieval logic (plugin APIs and postmeta lookups) has completed, so you receive the final resolved value and can adjust or replace it.

Parameters:

ParameterTypeDescription
$cogsfloat|nullThe resolved COGS value, or null if no COGS was found.
$productWC_ProductThe WooCommerce product object.

Return: float|null — The COGS value to use.

Example: Provide COGS from a custom source

/wp-content/themes/child-theme/functions.php
add_filter( 'sgadwc_product_cogs', function( $cogs, $product ) {
// If no COGS was found by the plugin, try our custom source
if ( is_null( $cogs ) ) {
$custom_cogs = get_post_meta( $product->get_id(), '_my_erp_cost_price', true );
if ( ! empty( $custom_cogs ) ) {
return (float) $custom_cogs;
}
}
return $cogs;
}, 10, 2 );

Boost your sales with Google Shopping ads. Allow Google to set your discount to secure the sale and optimize your profit margin.