我使用wc_create_order创建订单,一切都很好,除了用户不能在我的下载部分拥有可下载产品的下载链接。
$def_args = array('customer_id' => $user_id, 'status' => 'completed');
$order = wc_create_order( $def_args );
$targs['totals']['subtotal'] = $ord['pay_amount'];
$targs['totals']['total'] = $ord['pay_amount'];
$targs['totals']['subtotal_tax'] = 0;
$targs['totals']['tax'] = 0;
$sku_ = $ord['sku'];
$order->add_product( get_product_by_sku( $sku_ ) , 1, $targs ); //(get_product with id and next is for quantity)
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->set_total( $ord['pay_amount'] );
$order->calculate_totals();
// I took get_product_by_sku function in stackoverflow but I don't remember which question.
function get_product_by_sku( $sku ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $product_id ) return new WC_Product( $product_id );
return null;
}
$ord变量包含有关订单的一些信息。
我是否需要调用函数或类似的东西来使用下载链接进行订购?
我找到了解决方案,在创建订单时,进行状态处理和调用后update_status。
$def_args = array('customer_id' => $user_id, 'status' => 'processing');
...
$order->update_status('completed');
然后用户有她/他的下载链接。