Search
 
SCRIPT & CODE EXAMPLE
 

PHP

woocommerce get the price from session after add to cart

// 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;
}
Comment

PREVIOUS NEXT
Code Example
Php :: how to make core controller codeigniter 3 more than 1 
Php :: laravel valet refresh env 
Php :: import faker in laravel 
Php :: php number format without rounding 
Php :: laravel blade global variable 
Php :: php array access by key 
Php :: phpexcel set row height 
Php :: php strom key 1 
Php :: laravel blade @auth 
Php :: create laravel 8 resource route 
Php :: url segment in laravel 
Php :: laravel where null 
Php :: autoloader php 
Php :: laravel create session table 
Php :: echo errors php 
Php :: php check if valid xml 
Php :: laravel store file 
Php :: orwhere in wherehas laravel 
Php :: find php ini 
Php :: php function 
Php :: how to send data from html to php 
Php :: php include once inside a function? 
Php :: array marge in php 
Php :: mp3 file upload code in php 
Php :: jquery is less than or equal to 
Php :: nested with laravel 
Php :: Hide Add to cart button on specific products 
Php :: Get All dates of a month with laravel carbon 
Php :: display data from two dimensional array in vew laravel 
Php :: Diferencia entre dias PHP - Con date_diff() 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =