Skip to content

Commit

Permalink
CIVIWOO-10: Convert to Order API for line item support
Browse files Browse the repository at this point in the history
  • Loading branch information
agileware-pengyi authored and agileware-fj committed Jan 20, 2021
1 parent 09566d6 commit cfcc0f4
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions includes/class-woocommerce-civicrm-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,29 +606,26 @@ public function add_contribution( $cid, &$order ) {

$payment_instrument = $this->map_payment_instrument( $order->get_payment_method() );
$source = $this->generate_source( $order );
$params = array(
$params = [
'contact_id' => $cid,
'total_amount' => $rounded_subtotal,
// Need to be set in admin page
'contribution_type_id' => $default_contribution_type_id,
'financial_type_id' => $default_contribution_type_id,
'payment_instrument_id' => $payment_instrument,
'non_deductible_amount' => number_format( 0, 2, $decimal_separator, $thousand_separator ),
'fee_amount' => number_format( 0, 2, $decimal_separator, $thousand_separator ),
'trxn_id' => $txn_id,
'invoice_id' => $invoice_id,
'source' => $source,
'receive_date' => $order_paid_date,
'contribution_status_id' => $contribution_status_id,
// 'contribution_status_id' => $contribution_status_id,
'note' => $this->create_detail_string( $items ),
"$sales_tax_field_id" => $sales_tax,
"$shipping_cost_field_id" => $shipping_cost,
'campaign_id' => $campaign_name,
);
];

// If the order has VAT (Tax) use VAT Fnancial type
if( $sales_tax != 0 ){
// Need to be set in admin page
$params['contribution_type_id'] = $contribution_type_vat_id;
$params['financial_type_id'] = $contribution_type_vat_id;
}

/**
Expand All @@ -638,8 +635,12 @@ public function add_contribution( $cid, &$order ) {

if(count($items)){
$financial_types = array();
$params['api.line_item.create'] = array();
$params['skipLineItem'] = 1;
$params['line_items'] = [
[
"params" => [],
'line_item' => []
]
];
foreach( $items as $item ){
$custom_contribution_type = get_post_meta($item['product_id'], '_civicrm_contribution_type', true);
if($custom_contribution_type === 'exclude')
Expand All @@ -651,10 +652,8 @@ public function add_contribution( $cid, &$order ) {
if ($item['qty'] == 0) {
$item['qty'] = 1;
}
$params['api.line_item.create'][] = array(
'price_field_id' => array(
'0' => 3,
),
$params['line_items'][0]['line_item'][] = array(
'price_field_id' => '1',
'qty' => $item['qty'],
'line_total' => number_format( $item['line_total'], 2, $decimal_separator, $thousand_separator ),
'unit_price' => number_format( $item['line_total'] / $item['qty'], 2, $decimal_separator, $thousand_separator ),
Expand All @@ -664,7 +663,7 @@ public function add_contribution( $cid, &$order ) {
$financial_types[$custom_contribution_type] = $custom_contribution_type;
}
if(count($financial_types)==1){
$params['contribution_type_id'] = $custom_contribution_type;
$params['financial_type_id'] = $custom_contribution_type;
}
}

Expand All @@ -677,7 +676,7 @@ public function add_contribution( $cid, &$order ) {
* @since 2.0
* @param array $params The params to be passsed to the API
*/
$contribution = civicrm_api3( 'Contribution', 'create', apply_filters( 'woocommerce_civicrm_contribution_create_params', $params ) );
$contribution = civicrm_api3( 'Order', 'create', apply_filters( 'woocommerce_civicrm_contribution_create_params', $params, $order ) );
if(isset($contribution['id']) && $contribution['id']){
// Adds order note in reference to the created contribution
$order->add_order_note(sprintf(__('Contribution %s has been created in CiviCRM', 'woocommerce-civicrm'),
Expand All @@ -701,6 +700,8 @@ public function add_contribution( $cid, &$order ) {
} catch ( CiviCRM_API3_Exception $e ) {
// Log the error, but continue.
CRM_Core_Error::debug_log_message( __( 'Not able to add contribution', 'woocommerce-civicrm' ) );
CRM_Core_Error::debug_log_message( __( $e->getMessage(), 'woocommerce-civicrm' ) );
CRM_Core_Error::debug_log_message( __( $e->getTraceAsString(), 'woocommerce-civicrm' ) );
}


Expand Down

0 comments on commit cfcc0f4

Please sign in to comment.