Woocommerce获得变化的产品价格

时间:2012-09-04 23:43:17

标签: wordpress-plugin woocommerce

我试图在变体下拉列表中显示产品差异价格。 我试图更改默认行为,当您在下拉列表中选择变体时,在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();
}

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

5 个答案:

答案 0 :(得分: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;

}

希望这会有用。

答案 1 :(得分:12)

这可能会帮助你们;在搜索如何对新版WC进行相同操作时:

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

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

答案 2 :(得分: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“; 对此                     AND products.post_parent ='$ product-> id'“;

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

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

答案 3 :(得分: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

}

答案 4 :(得分: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;

}

希望这个能帮助别人。 在您的子主题中添加此功能.php

相关问题