动态购物车项目定价不适用于 WooCommerce 3.0+ 中的订单

2021-12-22 00:00:00 php wordpress woocommerce cart price

我使用的是 WooCommerce 3.0+ 并且我已经在某个页面上设置了产品价格.

I am using WooCommerce 3.0+ and I have set the product price on a certain page.

       $regular_price = get_post_meta( $_product->id, '_regular_price', true);
      $buyback_percentage = get_post_meta( $_product->id, '_goldpricelive_buy_back', true);
      $fixed_amount = get_post_meta( $_product->id, '_goldpricelive_fixed_amount', true);
      $markedup_price = get_post_meta( $_product->id, '_goldpricelive_markup', true);
      $buyback_price = ($regular_price - $fixed_amount)/(1 + $markedup_price/100)  * (1-$buyback_percentage/100);
      $_product->set_price($buyback_price);

我的购物车上的价格正在更新,但是当我点击提交订单时,订单对象似乎没有得到我设置的价格.它需要原产地产品价格.

The price is updating on my cart but when I click on to submit my order, Order object doesn't seem to get the price I set. It takes the origin product price.

知道如何实现这一点吗?

Any idea on how I can accomplish this?

谢谢

推荐答案

更新为 get_price() 方法……

Updated with get_price() method …

您应该在此自定义挂钩函数、您的产品 ID 或产品 ID 数组中使用 woocommerce_before_calculate_totals 操作挂钩设置.
然后,您可以为每个人进行自定义计算以设置将在购物车、结帐和提交订单后设置的自定义价格.

You should use woocommerce_before_calculate_totals action hook setting inside this custom hooked function, your products IDs or an array of product IDs.
Then for each of them you can make a custom calculation to set a custom price that will be set on Cart, checkout and after submitting in the order.

这是在 WooCommerce 3.0+ 版上测试的功能代码:

Here is that functional code tested on WooCommerce version 3.0+:

add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);
function adding_custom_price( $cart_obj ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Set below your targeted individual products IDs or arrays of product IDs
    $target_product_id = 53;
    $target_product_ids_arr = array(22, 56, 81);

    foreach ( $cart_obj->get_cart() as  $cart_item ) {
        // The corresponding product ID
        $product_id = $cart_item['product_id'];

        // For a single product ID
        if($product_id == $target_product_id){
            // Custom calculation
            $price = $cart_item['data']->get_price() + 50;
            $cart_item['data']->set_price( floatval($price) );
        } 

        // For an array of product IDs 
        elseif( in_array( $product_id, $target_product_ids_arr ) ){
            // Custom calculation
            $price = $cart_item['data']->get_price() + 30;
            $cart_item['data']->set_price( floatval($price) );
        }
    }
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

然后,您可以使用 get_post_meta() 函数轻松地将我的虚假计算中的固定值替换为您的产品动态值,就像在您的代码中一样,因为您拥有 $product_id对于每个购物车项目......

Then you can easily replace the fixed values in my fake calculations by your product dynamic values with that with get_post_meta() function just like in your code as you have the $product_id for each cart item…

相关文章