简体   繁体   English

Woocommerce:如何将产品从 iframe 添加到购物车?

[英]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.我在 WooCommerce 上有一家商店。它在 iframe 中有可定制的产品。“添加到购物车”按钮本身也在 iframe 中。当我单击“添加到购物车”按钮时,我从 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.在 event.data 我有一个产品 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?我需要在哪里添加这个链接?

Check out the answers here: https://wordpress.stackexchange.com/questions/53280/woocommerce-add-a-product-to-cart-programmatically-via-js-or-php在此处查看答案: 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:如果你不能这样做,也可以考虑 PHP 方式:

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.如果采用 PHP 方式,则需要在通过 JavaScript (AJAX) 调用的 PHP 函数中运行上述代码,因此会更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM