0

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 redirecting to their website 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);
});

FYI>> All the products are published as a WooCommerce simple product

Can you please help me how I can supply console logs data such as Title & Price into WooCommerce cart? I want to make third party book now button as a Add to Cart button so it'll take title & price only.

If you have any question please let me know.

1 Answer 1

0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.