php - 在表单提交时以编程方式创建订单 OVERIDE PRODUCT PRICE - WooCommerce
问题描述
我有一个代码,我想在提交表单时以编程方式在 WooCommerce 中创建订单。
我希望我的代码确定产品价格 - 所以覆盖产品的默认价格。
这可能吗?
不知道我的代码哪里出错了:
// set some variables
$user_id =rgar( $entry, '97' );
$product_id = rgar( $entry, '71' );
$quantity = rgar( $entry, '73' );
$price = rgar( $entry, '90' );
$note = rgar( $entry, '53' );
$product = wc_get_product($product_id);
$address = array( 'first_name' => rgar( $entry, '98' ), 'last_name' => rgar( $entry, '99' ), 'company' => rgar( $entry, '' ), 'email' => rgar( $entry, '83' ), 'phone' => rgar( $entry, '84' ), 'address_1' => rgar( $entry, '88.1' ), 'address_2' => rgar( $entry, '88.2' ), 'city' => rgar( $entry, '88.3' ), 'state' => rgar( $entry, '88.4' ), 'postcode' => rgar( $entry, '88.5' ), 'country' => rgar( $entry, '88.6' ), );
// Create the order object
$order = wc_create_order();
$order->set_customer_id( $user_id );
$order->add_product( wc_get_product($product_id), $quantity, $prices);
foreach ($order->get_items() as $item_key => $item ) {
$item->add_meta_data( 'Label', $note, true );
}
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status( 'pending payment', 'pending', TRUE);
$order->add_order_note( $note );
$coupon_code = rgar( $entry, '105' ); $order->apply_coupon($coupon_code);
break;
}}
“90”是我的表单的字段 ID,其中包含我想在产品订单中使用的价格。
解决方案
用途:WC_Product::set_price()
– 设置产品的有效价格。
https://woocommerce.wp-a2z.org/oik_api/wc_productset_price/
下一行
$order->add_product( wc_get_product($product_id), $quantity, $prices);
将是
$order->add_product( $product, $quantity );
因为你已经用过wc_get_product
$product = wc_get_product($product_id);
然后你得到
// set some variables
$user_id =rgar( $entry, '97' );
$product_id = rgar( $entry, '71' );
$quantity = rgar( $entry, '73' );
$price = rgar( $entry, '90' );
$note = rgar( $entry, '53' );
$product = wc_get_product($product_id);
// Set price
$product->set_price( $price );
$address = array(
'first_name' => rgar( $entry, '98' ),
'last_name' => rgar( $entry, '99' ),
'company' => rgar( $entry, '' ),
'email' => rgar( $entry, '83' ),
'phone' => rgar( $entry, '84' ),
'address_1' => rgar( $entry, '88.1' ),
'address_2' => rgar( $entry, '88.2' ),
'city' => rgar( $entry, '88.3' ),
'state' => rgar( $entry, '88.4' ),
'postcode' => rgar( $entry, '88.5' ),
'country' => rgar( $entry, '88.6' ),
);
// Create the order object
$order = wc_create_order();
$order->set_customer_id( $user_id );
// add product to order
$order->add_product( $product, $quantity );
foreach ($order->get_items() as $item_key => $item ) {
$item->add_meta_data( 'Label', $note, true );
}
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status( 'pending payment', 'pending', TRUE);
$order->add_order_note( $note );
$coupon_code = rgar( $entry, '105' );
$order->apply_coupon($coupon_code);
推荐阅读
- debugging - Windbg 条件内存搜索
- laravel - 在 laravel 中更新后如何删除旧图像?
- php - 上传前如何在PHP中获取宽度和高度?
- c# - 使用接口的委托作为参数类型时逆变无效
- d3.js - 从 D3 中的多级对象创建图表
- f# - 如何将 CsvHelper.CsvWriter 与 F# 选项类型一起使用?
- jupyter-notebook - 如何在不同架构的计算机上创建相同的 conda 环境?
- php - 如何从 url $GET 多个具有相同名称的值?
- android - Android Recycler E/AndroidRuntime:致命异常:主要
- lucene - Lucene - 构建多个模糊选择器单项但条件不同