将Woocommerce可变产品中的最高价格变化设置为默认值 [英] Set the highest priced variation as default in Woocommerce variable products

查看:196
本文介绍了将Woocommerce可变产品中的最高价格变化设置为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在woocommerce安装中有很多变化的产品. 当用户浏览到任何产品页面时,都不会默认选择任何一个.

I have a lots of products with variations in my woocommerce installation. None of them is preselected as default when user browses to any product page.

手动选择它们将是一项繁琐的工作.此外,每天都会使用SQL脚本自动导入新产品和新版本.

Manually, selecting them would be a tedious job. Additionally, new products and variations are imported automatically on daily basis using SQL script.

我想为浏览到具有可变产品的任何产品页面的所有用户预先选择价格最高的产品变体.

I would like to have preselected product variation with highest price for any user that browses to any product page which has variable product.

该怎么做?

推荐答案

在这里,这是一个自动化解决方案,每次客户调用/浏览可变产品时,该解决方案便会发挥作用.

Here it is an automated solution, that will do the job each time a variable product is called/browse by a customer.

可变产品将使用具有最高价格的默认版本进行更新.

The variable product will be updated with a default variation that has the highest price.

仅当我每次更改变量产品时设置自定义帖子元键/值时,此操作才会执行一次.

This will be done once only as I set a custom post meta key/value each time a variable product has been updated.

代码:

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文件中.

经过测试,可以正常工作.

Tested and works.

这篇关于将Woocommerce可变产品中的最高价格变化设置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆