• Let’s say that we have a simple PHP file serving as a basic pricing API. A remote site can send a request to this file and the sole function of it is to generate an ID and a Price, and store them in the database.

    Ideally then if someone hits the URL http://example.com/?add-to-cart-special-argument&id=XXXXXX&price=YYYYYYY, it verifies that the supplied arguments are valid and exist in the database, and then a product is added to the cart with the price YYYYYYY and a product attribute (or whatever) with the ID.

    Any ideas of where to look at or any guidance for the woocommerce part?

    https://wordpress.org/plugins/woocommerce/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    You’d need a custom plugin listening for that endpoint to handle this.

    Thread Starter jobme

    (@jobme)

    Thanks for the reply Mike!
    Do you believe that without the cross-check, it can be done simpler by using a product attribute value as the price?

    I found this but it’s adding a filter to show something, while I need to change the price.

    Thread Starter jobme

    (@jobme)

    In essence what I need is someone to be able to click a button on another website that “knows” the price and somehow lets my site learn it, add it to cart and order it.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    I think it could be done with a single dummy product. Your endpoint would be sent the price etc, it would add this dummy product to the cart, and then set the price and store in session.

    Thread Starter jobme

    (@jobme)

    Here is what I am trying to do: A fellow site makes a request to my site like this:
    http://example.com/?add-to-cart=14&REQ&id=32&key=6337

    Then if the REQ parameter exists, I crosscheck with a separate database to see the price in a table row where theID and KEY match. Then I use this price to add the custom product to cart.

    And here is the code as a separate plugin after seeing this solution:

    add_filter( 'woocommerce_add_cart_item' , 'set_woo_prices');
    add_filter( 'woocommerce_get_cart_item_from_session', 'set_session_prices', 20 , 3 );

    function set_woo_prices( $woo_data ) {
        $w_price = 999.99;
        if (isset($_GET['REQ'])) {
           $w_id = $_GET['id'];
           $w_key = $_GET['key'];
           //we ask the database
            $my_wpdb = new WPDB( 'rt', 'rt', 'db', 'localhost');
            $curr_req = $my_wpdb->get_row( "SELECT * FROM tbl WHERE id = $w_id", ARRAY_A );
            if ( $curr_req['keypin'] == $w_key ){
               $w_price = $curr_req['price'];
                }
            else {
               exit;
           }
            $woo_data['data']->set_price( $w_price );
            $woo_data['my_price'] = $w_price;
            return $woo_data;
           }
        };
    function  set_session_prices ( $woo_data , $values , $key ) {
        if ( ! isset( $woo_data['my_price'] ) || empty ( $woo_data['my_price'] ) ) { return $woo_data; }
        $woo_data['data']->set_price( $woo_data['my_price'] );
        return $woo_data;
    };

    But no… Of course it wouldn’t work:

    • If I add to cart for the first time and the ID and KEY match, I get the proper price and I’m good. If I delete the product from the cart, and re-add it with different ID, KEY and thus Price, it takes the previous price or the original from the product.
    • If I try to add any product to cart without the parameters it’s not added. If I deactivate the plugin, it works.

    For the love of God, I cannot understand what is wrong with the above code. I assume there must be something with the session but this would only explain the first point.

    Any ideas, help are highly appreciated and needed. Even if I’m in a completely wrong direction.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Hmm, when you do the second add, is that query string still there?

    With filters, you need to return the original value if you don’t want your code to affect things. Thats probably why you see:

    If I try to add any product to cart without the parameters it’s not added. If I deactivate the plugin, it works.

    Thread Starter jobme

    (@jobme)

    I’m not sure I get it right but if you mean the part “and re-add it with different ID…”, yes, I add it in the same way just using different ID & KEY in order to have a different price.

    Can I somehow pinpoint this to just product with ID 14; so at least all other products can be untouched?

    Is it the right direction at all or is it better to find another way?

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Let me point you back to your code.

    add_filter( 'woocommerce_add_cart_item' , 'set_woo_prices');

    you are filtering on woocommerce_add_cart_item. But in your code, if if (isset($_GET['REQ'])) { is false, you return nothing. It should return instead an unaltered $woo_data

    Thread Starter jobme

    (@jobme)

    Thank you Mike, I added an else statement which returns the $woo_data
    This saved the normal add to cart.

    What works horribly inconsistently though is -after clearing all sessions- when I try to add the product from scratch through this URL it tries to add it multiple times (have chosen sold individually) and prices are wrong. I do believe that something is wrong on the session part.

    Should I maybe use the hook woocommerce_get_price instead?

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Probably not because you’d still need to track what the price was which came in. I think the way you’re doing it (add to cart with custom props/session vars) is better.

    Maybe rather than hook into add_cart_item, you have your own custom add to cart handler and have that add to cart with custom data.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Woocommerce product with price from JSON’ is closed to new replies.