【问题标题】:Woocommerce: how to add a product from iframe to the cart?Woocommerce:如何将产品从 iframe 添加到购物车?
【发布时间】:2020-03-25 12:39:30
【问题描述】:

我在 WooCommerce 上有一家商店。它在 iframe 中有可定制的产品。“添加到购物车”按钮本身也在 iframe 中。当我单击“添加到购物车”按钮时,我会收到一条消息,其中包含来自iframe。

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

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

}

我在链接中找到了一种将产品添加到购物车的方法

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

在 event.data 我有一个产品 ID。 我的问题是:如何使用所有这些将产品添加到购物车?我需要在哪里添加这个链接?

【问题讨论】:

    标签: javascript php wordpress iframe woocommerce


    【解决方案1】:

    在此处查看答案: https://wordpress.stackexchange.com/questions/53280/woocommerce-add-a-product-to-cart-programmatically-via-js-or-php

    特别是,这是您需要的:

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

    如果你不能这样做,也可以考虑PHP方式:

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

    如果您采用 PHP 方式,则需要在您将通过 JavaScript (AJAX) 调用的 PHP 函数中运行上述代码,因此更加复杂。

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 2021-08-22
      • 2013-03-10
      • 1970-01-01
      • 2014-02-06
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多