WooCommerce以编程方式创建可下载产品的订单

时间:2015-01-22 11:46:42

标签: wordpress woocommerce orders

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

我是否需要通过下载链接调用函数或其他类似命令?

1 个答案:

答案 0 :(得分:2)

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

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

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