skip to Main Content

I am trying to pass a parameter to a Woocommerce site using a URL – for instance:

site/?custom_p=77 will be the URL.

here is my code

add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
function custom_price( $cprice, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());
    // Get the data from the GET request
$custom_p=$_GET['custom_p'];
    return ($custom_p) ;
}

URL add to cart site/?add-to-cart=455&custom_p=77

The problem when I declare the number 77 directly it works $custom_p=77 but $_GET['custom_p'] declaration gives as a result zero in cart.

3

Answers


  1. Your GET variables get lost once URL changes, so all product prices get empty because also you are not targeting a the product that has been added to cart.

    In this case you need to enable and use a WooCommerce Session variable to store the product ID and the custom price, when a custom_p GET variable is detected. Then you can use that WooCommerce Session variable to change the product price.

    First we detect and store the necessary data in a WC_Session variable:

    // get and set the custom product price in WC_Session
    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']),
            ]);
        }
    }
    

    Then there is 2 ways to change the price of the product added to cart (choose only one)

    Option 1 – Changing the product price directly:

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

    Option 2 – Change the cart item price:

    // Change cart item price from WC_Session data
    add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1 );
    function custom_cart_item_price( $cart ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // Must be required since Woocommerce version 3.2 for cart items properties changes
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Looo through our specific cart item keys
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get custom product price for the current item
            if ( ( $data = WC()->session->get('custom_p') ) && $cart_item['data']->get_id() == $data['id'] ) {
                // Set the new product price
                $cart_item['data']->set_price( $data['price'] );
            }
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    USAGE URL variables example: www.example.com/?add-to-cart=37&custom_p=75

    Login or Signup to reply.
  2. Option 1 gives a fatal error on WC admin. To fix that, I had to add !is_admin() to the if clause so it should look like this:

        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 ( ( !is_admin() && $data = WC()->session->get('custom_p') ) && $product->get_id() == $data['id'] ) {
            $price = $data['price'];
        }
        return $price;
    }
    
    Login or Signup to reply.
  3. Nice, it works even when dreamweaver says there is a syntax error in the first Part where the WC_Session variable is set:

        // 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']),
        ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search