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

Dynamic cart item pricing not working on orders in WooCommerce 3.0+(动态购物车项目定价不适用于 WooCommerce 3.0+ 中的订单)
本文介绍了动态购物车项目定价不适用于 WooCommerce 3.0+ 中的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用的是 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…

这篇关于动态购物车项目定价不适用于 WooCommerce 3.0+ 中的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)