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

Php 以编程方式更新订单行项目,php,wordpress,woocommerce,subscriptions,orders,Php,Wordpress,Woocommerce,Subscriptions,Orders,我试图允许用户从“我的帐户”面板更新其订阅的行项目。我可以从订阅id获取他们的订阅,并向他们显示更新表单。现在我向他们展示了我通过订阅获得的产品项目 $subscription = wcs_get_subscription($_GET['subscription']); $subscription_items = $subscription->get_items(); 我想做的是允许用户更新他们产品的数量。因此,如果他们更新数量,我想更新订阅的项目数量,以便使用更新的数量生成未来订单。

我试图允许用户从“我的帐户”面板更新其订阅的行项目。我可以从订阅id获取他们的订阅,并向他们显示更新表单。现在我向他们展示了我通过订阅获得的产品项目

$subscription = wcs_get_subscription($_GET['subscription']);
$subscription_items = $subscription->get_items();
我想做的是允许用户更新他们产品的数量。因此,如果他们更新数量,我想更新订阅的项目数量,以便使用更新的数量生成未来订单。 我在
WC\u Abstract\u Order
类中看到了update\u product方法。我认为这是可以使用的,但我在评论中对此感到困惑:

* Update a line item for the order.
*
* Note this does not update order totals.
使用此选项时是否需要重新计算总计? 当数量为0时,我还需要删除行项目。可能吗

因为我没有看到RemoveItem方法


谢谢

通常在woocommerce中,当付款后生成订单时(我的意思是结帐=>谢谢),您不能再编辑订单详细信息。更新数量、删除/添加项目是WC购物车方法

使用subscriptions插件,对于每个初始的shop_order post类型,都会同时生成一个初始的shop_subscription post类型和一个计划的action post类型(post ID会相互跟随)。例如:

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
您可以在数据库
wp\u posts
表中看到这一点

我们本可以在
表中使用相应的
函数来更新
'post\u id'=>413'
的总数

但这不起作用,因为下一次预定的订阅付款由支付网关(paypal或其他)处理,您不能更改该订阅的任何金额。

当预定订阅到期时,WooCommerce将仅生成此支付网关触发的新订单


唯一的方法是取消订阅并从一开始就生成一个新流程…

通常在woocommerce中,当付款后生成订单时(我的意思是结帐=>谢谢),您不能再编辑订单详细信息。更新数量、删除/添加项目是WC购物车方法

使用subscriptions插件,对于每个初始的shop_order post类型,都会同时生成一个初始的shop_subscription post类型和一个计划的action post类型(post ID会相互跟随)。例如:

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
您可以在数据库
wp\u posts
表中看到这一点

我们本可以在
表中使用相应的
函数来更新
'post\u id'=>413'
的总数

但这不起作用,因为下一次预定的订阅付款由支付网关(paypal或其他)处理,您不能更改该订阅的任何金额。

当预定订阅到期时,WooCommerce将仅生成此支付网关触发的新订单


唯一的方法是取消订阅并从一开始就生成一个新的进程…

因此我能够完成以下工作

  • 从订阅对象中删除所有订单项
  • 运行$\u POST以获取更改的数量
  • 再次向订阅添加产品
  • 注意:我使用自定义字段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) )
                    )
                ));
            }
        }
    

    所以我可以做以下的工作

  • 从订阅对象中删除所有订单项
  • 运行$\u POST以获取更改的数量
  • 再次向订阅添加产品
  • 注意:我使用自定义字段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) )
                    )
                ));
            }
        }
    

    谢谢你的回复。我看到在我们查看订阅时,有可能在后端编辑订阅产品数量。这些是如何处理的?当我们编辑数量时,他们会创建新的订阅吗?@Yalamber您不能真正编辑它,因为您正在更改金额,正如我所说的,一旦订阅完成,WooCommerce就不会处理预定的付款。这由支付网关或银行(paypal或其他)处理。即使您在woocommerce中更改金额,也不会在支付网关或银行上更新该金额…并且支付将保持与第一张订单中定义的金额相同。哦,我现在得到了它,我实际上不需要更新支付网关上的金额。因为我将限制总数量的产品相同,每个产品将有相同的价格。我只需要提供在不同产品之间更改数量的功能。因此,我想出于我的需要,可以更新元字段,对吗?请不要将此设置为个人设置。我只是提供了对我有用的东西。所以我选择了我的答案,并对你的答案投了赞成票。因为它帮助我查看数据库并给出了相关的提示。谢谢你的回复。我看到在我们查看订阅时,有可能在后端编辑订阅产品数量。这些是如何处理的?当我们编辑数量时,他们会创建新的订阅吗?@Yalamber您不能真正编辑它,因为您正在更改金额,正如我所说的,一旦订阅完成,WooCommerce就不会处理预定的付款。这由支付网关或银行(paypal或其他)处理。即使您在woocommerce中更改了金额,也不会在支付网关或银行上更新,并且支付将保持不变