我通过连接到woocommerce_cart_calculate_fees操作来向购物车添加自定义费用,如下所示:

    if (isset($_GET['discount'])) {



        add_action('woocommerce_cart_calculate_fees', 'add_loyalty_discounts');
    }

    function add_loyalty_discounts(WC_Cart $cart)
    {
        $total = 0;

        global $woocommerce;

        foreach (WC()->cart->get_cart() as $cart_item_key => $item) {

            $total += $item['data']->regular_price;

            $woocommerce->cart->cart_contents[$cart_item_key]['discount_amount'] = $_GET['discount'];
        }

        if ($total < $_GET['discount']) {
            wc_add_notice('You must spend more than €' . $_GET['discount'] . ' to use your loyalty discount.', 'error');
        } else {

            $current_user = wp_get_current_user();

            $loyalty_points = empty(get_user_meta($current_user->ID, 'loyalty_points', true)) ? 0 : get_user_meta($current_user->ID, 'loyalty_points', true);

            $loyalty_points  = 100;

            if ($loyalty_points < $_GET['discount']) {
                wc_add_notice('You do not have enough loyalty points to apply this discount.', 'error');
            } else {

                $cart->add_fee('Loyalty Discount',   -intval($_GET['discount']), true);
            }
        }
    }

费用显示在购物车页面上,但不会保留在 checkout 和订购页面上.

我试过用$cart->set_session(),但没有效果.

更新:我已经创建了一个新的可湿性粉剂网站与一个空白的主题(白板),只有最新版本的WooCommerce安装,我仍然得到错误.

推荐答案

Update,(based on your newly added code).

您不能在woocommerce_cart_calculate_fees钩子中使用$_GET$_POST$_REQUEST,因为这些变量将丢失.相反,您应该将$_GET[‘Discount’]存储在WCSESSION变量中.

现在,您的代码已经过时,有一些错误和遗漏的东西.

改为try 以下重新访问的代码:

add_action('template_redirect', 'save_discount_in_session');
function save_discount_in_session() {
    if ( isset($_GET['discount']) ) {
        WC()->session->set('loyalty_discount', floatval($_GET['discount']));
    }
}

add_action('woocommerce_cart_calculate_fees', 'add_loyalty_discounts');
function add_loyalty_discounts($cart){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $discount = (float) WC()->session->get('loyalty_discount');
    $subtotal = (float) WC()->cart->subtotal;

    if ( $discount > 0 ) {
        if ( $subtotal >= $discount ) {
            $loyalty_points = (int) get_user_meta(get_current_user_id(), 'loyalty_points', true);
    
            if ( $loyalty_points >= $discount ) {
                $cart->add_fee( __('Loyalty Discount'), -$discount );
            } else {
                wc_clear_notices();
                wc_add_notice( __('You do not have enough loyalty points to apply this discount.'), 'error');
            }
        } else {
            wc_clear_notices();
            wc_add_notice( sprintf( __('You must spend more than %s to use your loyalty discount.'), wc_price($discount) ), 'error');
        }
    }
}

代码放在您的子主题的函数.php文件中(或在插件中).应该能行得通.


要仅在 checkout 页面中应用费用,您需要使用:

add_action('woocommerce_cart_calculate_fees', 'add_loyalty_discounts');
function add_loyalty_discounts($cart) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( is_cart() )
        return;
        
    $cart->add_fee('Loyalty Discount', -100);
}

相关:

Php相关问答推荐

邮箱打开跟踪器不工作时发送邮件使用laravel调度程序

模型中的Laravel自定义查询字段

将带有值的属性添加到WooCommerce产品,保留现有属性

设置WordPress最近的帖子小部件按修改日期排序

是否使用联系人表单7 wpcf7_Feedback_Response输出答复中的html?

在 WooCommerce 更新结帐事件上动态更新自定义内容

将图像作为 PHP post 响应发送到浏览器

WooCommerce 有关新订单的附加邮箱通知,其中包含具体详细信息

在 WooCommerce 我的帐户自定义部分显示未购买的产品

使用现有的htaccess的PHP路由脚本

从图像URL中获取文件扩展名?

如何在WooCommerce中为访客购买者分配自定义客户代码

Laravel使用另一张表进行关联查询

使用来自 PHP 表单的数据更新 xml 命名空间

PHP for each 函数打印 td

是否有适用于 Windows 的真正 64 位版本的 PHP 5.6?

NetBeans 不显示中文内容

如何在光速网络服务器中使用 apache ..htaccess

基于其他数组创建多维数组 struct

如何获取最后一条消息属于 Laravel 中特定用户的对话列表