// Utility function: Get instock variations IDs linked to the variable product
function get_instock_variation_ids( int $parent_id ) {
global $wpdb;
return $wpdb->get_col("
SELECT pm.post_id FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
WHERE p.post_type = 'product_variation' AND p.post_status = 'publish'
AND p.post_parent = {$parent_id} AND pm.meta_key = '_stock_status' AND pm.meta_value = 'instock'
");
}
add_action( 'template_redirect', 'set_default_variation_job2' );
function set_default_variation_job2() {
if ( ! is_product() || is_admin() ) return;
$product_id = get_the_ID();
$product = wc_get_product($product_id); // Get the variable product object
if( ! is_a( $product, 'WC_Product' ) return; // Exit if is not a product object
// Only for variable products & Check if default variation has been updated
if ( $product->is_type( 'variable' ) && ! $product->get_meta('_default_variation_updated') ) {
$prices = $product->get_variation_prices(); // Get all variation prices
$reg_prices = $prices['regular_price']; // Variations IDs with reg prices
$sale_prices = $prices['sale_price']; // Variations IDs with sale prices
$instock_ids = get_instock_variation_ids($product->get_id()); // Get instock variations IDs
$percentages = array(); // Initializing variable
// 1st Loop: Loop through variation prices
foreach ( $sale_prices as $id => $sale_price ) {
if( ! in_array($id, $instock_ids) ) {
unset($sale_prices[$id], $reg_prices[$id]); // Remove variations ID / price that aren't instock
} elseif ( $reg_prices[$id] === $sale_prices[$id] ) {
unset($sale_prices[$id]); // Remove variations ID not on sale from "sales prices" array
}
}
// 2nd Loop: Loop through variation sale prices
foreach ( $sale_prices as $id => $sale_price ) {
// calculate the saving percentage
$percentages[$id] = ( $reg_prices[$id] - $sale_prices[$id] ) / $reg_prices[$id] * 100;
}
asort($percentages); // Sort array values in ascending order
$percentages_ids = array_keys($percentages); // Get the variation Ids from the percentages
$reg_prices_ids = array_keys($reg_prices); // Get the variation Ids from the prices
// Get the variation ID with the highest saving percentage
// If no variation on sale, get the variation with hightest price
$variation_id = count($percentages_ids) > 0 ? end($percentages_ids) : end($reg_prices);
if ( $variation_id > 0 ) {
$variation = wc_get_product( $variation_id ); // Get an instance of the WC_Product_Variation object
$default_attributes = $variation->get_variation_attributes(false); // Get formatted variation attributes
// Set the default variation attributes in the Variable product
$product->set_default_attributes( $default_attributes );
// Set a meta data flag to avoid update repetitions
$product->update_meta_data('_default_variation_updated', '1');
$product->save(); // Save the data
}
}
}
1条答案
按热度按时间yhqotfr81#
在下文中,基于此答案,我们将库存中具有最高折扣百分比的变化设置为默认选定变化(每个可变产品一次)。
作为一个后备,如果没有销售的变化,我们将最高的正常价格(库存)的变化设置为默认值。
代码放在子主题的functions.php文件中(或插件中)。测试和作品。
添加1
再次更新变体产品。
如果您对产品变化进行更改,例如更新价格或库存状态,您可以使用以下功能仅一次。
该函数将在所有地方重置'_default_variation_updated'自定义字段,允许该过程为所有可变产品再次启动一次。
添加以下代码:
代码放在子主题的functions.php文件中(或插件中)。
然后浏览你网站上的一个页面。完成后,将
true
替换为false
,如下所示:添加2
将此进程永久激活。
如果您希望进程始终处于活动状态,只需删除:
和