将最高价格的变化设置为 Woocommerce 可变产品中的默认值
Set the highest priced variation as default in Woocommerce variable products
我的 woocommerce 安装有很多不同的产品。
当用户浏览任何产品页面时,其中 None 个被预选为默认值。
手动选择它们将是一项乏味的工作。此外,每天都会使用 SQL 脚本自动导入新产品和变体。
我想为浏览到具有可变产品的任何产品页面的任何用户预选价格最高的产品变体。
怎么做?
这是一个自动化解决方案,每当客户 called/browse 可变产品时,它就会完成这项工作。
可变产品将更新为具有最高价格的默认变体。
每次更新可变产品时,我都会设置自定义 post 元 key/value,这只会执行一次。
代码:
add_action( 'template_redirect', 'set_default_variation_job' );
function set_default_variation_job() {
if ( ! is_product() || is_admin() ) return;
global $wpdb, $post;
// Get an instance of the WC_Product object
$product = wc_get_product($post->ID);
$product_id = $post->ID;
// Check if default variation has been updated, if yes we exit
if( get_post_meta( $product_id, '_default_variation_updated', true ) ) return;
// Only for variable products
if ( $product->is_type( 'variable' ) ) {
$max_price = $product->get_variation_price('max', true); // Get Max variation price
// SQL Query: Get the variation ID of the max variation price
$result = $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 LIKE 'product_variation' AND p.post_parent = $product_id
AND pm.meta_key LIKE '_price' and pm.meta_value = $max_price
");
$variation_id= reset($result); // get the first variation ID as a string
// Get an instance of the WC_Product_Variation object
$variation = wc_get_product($variation_id);
$default_attributes = array();
// Loop through this variation attributes and format them
foreach( $variation->get_variation_attributes() as $key => $value ){
$taxonomy = str_replace( 'attribute_', '', $key );
$default_attributes[ $taxonomy ] = $value;
}
// Set the default variation attributes in the Variable product
$product->set_default_attributes( $default_attributes );
// Set a meta data flag to avoid update repetitions
update_post_meta( $product_id, '_default_variation_updated', '1' );
$product->save(); // Save the data
}
}
代码进入您的活动子主题(活动主题)的 function.php 文件。
已测试并有效。
转到文件 woocommerce/templates/single-product/add-to-cart/variable.php
并添加以下行:
'selected' => $options[0],
到函数 wc_dropdown_variation_attribute_options()
。
这将预先 select 产品展示页面上的第一个变体。
我的 woocommerce 安装有很多不同的产品。 当用户浏览任何产品页面时,其中 None 个被预选为默认值。
手动选择它们将是一项乏味的工作。此外,每天都会使用 SQL 脚本自动导入新产品和变体。
我想为浏览到具有可变产品的任何产品页面的任何用户预选价格最高的产品变体。
怎么做?
这是一个自动化解决方案,每当客户 called/browse 可变产品时,它就会完成这项工作。
可变产品将更新为具有最高价格的默认变体。
每次更新可变产品时,我都会设置自定义 post 元 key/value,这只会执行一次。
代码:
add_action( 'template_redirect', 'set_default_variation_job' );
function set_default_variation_job() {
if ( ! is_product() || is_admin() ) return;
global $wpdb, $post;
// Get an instance of the WC_Product object
$product = wc_get_product($post->ID);
$product_id = $post->ID;
// Check if default variation has been updated, if yes we exit
if( get_post_meta( $product_id, '_default_variation_updated', true ) ) return;
// Only for variable products
if ( $product->is_type( 'variable' ) ) {
$max_price = $product->get_variation_price('max', true); // Get Max variation price
// SQL Query: Get the variation ID of the max variation price
$result = $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 LIKE 'product_variation' AND p.post_parent = $product_id
AND pm.meta_key LIKE '_price' and pm.meta_value = $max_price
");
$variation_id= reset($result); // get the first variation ID as a string
// Get an instance of the WC_Product_Variation object
$variation = wc_get_product($variation_id);
$default_attributes = array();
// Loop through this variation attributes and format them
foreach( $variation->get_variation_attributes() as $key => $value ){
$taxonomy = str_replace( 'attribute_', '', $key );
$default_attributes[ $taxonomy ] = $value;
}
// Set the default variation attributes in the Variable product
$product->set_default_attributes( $default_attributes );
// Set a meta data flag to avoid update repetitions
update_post_meta( $product_id, '_default_variation_updated', '1' );
$product->save(); // Save the data
}
}
代码进入您的活动子主题(活动主题)的 function.php 文件。
已测试并有效。
转到文件 woocommerce/templates/single-product/add-to-cart/variable.php
并添加以下行:
'selected' => $options[0],
到函数 wc_dropdown_variation_attribute_options()
。
这将预先 select 产品展示页面上的第一个变体。