Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to Order API for line item support #33

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like to know why this line is commented out!!.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably actually should be hard-coded to 'Pending' and then a payment recorded for completed WooCommerce orders, as per what the Order API expects.

'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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please replace the hardcoded ID with api call?

'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 ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 👍

Copy link

@ralphyke ralphyke Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have put the new code in and it seems to fix the problem of the financial contribution link, but the plugin also seems to be trying to create a membership which is failing as follows:

Error Details

An error of type E_ERROR was caused in line 133 of the file /home/rjakn8hl475q/public_html/shop/wp-content/plugins/civicrm/civicrm/api/api.php. Error message: Uncaught CiviCRM_API3_Exception: [constraint violation: DB Error: constraint violation thrown

Maybe it is because I have set the WooCommerce product to set to Contribution Type = Membership in Civi although it has worked in the past. Can you tell me what the code is trying to do with creating a membership record in Civi because I can't see anywhere in WooCommerce where this is set or how it is triggered?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ralphyke yes, setting the Financial Type to Member Dues seems to (obscurely) attempt to create a Membership, I think I will strip that out for now as it's not the best implementation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if you can strip that out and then I can put in the new version again, that would be great

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