2015-01-22 5 views
2

wc_create_order를 사용하여 주문을 만들고 내 다운로드 섹션에 사용자가 다운로드 할 수있는 제품의 다운로드 링크를 가질 수 없다는 점을 제외하고는 모든 것이 좋습니다.WooCommerce 프로그래밍 방식으로 다운로드 가능한 제품 주문 만들기

$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 변수에는 주문에 대한 정보가 있습니다.

다운로드 링크가있는 주문을하기 위해 함수 등을 호출해야합니까?

답변

2

주문을 만들 때 상태 처리를하고 update_status를 호출 한 후에 해결책을 찾았습니다.

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

그러면 사용자에게 다운로드 링크가 있습니다.

관련 문제