Woocommerce获得变化的产品价格

chi*_*iii 18 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();
}
Run Code Online (Sandbox Code Playgroud)

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

Rat*_*pps 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;

}
Run Code Online (Sandbox Code Playgroud)

希望这会有用.


num*_*web 12

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

global $woocommerce;
$product_variation = new WC_Product_Variation($_POST['variation_id']);
$regular_price = $product_variation->regular_price;
Run Code Online (Sandbox Code Playgroud)

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

  • 加上我的 1,因为这比接受的答案更清晰(它告诉我我需要在我的答案中的代码中添加“全局 $woocommerce;”,上面),但它不会更能适应未来的发展吗?通过引用一个函数(例如`get_price()`)而不是一个字段(在你的例子中是`regular_price`)来从`WC_Product_Variation` 对象中获取价格,因为后者在WooCommerce 的未来版本中更有可能改变? (3认同)

ban*_*ing 5

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

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

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

}
Run Code Online (Sandbox Code Playgroud)