PayPal express checkout integration with multiple buttons on one page

自作多情 提交于 2019-12-05 13:14:18

You need to give each element a unique id, then call render multiple times:

<ul>
    <li data-id="1">Item 1 <div id="button-1"></div></li>
    <li data-id="2">Item 2 <div id="button-2"></div></li>
    <li data-id="3">Item 3 <div id="button-3"></div></li>
</ul>

<script src="https://www.paypalobjects.com/api/checkout.js"></script>

<script>
    [ '#button-1', '#button-2', '#button-3' ].forEach(function(selector) {
        paypal.Button.render({
            // options
        }, selector);
    });
</script>

If you don't like keeping track of IDs like me, you can use classes instead. And use data attributes to differentiate.

<div class="paypal-button" data-my-attribute="tree"></div>
<div class="paypal-button" data-my-attribute="dog"></div>
<div class="paypal-button" data-my-attribute="car"></div>

<script src="https://www.paypalobjects.com/api/checkout.js"></script>

<script>
    document.querySelectorAll('.paypal-button').forEach(function(selector) {
        paypal.Button.render({
            // options
            console.log( selector.dataset.myAttribute );
        }, selector);
    });
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!