skip to Main Content

I found the ability to add to the cart through URL:

http://yoururl.com/cart/?add-to-cart=ID

I also found how to add quantity and attributes using this link, but can’t find a way to add a price.
How to use this link to add a price to the cart?

2

Answers


  1. If it is a simple product, the price is the one declared.

    If it is a variable product, you can use the variation id.

    > href=”http://yourdomain.com/?add-to-cart=88″
    

    You can find more info about that, here. https://businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/

    If is not working, please provide what code are you trying to use and more info about your issue.

    Login or Signup to reply.
  2. If you want to add the product to cart with a custom price, you can’t via URL.
    You need to do it via PHP (if you are currently using JS, you will need to use an AJAX function like jQuery $.post and call a PHP function).

    In the PHP function you add the product:

    function addtocart(){
       $cart_item_data['custom_price'] = 5678;
       WC()->cart->add_to_cart( $product_id, 1, 0, array(), $cart_item_data);
    }
    

    You will also need to modify the price in the cart

    // Change product price in the cart
    add_action( 'woocommerce_before_calculate_totals', 'change_price_function' );
    
    function change_price_function( $_cart ){
            // loop through the cart_contents
            foreach ( $_cart->cart_contents as $cart_item_key => $value ) {       
                $value['data']->set_price($value['custom_price']);
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search