在 WooCommerce 中更改虚拟、可下载、免费或延期交货产品的订单状态 [英] Change order status for virtual, downloadable, free or backorder products in WooCommerce
问题描述
我尝试通过 +1 检查插件来稍微修改 定位这里因此,对于所有 Virtual Downloadable Free (price=0,00) &在延期交货产品上,我希望 Woocommerce 将订单状态设置为正在处理"
我用下面的代码得到的结果 - Woocommerce 设置订单状态待付款"是否有任何想法如何将其切换到正在处理":
add_action('woocommerce_checkout_order_processed', 'handmade_woocommerce_order');函数 handmade_woocommerce_order( $order_id ){$order = wc_get_order($order_id);foreach ($order->get_items() as $item_key => $item_values):$product_id = $item_values->get_product_id();//获取商品id//获取产品设置,即虚拟$virtual_product = get_post_meta($product_id,'_virtual',true);$downloadable_product = get_post_meta($product_id,'_downloadable',true);$product_backordered=backorders_allowed($product_id,'_backorders',true);$price = get_post_meta($product_id,'_regular_price',true);$virtuald=get_option('hmade_vd');if($virtuald=='yes' && $downloadable_product=='yes' && $virtual_product=='yes' && $product_backordered=='yes'){如果($价格=='0.00'){$order->update_status('处理');}}Endforeach;}
注意 1:改用
woocommerce_thankyou
钩子
<块引用>
注意 2:要知道产品是虚拟的还是可下载的,您可以使用以下函数 $product->is_virtual();
和 $product->is_downloadable();
对面 get_post_meta();
更多信息:https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html
<块引用>
注意3:最好不要在foreach循环中执行操作,之后使用检查
function handmade_woocommerce_order( $order_id ) {如果(!$order_id)返回;//获取订单$order = wc_get_order( $order_id );//获取订单商品 = 订单中的每个产品$items = $order->get_items();//设置变量$发现=假;foreach ( $items as $item ) {//获取商品id$product = wc_get_product( $item['product_id'] );//是虚拟的$is_virtual = $product->is_virtual();//Is_downloadable$is_downloadable = $product->is_downloadable();//允许缺货$backorders_allowed = $product->backorders_allowed();if( $is_virtual && $is_downloadable && $backorders_allowed ) {$发现=真;//真,中断循环休息;}}//真的如果($找到){$order->update_status('处理');}}add_action('woocommerce_thankyou', 'handmade_woocommerce_order', 10, 1);
i try to slightly modify with +1 check for plugin located here So , for all Virtual Downloadable Free (price=0,00) & on Backorder products I want Woocommerce to set order status 'Processing'
the result I have with code below - Woocommerce to set order status 'Pending Payment' Are there any ideas how to switch it to 'Processing':
add_action('woocommerce_checkout_order_processed', 'handmade_woocommerce_order');
function handmade_woocommerce_order( $order_id )
{
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_key => $item_values):
$product_id = $item_values->get_product_id(); //get product id
//get prodct settings i.e virtual
$virtual_product = get_post_meta($product_id,'_virtual',true);
$downloadable_product = get_post_meta($product_id,'_downloadable',true);
$product_backordered=backorders_allowed($product_id,'_backorders',true);
$price = get_post_meta($product_id,'_regular_price',true);
$virtuald=get_option('hmade_vd');
if($virtuald=='yes' && $downloadable_product=='yes' && $virtual_product=='yes' && $product_backordered=='yes')
{
if($price=='0.00')
{
$order->update_status( 'processing' );
}
}
endforeach;
}
Note 1: use
woocommerce_thankyou
hook instead
Note 2: To know whether a product is virtual or downloadable you can use the following functions
$product->is_virtual();
and$product->is_downloadable();
oppositeget_post_meta();
More info: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html
Note 3: It is better not to perform operations in a foreach loop, use a check afterwards
function handmade_woocommerce_order( $order_id ) {
if( ! $order_id ) return;
// Get order
$order = wc_get_order( $order_id );
// get order items = each product in the order
$items = $order->get_items();
// Set variable
$found = false;
foreach ( $items as $item ) {
// Get product id
$product = wc_get_product( $item['product_id'] );
// Is virtual
$is_virtual = $product->is_virtual();
// Is_downloadable
$is_downloadable = $product->is_downloadable();
// Backorders allowed
$backorders_allowed = $product->backorders_allowed();
if( $is_virtual && $is_downloadable && $backorders_allowed ) {
$found = true;
// true, break loop
break;
}
}
// true
if( $found ) {
$order->update_status( 'processing' );
}
}
add_action('woocommerce_thankyou', 'handmade_woocommerce_order', 10, 1 );
这篇关于在 WooCommerce 中更改虚拟、可下载、免费或延期交货产品的订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!