Php 如果已购买产品,则将商品价格设置为零

Php 如果已购买产品,则将商品价格设置为零,php,wordpress,woocommerce,cart,price,Php,Wordpress,Woocommerce,Cart,Price,我无法推翻第二次购买的价格 用例是:如果用户已经购买了产品“944”,那么下次订单的价格将是0 也就是说,客户只需为该特定产品的第一次订单付款,而下一次订单则是免费的 这是我的代码: // 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 ); /

我无法推翻第二次购买的价格

用例是:如果用户已经购买了产品“944”,那么下次订单的价格将是0

也就是说,客户只需为该特定产品的第一次订单付款,而下一次订单则是免费的

这是我的代码:

 // 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+中工作的方法:

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 );
        }
    }
}
add\u action('woocommerce\u before\u calculate\u totals','conditionally\u change\u cart\u items\u price',10,1);
函数有条件地\u更改\u购物车\u项目\u价格($cart\u对象){
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
//在此设置您的目标产品ID
$targeted_product_id=944;
//在此设置您的自定义价格(第一次购买)
$custom_price=10;//首次购买产品ID 944
//检测客户是否已购买目标产品(944)
如果(用户是否已登录){
$customer=wp_get_current_user();
$customer\u id=$customer->id;//客户id
$customer\u email=$customer->email;//客户电子邮件
if(wc_客户_购买的_产品($customer_email、$customer_id、$targeted_product_id))
$custom_price=0;//对于其他购买(产品ID 944)设置为0
}
foreach($cart\u object->get\u cart()作为$cart\u项目){
//当目标产品在购物车中时,我们会更改价格
if($cart\u item['product\u id']==$targeted\u product\u id){
//Woocommerce 3+兼容性

if(版本比较(WC_版本,'3.0',它不起作用。if(!WC_客户_购买的产品($customer_email,$customer_id,$targeted_product_id))需要是if(WC_客户_购买的产品($customer_email,$customer_id,$targeted_product_id))没有!@MujtabaK Updated抱歉,我忘了删除在进行测试时添加的
,但此代码有效…如果我从特定类别中选择5种产品,购物车中的所有产品都要收取100美元的费用。我想将第一次订单的费用限制为100美元,而不是500美元,然后下一次订单的费用为0.00美元。@MujtabaK wc_customer_bunded\u prproduct()函数不适用于产品类别,仅适用于产品ID