有条件地更改 Woocommerce 中的特定产品价格 [英] Conditionally alter specific product price in Woocommerce

查看:33
本文介绍了有条件地更改 Woocommerce 中的特定产品价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改 Woocommerce 中的特定产品,以编程方式在原始产品的基础上增加 10 美元,在这些情况下除外:

I would like to alter a specific product in Woocommerce, adding programmatically to its original an amount of $10, EXCEPT in those cases:

  • 在属于某个类别C"的可预订产品(Woocommerce Bookings)的特定产品页面上.
  • 如果有任何购物车项目属于C"类别.

直到现在,我一直在使用这个答案代码来解决我的一个问题.但我应该需要在任何地方显示这个特定产品的更改价格,除了在前端的可预订产品的产品页面上.

I was using this answer code to one of my questions until now. But I should need to display this specific product altered price everywhere except on that product pages that are bookable products, in front end.

我还有这个代码,在标题旁边显示强制卖出价格.这仍应在强制销售"部分的可预订产品的产品页面上显示特定产品的原始价格:

I have also this code that displays the Force Sells price beside the titles. This should still show the specific product original price on that product pages that are bookable products in the Force Sells section:

function wc_forcesells_product_price( $name, $product ) {
    // Only in single product pages
    if( ! is_product() ) return $name;

    // The product name + the product formatted price
    return $name . ' (' . wc_price( wc_get_price_to_display( $product ) ) . ')';
}
add_filter( 'woocommerce_product_title', 'wc_forcesells_product_price', 20, 2 );

以上参考:在 Woocommerce 强制销售旁边添加价格..>

推荐答案

更新以在必要时处理多个产品 ID

Updated to handle multiple product Ids if necessary

请尝试以下操作,为特定产品 ID 显示自定义价格除了:

Try the following that will display for a specific product ID a custom price everywhere except:

  • 关于预订产品以租用网页
  • 当产品类别在购物车中时

该代码还将更改购物车项目中的此自定义产品价格除非特定产品类别在购物车项目中.

The code will also change this custom product price in cart items except when a specific product category is in cart items.

您必须在这些功能中进行设置:

You will have to set in those functions:

  • 此特定产品 ID
  • 要租用的预订产品 ID
  • 附加价格金额
  • 产品类别(Id slug 或名称)

代码:

// Change product ID 87 price everywhere on front end EXCEPT:
// - On booking products to hire pages
// - When a product category is in cart
add_filter( 'woocommerce_get_price_html', 'product_id_87_displayed_price', 10, 2 );
function product_id_87_displayed_price( $price_html, $product ) {
    // Only for product ID 87
    if( in_array( $product->get_id(), array( 87, 2799 ) ) ){ 
        return $price_html; // Exit

    // HERE set booking products IDs to hire in the array
    $product_ids_to_hire = array( 53, 738 );

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

    // HERE set the price additional amount
    $addp = 10;

    // EXCEPT on booking products to hire pages
    if( is_single($product_ids_to_hire) ) return $price_html; // Exit

    // 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 $price_html; // Product category found ==> Exit
    }

    if ( '' !== $product->get_price() && ! $product->is_on_sale() ) {
        $price_html = wc_price( wc_get_price_to_display( $product ) + $addp ) . $product->get_price_suffix();
    }
    return $price_html;
}

// Add custom calculated price to cart item data for product ID 87
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( ! in_array( $product->get_id(), array( 87 ) ) ) 
        return $cart_item_data; // Exit

    // HERE set the price additional amount
    $addp = 10;

    $product = wc_get_product($product_id); // The WC_Product Object
    $price   = (float) $product->get_price(); // The product price

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

    return $cart_item_data;
}

// Set custom calculated price to cart for product ID 87
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 = 'hoodies';

    // 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'] );
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

它也应该适用于您的 wc_forcesells_product_price() 函数.

It should also work with your wc_forcesells_product_price() function.

这篇关于有条件地更改 Woocommerce 中的特定产品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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