如果已经购买了产品,请将Woocommerce购物车项目价格设为零 [英] Set Woocommerce cart item price to zero if the product has already been bought

查看:67
本文介绍了如果已经购买了产品,请将Woocommerce购物车项目价格设为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在第二次购买时超越价格。

I am unable to override price on second time purchase.

用例将是:如果用户已经购买了产品 944,则下一次订购的价格为0。

The use case would be: If a user has already bought product "944" then the price would be 0 for next time orders.

意思是,客户仅需为该特定产品的第一笔订单付款,下一笔订单即可免费。

Meaning, the customer would pay only for first order of that specific product and it would be free for next orders.

这里是我的代码:

 // Enter the ID of the product that shouldn't be purchased again
    $no_repeats_id = 944;
    $no_repeats_product = wc_get_product( $no_repeats_id );

    // Get the current product to check if purchasing should be disabled
    global $product;

    if ( $no_repeats_product->is_type( 'variation' ) ) {
        // Bail if we're not looking at the product page for the non-purchasable product
        if ( ! $no_repeats_product->parent->id === $product->id ) {
            return;
        }

        // Render the purchase restricted message if we are
        if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
            sv_render_variation_non_purchasable_message( $product, $no_repeats_id );
        }

    } elseif ( $no_repeats_id === $product->id ) {
        if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id ) ) {
            // Create your message for the customer here
            add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    $target_product_id = 944;
    foreach ( $cart_object->cart_contents as $value ) {
        if ( $value['product_id'] == $target_product_id ) {
            $value['data']->price = $custom_price;
        }
        /*
        // If your target product is a variation
        if ( $value['variation_id'] == $target_product_id ) {
            $value['data']->price = $custom_price;
        }
        */
    }
}

        }
    }
}
add_action( 'woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31 );


推荐答案

这是使其在woocommerce中起作用的方法3

Here is the way to make it work in woocommerce 3+ too:

add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
function conditionally_change_cart_items_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Set Here your targeted product ID
    $targeted_product_id = 944;

    // Set Here your custom price (1st purshase)
    $custom_price = 10; // First purshase for product ID 944

    // Detecting if customer has already bought The targeted product (944)
    if( is_user_logged_in() ){
        $customer      = wp_get_current_user();
        $customer_id   = $customer->ID; // customer ID
        $customer_email = $customer->email; // customer email

        if( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id) )
            $custom_price = 0; // Set to 0 for other purchases (product ID 944)
    }

    foreach ( $cart_object->get_cart() as $cart_item ) {
        // When targeted product is in cart we change the price
        if ( $cart_item['product_id'] == $targeted_product_id ) {
            // Woocommerce 3+ compatibility
            if ( version_compare( WC_VERSION, '3.0', '<' ) )
                $cart_item['data']->price = $custom_price;
            else
                $cart_item['data']->set_price( $custom_price );
        }
    }
}

代码进入function.php子主题文件(或主题文件)或任何插件文件中。

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码已经过测试,即使在WooCommerce 3+中也可以使用

This code is tested and works even in WooCommerce 3+

这篇关于如果已经购买了产品,请将Woocommerce购物车项目价格设为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆