5

I am using the third party gadget where they are providing live availability, cost and book now button. When customer click on book now button, it's using their booking gadget which I want to ignore.

After doing some google research, I am able to get correct Title & cost under console logs when some is clicking on the book now button.

$w.event.subscribe("item.book.click", function(item) { 
   console.log(item);
   console.log("Title " + item[3].Name + " (Date " + item[4].date  + ", Period " + item[4].period + ", Adults " + item[4].adults + ", Children " + item[4].children + ", Infants " + item[4].infants + ")");
   console.log("Price " + item[3].Availability.Cost);
});

WooCommerce simple product Live Demo: http://plugin.seminyak.holiday/product/the-layar/#/accom/66268

Click on Book Now button and see console log it's returning value of Title & Price but how to trigger console data value in pop up buttons like BUY NOW & ADD ITEM TO CART.

When user click on ADD ITEM TO CART it'll add data into /cart page + basket and If someone click on BUY NOW button it'll take data and redirect user on /checkout page with console Title and Price

If you have any question please let me know.

Group Of Oceninfo
  • 358
  • 2
  • 3
  • 19
  • I don't get it. – Reigel Gallarde Nov 02 '17 at 03:03
  • open link and you'll see Add to Basket button that button is woocommerce button, after scrolling down to the page you'll see some change dates or book now button, When you click on Book Now button it'll open pop up which contains Add item to cart and book now button. So, When you click on book now button you'll see in console logs Title and price. I want that title and price when someone click Add item to cart button in pop up or click on book now. So, I want that third party gadget value in woocommerce cart. – Group Of Oceninfo Nov 02 '17 at 06:39
  • can save `item` on a global variable and access it on "Add item" button click... – Reigel Gallarde Nov 02 '17 at 09:31
  • Simple, you just need to subscribe to the click event of the Add Item button and display the values. Can you tell us what the plugin you're using is called? – user2924019 Nov 02 '17 at 17:06
  • I found it. The click event you want to subscribe to is `cart.add.click`. Look at what code you have on the frontend.js, and make a copy for cart.add.click. The values should be the same. – user2924019 Nov 02 '17 at 17:25

2 Answers2

0

As is done in this answer:

Capturing javascript console.log?

You may be able to capture the console log when the page loads and fire events based on the output.

(function(){
      var oldLog = console.log;
      console.log = function (message) {
          if(message.indexOf("Price") !== -1){
            var price = message.split(' ')[1]
            // Do something with the price...
          }
          oldLog.apply(console, arguments);
       };
    })();

You might have to do something tricky since the description and price come in two separate logs.

nbwoodward
  • 2,816
  • 1
  • 16
  • 24
  • I can merge description and price in one console.log as well but no idea how I can supply this data into woocommerce cart – Group Of Oceninfo Nov 07 '17 at 05:40
  • Oh right, i definitely misunderstood the question. For the "Book Now" button could you pass the price and title using URL params? `window.location("/checkout?name=" + item[3].Name + "&price=" + item[3].Availability.Cost )` Or is the checkout page totally handled by Woocommerce? – nbwoodward Nov 07 '17 at 11:13
  • I have tried that one but it's managed by WooCommerce completely. – Group Of Oceninfo Nov 14 '17 at 07:17
0

Thank you for your support and response, After doing lots of research I got my result with below code.

To achieve final result I have to install WC Fields Factory plugin and pass dynamic value using console log or variable def

Step 1: Use this code under .js file and make sure it's getting trigger when button is getting click.

$w.event.subscribe("item.book.click", function(item) {
    $('[name="room_type"]')[0].value = item[3].Name;
    $('[name="date"]')[0].value = item[4].date;
    $('[name="nights"]')[0].value = item[4].period;
    $('[name="adults"]')[0].value = item[4].adults;
    $('[name="children"]')[0].value = item[4].children;
    $('[name="infants"]')[0].value = item[4].infants;
    $('[name="cost"]')[0].value = item[3].Availability.Cost;
    $('[name="add-to-cart"]')[0].click(); // Clicked on add to cart button
});

function.php code to get custom cost value dynamically.

/* Price Update Based On Plugin WC Field */

function calculate_cart_total( $cart_object ) {
    $additionalPrice = 0;

    foreach ( $cart_object as $key => $valueArray ) {
        if(is_array($valueArray)) {
            foreach ( $valueArray as $k => $value ) {
                if($value['wccpf_cost']) {
                    $additionalPrice = $value['wccpf_cost']['user_val'];
                }
            }
        }
    }

    foreach ( WC()->cart->get_cart() as $key => $value ) {
        if( method_exists( $value['data'], "set_price" ) ) {
            $value['data']->set_price( $additionalPrice );
        } else {
            $value['data']->price = ($additionalPrice );
        }     
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total', 99 );

/* before addto cart, clear cart values */
add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );

function woo_custom_add_to_cart_before( $cart_item_data ) {

    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return true;
}

Under WC Fields Factory I have created room_type, date, nights, adults, children, infants, cost fields and onclick value is getting picked up dynamically accordingly.

Group Of Oceninfo
  • 358
  • 2
  • 3
  • 19