【问题标题】:Set a specific product price conditionally on Woocommerce single product page & cart在 Woocommerce 单品页面和购物车上有条件地设置特定的产品价格
【发布时间】:2018-11-07 12:56:13
【问题描述】:

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

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

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

背景:如果有人单独购买此产品,则应向他们收取高于正常价格 10 美元的费用。如果有人与某个类别的产品一起购买此商品,则应向他们收取正常价格。 这本质上是一种反向折扣(我不想使用优惠券功能收取额外费用)。

对于购物车功能,到目前为止,我有这个,但它不起作用 - 当当前的常规/基本价格为 45 美元时,购物车价格为 75 美元。我不知道 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 服务器错误。我做错了什么?欢迎任何帮助。

参考资料:

Change cart item prices in WooCommerce version 3.0

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

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

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy price


    【解决方案1】:

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

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 这会改变所有简单产品的价格,对吗?我只想更改一种特定产品的价格(ID 为 87)。并且当商品 id 87 添加到购物车时,如果类别 X 中还有其他商品,则会添加正常价格。但如果类别 X 不在购物车中,则会显示额外的 10 美元价格。
    • 对不起,我有一种导致失语症的残疾(我把类似的词弄混了)。 “更改单个产品的价格”应该是“更改特定产品的价格”,这就是为什么在我的代码中有 $targeted_product_id = 87;
    • 问题是我不明白为什么我需要完全改变我的问题,因为我用错了一个词,这意味着我的问题被误解了。我真的很感谢你的时间,但这对我真正需要的东西没有任何帮助,再次问同样的问题似乎是多余的。
    • 非常感谢!完美运行。
    • 我愚蠢地没有考虑在存档页面(又名商店页面和类别页面)上更改产品(ID 为 87)的价格。这很容易改变以包括那些,还是我应该创建一个新问题来调整上述内容来做到这一点?再次感谢! :)
    【解决方案2】:

    如果您想在单个产品页面上更改产品价格。 请按照此代码进行操作,我已添加我的产品 ID“99”:

    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' );
    

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2018-07-31
      • 2017-12-10
      • 2020-12-13
      • 1970-01-01
      • 2017-04-06
      相关资源
      最近更新 更多