如何获取raw_woocommerce_price钩子中的当前变化数据


How to get current variation data inside raw_woocommerce_price hook?

我想在raw_woocommerce_price钩子中获取当前的变化数据。

function filter_raw_woocommerce_price( $price_1 ) {
    global $product;
    // Some custom code to change price by variation factor
    $variation_id = product->Something_I_Need_To_Know_To_Get_Current_Variation();
    // bla bla bla
    $factor = PutSomeCustomCalculationHere($variation_id);
    $price_1 = $price_1 * $factor;
    return $price_1;
};
add_filter( 'raw_woocommerce_price', 'filter_raw_woocommerce_price', 10, 1 );

我怎样才能做到这一点?

感谢

使用此代码,如果产品是ok类型,则使用您的自定义逻辑,如下所示:

add_filter( 'raw_woocommerce_price', array( $this, 'asdfasdadf' ) );
function asdfasdadf($price){
    global $product;
    // check if that is var product
    if( ! $product->is_type( 'variable' ) ) return $price;
    // get variable data!
    var_dump( $product->get_attributes() ); exit;
}

'raw_woocommerce_price'不是我需要的钩子。

取而代之的是使用这些钩子。

为什么?因为这些钩子将产品的实例作为第二个参数。在那里,它得到了我需要进一步了解的所有信息。

add_filter('woocommerce_get_price', 'return_custom_price', $product, 10, 2 );
add_filter('woocommerce_get_regular_price', 'return_custom_price', 10, 2 );
add_filter('woocommerce_get_sale_price', 'return_custom_price', 10, 2 );
function return_custom_price($price, $product) {
    global $post, $woocommerce;
    if( ! $product->product_type == 'variable'  ) return $price;
    switch (@$product->variation_data['attribute_pa_support']) {
        case "pdf" :
            return ($price * 0.5);
        break;
    }
    return $price;
}