Woocommerce获得变化的产品价格

问题描述 投票:18回答:5

我试图在变化下拉列表中显示产品变化价格。我试图更改默认行为,当您在下拉列表中选择变体时,在div内显示价格。

问题是我无法找到div获得变化价格的地方。我搜索了所有的JavaScript但无法找到它

如果我使用:

add_filter('woocommerce_variation_option_name' ,'add_price_to_dropdown');

function add_price_to_dropdown($name){

    global $product;
    return $name.' '.$product->get_price_html();
}

我只是获得所有选项的最小变化价格。我想得到每个变化的价格。任何线索?

wordpress-plugin woocommerce
5个回答
41
投票

这是您要寻找的代码

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    if ( empty( $term ) ) return $term;
    if ( empty( $product->id ) ) return $term;

    $id = $product->get_id();

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;

    $query = "SELECT postmeta.post_id AS product_id
                FROM {$wpdb->prefix}postmeta AS postmeta
                    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                WHERE postmeta.meta_key LIKE 'attribute_%'
                    AND postmeta.meta_value = '$term_slug'
                    AND products.post_parent = $id";

    $variation_id = $wpdb->get_col( $query );

    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
         $_product = new WC_Product_Variation( $variation_id[0] );
         return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
    }
    return $term;

}

希望这会有用。


12
投票

这可能会帮助你们;在搜索如何使用新版本的WC时:

global $woocommerce;
$product_variation = new WC_Product_Variation($_POST['variation_id']);
$regular_price = $product_variation->regular_price;

我正在使用ajax并使用post方法传递变体的ID。


4
投票

我一直在Woocommerce上使用这个代码来显示我的变化价格,当我更新到Woocommerce 2.0时,我注意到每当我编辑产品时出现在wp-admin / error-log中的数据库错误。我不确定在更新到2.0之前是否也发生了错误,因为我在更新之前很久没有使用Woocommerce,并且我认为直到今天才检查错误日志。

变化价格按预期显示,但每次打开产品进行编辑时,错误日志都会出现以下错误。与我正在编辑的产品相关的每个变体都有错误。

WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8 for query SELECT postmeta.post_id AS product_id
            FROM wp_postmeta AS postmeta
                LEFT JOIN wp_posts AS products ON ( 

products.ID = postmeta.post_id )
            WHERE postmeta.meta_key LIKE 'attribute_%'
                AND postmeta.meta_value = '12'
                AND products.post_parent =  made by 

include('wp-admin/edit-form-advanced.php'), do_meta_boxes, call_user_func, woocommerce_product_data_box, do_action('woocommerce_product_write_panels'), call_user_func_array, variable_product_type_options, include('/plugins/woocommerce/admin/post-types/writepanels/variation-admin-html.php'), apply_filters('woocommerce_variation_option_name'), call_user_func_array, display_price_in_variation_option_name

我在您的代码中更改了以下行:AND products.post_parent = $ product-> id“; to this AND products.post_parent ='$ product-> id'”;

现在,没有更多的错误。错误日志保持良好和空白。

只是想分享以防任何其他人遇到问题。


4
投票

我和OP有完全相同的问题,但我的情况有点不同。这是我的解决方案,可以帮助其他人登陆此页面。

function get_product_variation_price($variation_id) {
    $product = new WC_Product_Variation($variation_id);
    return $product->product_custom_fields['_price'][0];
}

WooCommerce 2.2.10的更新:以上代码不适用于当前版本的WooCommerce。下面的代码可以使用并且值得考虑,因为它使用了WooCommerce API,它避免了手动SQL查询,而且非常简单......

/*
 * You can find the $variation_id in the Product page (go to Product Data > Variations, and it is shown with a preceeding "#" character)
 */
function get_product_variation_price($variation_id) {

    global $woocommerce; // Don't forget this!
    $product = new WC_Product_Variation($variation_id);
    //return $product->product_custom_fields['_price'][0]; // No longer works in new version of WooCommerce
    //return $product->get_price_html(); // Works. Use this if you want the formatted price
    return $product->get_price(); // Works. Use this if you want unformatted price

}

0
投票

我对这个问题的看法,在最新的Woocommerce:3。3。1(2017年10月)价格将显示在下拉列表的变化旁边。

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;


    $query = "SELECT postmeta.post_id AS product_id
                FROM {$wpdb->prefix}postmeta AS postmeta
                    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                WHERE postmeta.meta_key LIKE 'attribute_%'
                    AND postmeta.meta_value = '$term_slug'
                    AND products.post_parent = $product->id";

    $variation_id = $wpdb->get_col( $query );

    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
        $_product = new WC_Product_Variation( $variation_id[0] );
        $_currency = get_woocommerce_currency_symbol();
        return $term . ' ('.$_currency.' '. $_product->get_price()  . ')';
    }
    return $term;

}

希望这个可以帮助别人。在您的子主题functions.php中添加它

© www.soinside.com 2019 - 2024. All rights reserved.