doujingya1166 2016-09-19 10:35
浏览 24
已采纳

以编程方式更新WooCommerce订阅订单行项目

I am trying to allow users to update line items of their subscriptions from their my account panel. I am able to get their subscription from subscription id and show them update form. Right now I show them with product items in their subscription that I got through

$subscription = wcs_get_subscription($_GET['subscription']);
$subscription_items = $subscription->get_items();

What I am trying to do is allow users to update quantities of their product. So If they update quantity I want to update the subscription's item quantity so that future orders are generated with updated quantity. I saw there is update_product method in WC_Abstract_Order class. I think this can be used but I am confused on this note in comments:

* Update a line item for the order.
*
* Note this does not update order totals.

Do I need to recalculate totals when I use this? Also I needed to remove line item when quantity was 0. Is that possible?

As I do not see a remove item method.

Thanks

  • 写回答

2条回答 默认 最新

  • duansai1314 2016-09-22 09:37
    关注

    So I was able to make this work doing following.

    1. Remove all order items from subscription object.
    2. Run through the $_POST to get changed quantity
    3. Add product to subscription again.

    note: I am using custom field price_level as it was dynamically priced during subscription and we wanted to use it so that price was same as when they subscribed.

        //remove product items
        $subscription->remove_order_items('line_item');
        //add product item again
        foreach($_POST['quantity'] as $product_id => $qty) {
            if($qty > 0) {
                //we will need to set dynamic prices based on cusotm field
                $price_level = get_field('coffee_price_level', $subscription->id);
                //Get the product
                $product = wc_get_product($product_id);
                //set the price
                $product->set_price(floatval($price_level));
                $tax = ($product->get_price_including_tax()-$product->get_price_excluding_tax())*$qty;
                //subscription item price level
                $subscription->add_product($product, $qty, array(
                    'totals' => array(
                        'subtotal'     => $product->get_price(),
                        'subtotal_tax' => $tax,
                        'total'        => $product->get_price(),
                        'tax'          => $tax,
                        'tax_data'     => array( 'subtotal' => array(1=>$tax), 'total' => array(1=>$tax) )
                    )
                ));
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系