• Resolved Sajid Manzoor

    (@sajiddesigner)


    Hello
    I have one product in Woo Commerce, lets say MY Product
    This product price varies for each user.
    Lets say
    For User 1, product price is $10
    For User 2, product price is $20
    For User 3, product price is $30

    I have multiple number of users, so i dont want to create multiple product for each user.,
    Please suggest how can i change product price depending on user ID

    I have checked woocommerce_before_calculate_totals action and it may work for me, but i am not getting how can i change product price based on user ID
    Here is action code

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
    function add_custom_price( $cart_object ) {
        $custom_price = 10; // This will be your custome price
        foreach ( $cart_object->cart_contents as $key => $value ) {
            $value['data']->price = $custom_price;
        }
    }

    Also I don’t want to use Variable products

    Thank you
    Sajid

    https://www.ads-software.com/plugins/woocommerce/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    You can use the get_current_user_id function.

    https://codex.www.ads-software.com/Function_Reference/get_current_user_id

    Or use an extension such as dynamic pricing.

    Thread Starter Sajid Manzoor

    (@sajiddesigner)

    @mike Jolley Thank you for your reply
    I know how can i get user_id
    but i dont know how to use user_id or post_id in action function

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    user_id = get_current_user_id()
    post_id = get the cart and loop like this:

    foreach ( WC()->cart->get_cart() as $key => $cart_item ) {
       if ( $cart_item['data']->id == 123 ) {
          $cart_item['data']->adjust_price( '10' );
       }
    }

    Something along those lines. Easier to use dynamic pricing though – that can add rules based on user id without code.

    Thread Starter Sajid Manzoor

    (@sajiddesigner)

    @mike,
    Friend My scenario has changed. Now i need to calculate Product price on run time using Post_id
    I have custom posts. with add to cart to button.
    Let say
    1- Custom Post 1 (id=32, price=30)
    2- Custom Post 2 (id=33, price=36)
    3- Custom Post 3 (id=34, price=50)

    and so on.
    I have added a custom meta field for Price in custom post.
    I have changed my code as follows

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
    function add_custom_price( $cart_object ) {
    global $post;
    $post_id=$post->ID();  // To get post id
     // Code to get custom field here.
        $custom_price = 10; // This will be your custome price
        foreach ( $cart_object->cart_contents as $key => $value ) {
            $value['data']->price = $custom_price;
        }
    }

    But now in return $post_id=$post->ID(); return ID of cart page.
    Please tell me how can i get post_id of post from where the Add to cart event has fired

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Like in my example, you’d need to loop over cart items, not take the global. The extension is still advisable if you’re not familiar with PHP.

    Thread Starter Sajid Manzoor

    (@sajiddesigner)

    Hello @mike
    Thank you for suggesting plugin. I will check and let you know.
    But i would like to do this using code.
    Just plz tell me how can i get right post id when clicked on add to cart buttom

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    There are various hooks at play. It’s not a one line snippet! Take a look at https://github.com/mikejolley/woocommerce-product-gift-wrap/blob/master/woocommerce-product-gift-wrap.php#L77-L81 – that adds costs to line items. Could be adapted maybe.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WooCommerce, Add Product to Cart with different price each time’ is closed to new replies.