WooCommerce obtém o preço da sessão após adicionar ao carrinho
// get and set the custom product price in WC_Session
// site/?custom_p=77
add_action( 'init', 'get_custom_product_price_set_to_session' );
function get_custom_product_price_set_to_session() {
// Check that there is a 'custom_p' GET variable
if( isset($_GET['add-to-cart']) && isset($_GET['custom_p'])
&& $_GET['custom_p'] > 0 && $_GET['add-to-cart'] > 0 ) {
// Enable customer WC_Session (needed on first add to cart)
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('custom_p', [
'id' => (int) wc_clean($_GET['add-to-cart']),
'price' => (float) wc_clean($_GET['custom_p']),
]);
}
}
// Change product price from WC_Session data
add_filter('woocommerce_product_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_product_price', 900, 2 );
function custom_product_price( $price, $product ) {
if ( ( $data = WC()->session->get('custom_p') ) && $product->get_id() == $data['id'] ) {
$price = $data['price'];
}
return $price;
}
Helpful Hawk