在Woocommerce单一产品页面上有条件地设置特定的产品价格&大车 [英] Set a specific product price conditionally on Woocommerce single product page & cart

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

问题描述

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

In Woocommerce I would like to alter the price of a specific product (in this case with an ID of 87) on both the single product page and for related cart items.

产品价格需要提高10美元,但只能在单个产品页面上并且仅在外部(这样,内部价格或Woocommerce中设置的价格不会改变)。

The product price needs to be increased by $10 but only on the single product page and only externally (so that the internal price or price set in Woocommerce is not altered).

此外,也应在购物车中更改此价格,但前提是购物车中没有特定类别的产品。

Additionally, this price should also be altered in the cart but only if products from a certain category are NOT also in the cart.

上下文:如果有人购买了此商品产品本身,则应比正常价格多支付10美元。如果某人与某类产品一起购买此商品,则应按正常价格收费。
这实际上是一种倒置的折扣(我不想使用优惠券功能收取额外的费用)。

Context: If someone buys this product on its own, they should be charged $10 above the regular price. If someone buys this item in conjunction with products from a certain category, they should be charged the regular price. This is essentially an inverted discount (I don't want to use coupon functionality to charge extra).

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

For the cart functionality, so far I have this but it's not working - the cart price is coming out at $75 when the current regular/base price is $45. I have no idea where 75 is coming from when it should be 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' );

目前,我用于更改产品页面价格的代码是:

My code for changing the product page price is, at the moment:

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台服务器错误。我做错了什么?欢迎任何帮助。

It is a disaster and causing a 500 server error. What I am doing wrong? Any help is welcome.

参考文献:

更改WooCommerce 3.0版中的购物车价格

https://rudrastyh.com/woocommerce/change- product-prices-in-cart.html

> https://www.skyverge.com/blog/checking-woocommerce-cart-contains-product-category/

推荐答案

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

要使其工作,只需更改单个产品页面上的简单产品价格(不更改归档产品价格,相关产品价格,加价销售和交叉销售)(如果有) rt商品不属于特定的产品类别

To make it work as you want to change the simple products prices only on single product pages (without altering archives product prices, related product prices, upsells and cross-sells) if any cart item doesn't belong to a specific product category.

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

Then you will need all this following code:

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

代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。经过测试,可以正常工作。

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

这篇关于在Woocommerce单一产品页面上有条件地设置特定的产品价格&amp;大车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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