[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Set a custom add to cart price via the URL (GET) in WooCommerce

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #8940
    David Hoang
    Keymaster

    I am developing a wordpress and woocommerce-based website where information on cooking-related training is provided and various kitchen materials are sold.

    Those who want to participate in the trainings apply by filling out a form. Kitchen supplies are also sold through woocommerce.

    Trainings are added to the website with a type of content called training.

    Some trainings are requested to be sold over the woocommerce structure. However, these "Trainings" that want to be sold are wanted to remain in the form of educational content. In addition, it is requested not to be added or moved as a product.

    First of all, I created a virtual product called Education. I hid the product in the store.

    Then I added a custom field for Tutorials called price. The price of each training to be sold will be entered here.

    I have a button "Register for Training" on the training detail page, I changed it to "Buy" for the trainings wanted to sell and the link

    ?add-to-cart=340&custom_price=600&quantity=1 
    

    I gave in the form.

    Here 340 is the id of the virtual product I created.

    When the Buy button is clicked, the virtual product called Education is added to the basket. But I want to update the name and price of this training according to which training detail page is printed.

    The codes I added to functions.php.

    add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
    function before_calculate_totals( $_cart ){
    // loop through the cart_contents
    foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
        // you will need to determine the product id you want to modify, only when the "donation_amount" is passed
        if ( $item['product_id'] == 340 && isset( $_GET['custom_price'] ) ){
            // custom price from POST
            $custom_price = $_GET['custom_price'] > 0 ? $_GET['custom_price'] : 0;
            // save to the cart data
            //$item['data']->price = $custom_price;
            // new versions of WooCommerce may require (instead of line above)...
            $item['data']->set_price($custom_price);
        }
    }
    }
    
    function ipe_product_custom_price( $cart_item_data, $product_id ) {
    if( isset( $_POST['custom_price'] ) && !empty($_POST['custom_price'])) {        
        $cart_item_data[ "custom_price" ] = $_POST['custom_price'];     
    }
    return $cart_item_data;
    }
    add_filter( 'woocommerce_add_cart_item_data', 'ipe_product_custom_price', 99, 2 );
    

    I wanted to update the price with these codes, but it didn’t work.

    How do I dynamically update the information of the virtual product? Or what different method would you suggest?

    #8941
    David Hoang
    Keymaster

    There are some mistakes in your code and some missing things to make it work. Try the following:

    // Set custom data as custom cart data in the cart item
    add_filter( 'woocommerce_add_cart_item_data', 'add_custom_price_as_custom_cart_item_data', 30, 3 );
    function add_custom_price_as_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
        if( ! isset($_GET['custom_price']) ) {
            $cart_item_data['custom_price'] = (float) esc_attr( $_GET['custom_price'] );
            $cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique  
        }
        return $cart_item_data;
    }
    
    // Change cart item price
    add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price' );
    function change_cart_item_price( $cart ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // loop through the cart_contents
        foreach ( $cart->get_cart() as $cart_item ) {
            // you will need to determine the product id you want to modify, only when the "donation_amount" is passed
            if ( isset($cart_item['custom_price']) ) {.
                $item['data']->set_price($cart_item['custom_price']);
            }
        }
    }
    
    // Display the custom price on minicart too
    add_filter( 'woocommerce_cart_item_price', 'change_minicart_item_price', 10, 3 );
    function change_minicart_item_price( $price_html, $cart_item, $cart_item_key ) {
        if( ! is_cart() && isset( $cart_item['custom_price'] ) ) {
            $args = array( 'price' => floatval($cart_item['custom_price']) );
    
            if ( WC()->cart->display_prices_including_tax() ) {
                $product_price = wc_get_price_including_tax( $cart_item['data'], $args );
            } else {
                $product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
            }
            return wc_price( $product_price );
        }
        return $price_html;
    }
    

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

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.