简体   繁体   English

如何使用 PayPal 智能按钮在商品缺货时显示售罄消息

[英]How to display sold out message once an itemruns out of stock with PayPal smart button

I am currently working on the store for my website.我目前正在为我的网站开发商店。 I am using the PayPal Smart button for payments.我正在使用 PayPal 智能按钮进行付款。 I was wondering how I can add a stock amount so that when stock number is 0, the ability to purchase becomes unavailable.我想知道如何添加库存数量,以便当库存编号为 0 时,无法购买。 I've searched everywhere around the internet and could not find any answers for my question.我在互联网上到处搜索,找不到我的问题的任何答案。 Here is my current code:这是我当前的代码:

    paypal.Buttons({

    style: {
        shape: 'rect',
        color: 'blue',
        layout: 'vertical',
        label: 'paypal',
        locale: 'en_CY'
    },

    createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [{
                amount: {
                    value: ". $row['paypal_price'] .",
                    currency: 'EUR'
                }
            }]
        });
    },
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
            localStorage.clear();
            window.location.href = 'http://localhost/website/thankyou.php';
            //alert('Transaction completed by ' + details.payer.name.given_name + '!');
            $stock = 0;
        });
    }
}).render('#paypal-button-container');

Thank you in advance!先感谢您!

Your question is more about "how do i update my current stock" and "how do i get my current stock"您的问题更多是关于“我如何更新我当前的股票”和“我如何获得我目前的股票”

Javascript runs client-side, since you dont have your current stock-number on client-side, you will need to make Javascript communicate with your server. Javascript 运行客户端,因为您在客户端没有当前库存编号,您需要让 Javascript 与您的服务器通信。

One way to do this is via ajax.一种方法是通过 ajax。 You can use the fetch-api to accomplish this.您可以使用fetch-api来完成此操作。

This is what you have to do:这是你必须做的:

Before you display the button (php-code):在显示按钮(php-code)之前:

<?php
    // your code ...
    // check stock before you echo. Only echo when $stock is > 0
    
    $stock = /* get current stock-amount */;

    if ($stock > 0) {
        echo "paypal.Buttons([...";
    }

Update your stock on confirmed payment:在确认付款后更新您的库存:

// your current code
localStorage.clear();
window.location.href = 'http://localhost/website/thankyou.php';
// alert('Transaction completed by ' + details.payer.name.given_name + '!');
// $stock = 0;

// add something like this
const formData = new FormData();
formData.append('amountOrdered', /* number as string */);

// maybe use await to block further processing until stock has been updated
fetch('update_stock.php', {
     method: 'POST',
     body: formData
});

In your new update_stock.php :在您的新update_stock.php

<?php
    // update your stock-source by reducing it by $_POST['amountOrdered']

The last step you will need is to check your stock right before you create an order (JS):您需要的最后一步是在创建订单 (JS) 之前检查您的库存:

// async here needed so you can use await
createOrder: async function(data, actions) {
    // check stock here
    let stock = (await fetch('get_current_stock.php')).json();
    let currentStock = stock['current'];

    // you may want to check for amoutTryingToOrder instead of 1
    if (currentStock < 1) {
        alert('Out of stock, sorry :(');
        return false;
    }

    return actions.order.create({

In your get_current_stock.php :在您的get_current_stock.php中:

<?php
    header('Content-Type: application/json');

    $currentStock = /* get current stock */;
    
    echo json_encode([
        'current' => $currentStock
    ]);

Your createOrder function can return false when it's not in stock, rather than proceeding with the actions.order.create您的 createOrder function 可以在没有库存时返回 false,而不是继续执行 actions.order.create

暂无
暂无

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

相关问题 如果 WooCommerce 中的所有变体都缺货,则显示已售罄的灰色按钮 - Display a Sold out greyed button if all variations are out of stock in WooCommerce 如何使用 paypal 智能按钮使产品缺货时无法付款 - How to make payment unavailable when product out of stock with paypal smart button 如何在某个 Woocommerce 类别存档页面中显示已售出/缺货商品,但不在其他页面中显示它们? - How do I show sold/out of stock items in a certain Woocommerce category archive page, but not display them in others? 无法使用已售罄消息禁用缺货产品变体的功能 - Unable to get functionality to work to disable out of stock product variant with sold out message 显示在 WooCommerce 可变产品上售罄,当所有变体都缺货时 - Display sold out on WooCommerce variable product when all variations are out of stock 在woocommerce中显示短代码而不是缺货消息 - Display shortcode instead of out of stock message in woocommerce 如果变体是我们的库存,如何在下拉列表中的产品变体旁边添加售罄文本? - How to add sold out text next to product variant in drop down if variant is our of stock? 使用 PayPal 智能按钮购买后库存价值未更新 - Stock value not updating after purchase with PayPal smart button WooCommerce 产品在实际没有缺货时显示“缺货”消息 - WooCommerce products showing “Out of stock” message when not actually out of stock 如何在woocommerce中为缺货产品启用“添加到购物车”按钮? - How to enable add to cart button for out of stock product in woocommerce?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM