简体   繁体   English

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

[英]Updating WooCommerce Subscriptions Order line Items programmatically

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.我能够从订阅 ID 获取他们的订阅并向他们展示更新表单。 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:我看到WC_Abstract_Order class 中有 update_product 方法。我认为这个可以使用,但我对评论中的这个注释感到困惑:

* 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?我还需要在数量为 0 时删除订单项。这可能吗?

As I do not see a remove item method.因为我没有看到删除项目的方法。

Thanks谢谢

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 通过$ _POST运行以获取更改数量
  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. 注意:我使用的是自定义字段price_level,因为它在订阅期间是动态定价的,因此我们想使用它,以便价格与订阅时的价格相同。

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

Normally in woocommerce when an order is generated after payment (I mean checkout => thankyou), you can't edit anymore the order details. 通常在woocommerce中,付款后生成订单(我的意思是结帐=>谢谢),您无法再编辑订单详细信息。 Updating quantities, removing/adding items are WC cart methods. 更新数量,删除/添加项目是WC购物车方法。

With subscriptions plugin, with each initial shop_order post-type, there is an initial shop_subscription post-type and a scheduled-action post-type generated at the same time (and post IDs follow each other). 使用subscriptions插件时,每种初始shop_order帖子类型都可以同时生成一个初始shop_subscription帖子类型和计划动作帖子类型(并且帖子ID彼此跟随)。 For example: 例如:

Initial (post type) 'shop_order' -> ID is        412
Initial (post type) 'shop_subscription' -> ID is 413  (and 'post_parent': 412)
Initial (post type) 'scheduled-action' -> ID is  414

You can see that in your database wp_posts table. 您可以在数据库wp_posts表中看到它。

We could have updated the total in wp_postmeta table for 'post_id' => '413' with the corresponding meta_key '_order_total' using in this case the update_post_meta() function. 我们可以使用update_post_meta()函数在wp_postmeta表中使用相应的meta_key '_order_total'更新了'post_id' => '413'meta_key

BUT this will not work, because the next scheduled subscription payment is handled by the payment gateway (paypal or others) and you can't change any amount of that subscription. 但是这将不起作用,因为下一次预定的订阅付款是由付款网关 (贝宝或其他)处理的,并且您不能更改该订阅的任何金额。

WooCommerce will just generate the new order triggered by this payment gateway, when a scheduled subscription is due. 当计划的订阅到期时,WooCommerce只会生成由该支付网关触发的新订单。

The only way is to cancel the subscription and generate a new process from the beginning… 唯一的方法是取消订阅并从头开始生成新的流程…

Its an old thread but in case its of any help to other people.它是一个旧线程,但万一它对其他人有任何帮助。 You can just use the $subscription->remove_item function and then $subscription->save.您可以只使用 $subscription->remove_item function 然后 $subscription->save。

In your case:在你的情况下:

$item_ids_to_remove = array();
// 1. get the item ids that should be removed
foreach ( $subscription->get_items() as $item ) {
    if ($item->get_quantity() == 0) {
        $item_ids_to_remove[] = $item->get_id();
    }
}
// 2. remove the items from the subscription
foreach ($item_ids_to_remove as $item_id_to_remove) {
    $subscription->remove_item($item_id_to_remove);
}
// 3. recalculate totals (incl. tax) and save
$subscription->calculate_totals();
$subscription->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM