2

我使用 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 变量有一些关于订单的信息。

我是否需要调用一个函数或类似的东西来通过下载链接订购?

4

1 回答 1

2

我找到了解决方案,在创建订单时,进行状态处理,然后调用 update_status。

$def_args = array('customer_id' => $user_id, 'status' => 'processing');
...
$order->update_status('completed');

然后用户有她/他的下载链接。

于 2015-01-22T16:20:29.487 回答