首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有条件地更改Woocommerce中的特定产品价格

有条件地更改Woocommerce中的特定产品价格
EN

Stack Overflow用户
提问于 2018-05-30 08:51:06
回答 1查看 794关注 0票数 3

我想在Woocommerce中修改一个特定的产品,除了在那些情况下之外,以编程方式添加到其原始的$10,

特定产品页面上的

  • 属于某个类别"C“的可预订产品(Woocommerce Bookings)。
  • 如果有任何购物车项目属于该类别”C“。

在此之前,我一直在使用this answer code to one of my questions。但我需要显示这个特定的产品改变了价格,除了在产品页面上,这是可预订的产品,在前端。

我也有这个代码,显示原力销售价格旁边的标题。这仍然应该在该产品页面上显示特定的产品原始价格,这些产品是强制销售部分中的可预订产品:

代码语言:javascript
复制
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 );

上面的参考:Add price beside Woocommerce Force Sells

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-30 18:34:04

已更新,可在必要时处理多个产品if

尝试以下操作,可以在除之外的所有地方显示特定产品ID的自定义价格

当产品类别位于cart中时,

  • On booking products to hire pages

当特定产品类别位于cart items中时,代码还将在cart items 中更改此自定义产品价格(除外)。

您必须在这些函数中设置:

代码:

代码语言:javascript
复制
// 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文件中。经过测试,效果良好。

它也应该和你的wc_forcesells_product_price()函数一起工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50594920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档