Анна Мороз
Анна Мороз

Reputation: 91

Woocommerce: how to add a product from iframe to the cart?

I have a store on WooCommerce.It has customizable products in the iframe.The "add to cart" button itself is also in the iframe.When I click the Add to Cart button, I get a message with product data from the iframe.

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event)
{
    console.log('test',event);

}

I found a way to add the product to the cart at the link

http: // some_url / checkout /? Add to cart = id

In event.data I have a product id. My question is this:how do I use all this to add a product to the cart? where do i need to add this link?

Upvotes: 0

Views: 1654

Answers (1)

Stefan S
Stefan S

Reputation: 311

Check out the answers here: https://wordpress.stackexchange.com/questions/53280/woocommerce-add-a-product-to-cart-programmatically-via-js-or-php

In particular, this is what you need:

function addToCart(p_id) {
          $.get('/wp/?post_type=product&add-to-cart=' + p_id, function() {
             // call back
          });
       }

If you can't do it like this, consider the PHP way as well:

global $woocommerce;
$woocommerce->cart->add_to_cart($product_id);

If you go the PHP way, you would need to run the above code in an PHP function that you would call via JavaScript (AJAX), so it's more complicated.

Upvotes: 1

Related Questions