Php 在Woocommerce单一产品页面上有条件地设置特定产品价格&;运货马车

Php 在Woocommerce单一产品页面上有条件地设置特定产品价格&;运货马车,php,wordpress,woocommerce,custom-taxonomy,price,Php,Wordpress,Woocommerce,Custom Taxonomy,Price,在Woocommerce中,我想更改单个产品页面和相关购物车项目上特定产品的价格(在本例中,ID为87) 产品价格需要增加10美元,但只能在单个产品页面上增加,并且只能在外部增加(以便内部价格或Woocommerce中设置的价格不会改变) 此外,此价格也应在购物车中更改,但前提是某一类别的产品不在购物车中 上下文:如果有人自己购买此产品,他们应收取高于正常价格10美元的费用。如果有人购买该商品时同时购买某一类别的产品,则应按正常价格向其收取费用。 这本质上是一种反向折扣(我不想使用优惠券功能收取

在Woocommerce中,我想更改单个产品页面和相关购物车项目上特定产品的价格(在本例中,ID为87)

产品价格需要增加10美元,但只能在单个产品页面上增加,并且只能在外部增加(以便内部价格或Woocommerce中设置的价格不会改变)

此外,此价格也应在购物车中更改,但前提是某一类别的产品不在购物车中

上下文:如果有人自己购买此产品,他们应收取高于正常价格10美元的费用。如果有人购买该商品时同时购买某一类别的产品,则应按正常价格向其收取费用。 这本质上是一种反向折扣(我不想使用优惠券功能收取额外费用)

对于购物车功能,到目前为止,我有这个,但它不起作用-购物车的价格是75美元,而当前的常规/基本价格是45美元。我不知道75是从哪里来的,而它应该是55

function wc_cart_custom_price( $cart_object ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

        // set our flag to be false until we find a product in that category
        $cat_check = false;

        // check each cart item for our category
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

            $product = $cart_item['data'];

            // replace 'your-category' with your category's slug
            if ( has_term( 'your-category', 'product_cat', $product->id ) ) {
                $cat_check = true;
                // break because we only need one "true" to matter here
                break;
            }
        }

        // if a product in the cart is in our category, stop there and don't change price
        if ( $cat_check ) {
            return;
        }
        // else continue and alter the product price
        else if ( $cart_item['product_id'] == 87 ) {
            // we have the right product, do what we want
            $price = $cart_item['data']->get_price(); // Get the product price
            $new_price = $price + 10; // the calculation
            $cart_item['data']->set_price( $new_price ); // Set the new price
            }
}
add_action( 'woocommerce_before_calculate_totals', 'wc_cart_custom_price' );
目前,我更改产品页面价格的代码是:

function wc_change_product_price($price, $product) {
    if ( is_single('87') ) {
        $post_id = $product->id;

        $regular_price = get_post_meta( $post_id, '_regular_price', true);
        $custom_value = ( $regular_price + 10 );
        $price = $custom_value;

    return $price;
    }
}
add_filter('woocommerce_get_price', 'wc_change_product_price', 99);
这是一场灾难,并导致500服务器错误。我做错了什么?欢迎任何帮助

参考资料:

更新3(仅适用于已定义的产品ID)

如果任何购物车项目不属于特定的产品类别,则只在单个产品页面上更改简单产品价格(不更改存档产品价格、相关产品价格、追加销售和交叉销售)

然后,您将需要以下所有代码:

// Change displayed price on single product pages
add_action( 'woocommerce_single_product_summary', 'change_simple_product_price_html', 9 );
function change_simple_product_price_html() {
    global $product;

    // Only for product ID 87
    if( $product->get_id() != 87 ) return;

    // HERE set your product category term ID, slug or name
    $product_category = 't-shirts';

    // Checking for the product category in cart items loop
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
            return; // Product category found ==> We EXIT from this function
    }

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    add_action( 'woocommerce_single_product_summary', 'custom_simple_product_price_html', 10 );
}

function custom_simple_product_price_html(){
    global $product;

    $addp = 10; // Here the price additional amount

    if ( '' !== $product->get_price() && ! $product->is_on_sale() ) {
        $price = wc_price( wc_get_price_to_display( $product ) + $addp ) . $product->get_price_suffix();
    }
    echo '<p class="price">' . apply_filters( 'woocommerce_get_price_html', $price, $product ) . '</p>';
}

// Add custom calculated price to cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_simple_product_custom_price', 20, 2 );
function add_cart_simple_product_custom_price( $cart_item_data, $product_id ){
    // Only for product ID 87
    if( $product_id != 87 ) 
        return $cart_item_data;

    $product = wc_get_product($product_id); // The WC_Product Object

    // Only if product is not on sale
    if( $product->is_on_sale() ) 
        return $cart_item_data;

    $price = (float) $product->get_price();

    // Set the custom amount in cart object
    $cart_item_data['new_price'] = $price + 10;

    return $cart_item_data;
}

// Set custom calculated price to cart
add_action( 'woocommerce_before_calculate_totals', 'set_cart_simple_product_custom_price', 20, 1 );
function set_cart_simple_product_custom_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // HERE set your product category term ID, slug or name
    $product_category = 't-shirts';

    // 1st cart items Loop: checking for the product category
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
            return; // Product category found ==> We EXIT from this function
    }

    // 2nd Loop: Changing cart item prices (Product category not found)
    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['new_price']))
            $cart_item['data']->set_price( $cart_item['new_price'] );
    }
}
//更改单个产品页面上显示的价格
添加操作('woocommerce\u single\u product\u summary'、'change\u simple\u product\u price\u html',9);
功能更改\u简单\u产品\u价格\u html(){
全球$产品;
//仅适用于产品ID 87
如果($product->get_id()!=87)返回;
//在这里设置您的产品类别术语ID、slug或名称
$product_分类=‘t恤’;
//在购物车项目循环中检查产品类别
foreach(WC()->cart->get_cart()作为$cart_项目){
if(具有术语($product_category,$product_cat',$cart_item['product_id']))
return;//找到的产品类别==>我们退出此函数
}
删除行动('woocommerce\u single\u product\u summary'、'woocommerce\u template\u single\u price',10);
添加操作('woocommerce\u single\u product\u summary'、'custom\u simple\u product\u price\u html',10);
}
函数自定义\简单\产品\价格\ html(){
全球$产品;
$addp=10;//这里是价格附加金额
如果(“”!=$product->get_price()&&!$product->正在销售()){
$price=wc_price(wc_get_price_to_display($product)+$addp)。$product->get_price_后缀();
}
回显“

”。应用过滤器('woocommerce\u get\u price\u html',$price$product)。”

; } //将自定义计算价格添加到购物车项目数据 添加过滤器(“woocommerce\u添加购物车\u项目\u数据”、“添加购物车\u简单\u产品\u自定义\u价格”,20,2); 函数add\u cart\u simple\u product\u custom\u price($cart\u item\u data,$product\u id){ //仅适用于产品ID 87 如果($product_id!=87) 返回$cart\u item\u数据; $product=wc\U get\U product($product\U id);//wc\U product对象 //仅当产品不在销售时 如果($product->正在销售()) 返回$cart\u item\u数据; $price=(float)$product->get_price(); //设置购物车对象中的自定义金额 $cart\u item\u data['new\u price']=$price+10; 返回$cart\u item\u数据; } //将自定义计算价格设置为购物车 添加操作(“计算合计前的woocommerce”、“设置购物车”、“简单产品”、“自定义价格”,20,1); 功能集\购物车\简单\产品\自定义\价格($cart){ if(定义了('DOING'uajax')) 返回; 如果(did_action('woocommerce_before_calculate_totals')>=2) 返回; //在这里设置您的产品类别术语ID、slug或名称 $product_分类=‘t恤’; //第一个购物车项目循环:检查产品类别 foreach($cart->get\u cart()作为$cart\u项目){ if(具有术语($product_category,$product_cat',$cart_item['product_id'])) return;//找到的产品类别==>我们退出此函数 } //第二个循环:更改购物车项目价格(未找到产品类别) foreach($cart->get\u cart()作为$cart\u项目){ 如果(isset($cart\u item['new\u price'])) $cart_item['data']->set_price($cart_item['new_price']); } }

代码进入活动子主题(或活动主题)的function.php文件。已测试且有效。

如果您希望在“单一产品”页面上更改产品价格。 请遵循此代码,我已添加产品ID“99”:

add_action('woocommerce_single_product_summary'、'e_coding_change_simple_product_price_html',9);
函数e\编码\更改\简单\产品\价格\ html(){
全球$产品;
//仅适用于产品ID 99
如果($product->get_id()!=99)返回;
//在这里设置您的产品类别术语ID、slug或名称
$product_category=‘我的书’;
//在购物车项目循环中检查产品类别
foreach(WC()->cart->get_cart()作为$cart_项目){
if(具有术语($product_category,$product_cat',$cart_item['product_id']))
return;//找到的产品类别==>我们退出此函数
}
删除行动('woocommerce\u single\u product\u summary'、'woocommerce\u template\u single\u price',10);
添加操作('woocommerce\u single\u product\u summary'、'custom\u simple\u product\u price\u html',10);
}
函数自定义\简单\产品\价格\ html(){
全球$产品;
$addp=10;//这里是价格附加金额
如果(“”!=$product->get_price()&&!$product->正在销售()){
$price=wc_price(wc_get_price_to_display($product)+$addp)。$product->get_price_后缀();
}
回显“

”。应用过滤器('woocome

add_action( 'woocommerce_single_product_summary', 'e_coding_change_simple_product_price_html', 9 );
function e_coding_change_simple_product_price_html() {
    global $product;

// Only for product ID 99
if( $product->get_id() != 99 ) return;

// HERE set your product category term ID, slug or name
$product_category = 'my-book';

// Checking for the product category in cart items loop
foreach ( WC()->cart->get_cart() as $cart_item ) {
    if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
        return; // Product category found ==> We EXIT from this function
}

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'custom_simple_product_price_html', 10 );
}

function custom_simple_product_price_html(){
global $product;

$addp = 10; // Here the price additional amount

if ( '' !== $product->get_price() && ! $product->is_on_sale() ) {
    $price = wc_price( wc_get_price_to_display( $product ) + $addp ) . $product->get_price_suffix();
}
echo '<p class="price">' . apply_filters( 'woocommerce_get_price_html', $price, $product ) .'</p>';
}

// Add custom calculated price to cart item data

add_filter( 'woocommerce_add_cart_item_data', 'add_cart_simple_product_custom_price', 20, 2 );

function add_cart_simple_product_custom_price( $cart_item_data, $product_id ){

// Only for product ID 87
if( $product_id != 99 )
    return $cart_item_data;

$product = wc_get_product($product_id); // The WC_Product Object

// Only if product is not on sale
if( $product->is_on_sale() )
    return $cart_item_data;

$price = (float) $product->get_price();

// Set the custom amount in cart object
$cart_item_data['new_price'] = $price + 10;

return $cart_item_data;
}


function cw_change_product_price_display( $price ) {

    $price .= ' At Each Item Product';
    return $price;

}

add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );