Woocommerce 中特定产品的自定义价格

Custom Price for a specific product in Woocommerce

在 Woocommerce 中,我想为特定产品应用自定义价格。

这是我的实际代码:

add_action('woocommerce_get_price','change_price_regular_member', 10, 2);
function change_price_regular_member($price, $productd){
    return '1000';
}

如有任何帮助,我们将不胜感激。

试试这个代码。只需替换产品 ID 3030

add_action('woocommerce_get_price','change_price_regular_member', 10, 2);
function change_price_regular_member($price, $productd){
    if($productd->id==3030){
        $price= 1000;
    }
    return $price;
}

挂钩 woocommerce_get_price 在 Woocommerce 3 中已过时和弃用(它是一个 过滤器挂钩 但不是动作挂钩).它已被替换为 woocommerce_product_get_price

所以试试这个 (您将在此挂钩函数中定义目标产品 ID)

add_filter( 'woocommerce_product_get_price','change_price_regular_member', 10, 2 );
function change_price_regular_member( $price, $product ){
    // HERE below define your product ID
    $targeted_product_id = 37;

    if( $product->get_id() == $targeted_product_id )
        $price = '1000';

    return $price;
}

此代码位于您的活动子主题(或主题)的 function.php 文件中。已测试并有效。