Skip to content

Commit

Permalink
Set customer from WooCommerce subscription order.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Jun 7, 2023
1 parent 3d40213 commit bb97664
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
27 changes: 8 additions & 19 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,23 +516,14 @@ private function new_pronamic_payment_from_wc_order( WC_Order $order ) {

$description = strtr( $this->payment_description, $replacements );

// Contact.
$contact_name = new ContactName();
$contact_name->set_first_name( WooCommerce::get_billing_first_name( $order ) );
$contact_name->set_last_name( WooCommerce::get_billing_last_name( $order ) );

$customer = new Customer();
$customer->set_name( $contact_name );
$customer->set_email( WooCommerce::get_billing_email( $order ) );
$customer->set_phone( WooCommerce::get_billing_phone( $order ) );
$customer->set_user_id( $order->get_user_id() );

// Company name.
$company_name = WooCommerce::get_billing_company( $order );

if ( ! empty( $company_name ) ) {
$customer->set_company_name( $company_name );
}
// Order helper.
$order_helper = new OrderHelper( $order );

// Contact name.
$contact_name = $order_helper->get_contact_name();

// Customer.
$customer = $order_helper->get_customer();

// Customer gender.
$gender = null;
Expand Down Expand Up @@ -708,8 +699,6 @@ private function new_pronamic_payment_from_wc_order( WC_Order $order ) {
);

// Payment lines and order items.
$order_helper = new OrderHelper( $order );

$payment->lines = $order_helper->get_lines();

return $payment;
Expand Down
41 changes: 41 additions & 0 deletions src/OrderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace Pronamic\WordPress\Pay\Extensions\WooCommerce;

use Pronamic\WordPress\Money\TaxedMoney;
use Pronamic\WordPress\Pay\Customer;
use Pronamic\WordPress\Pay\ContactName;
use Pronamic\WordPress\Pay\Payments\PaymentLines;
use Pronamic\WordPress\Pay\Payments\PaymentLineType;
use WC_Order;
Expand All @@ -33,6 +35,45 @@ public function __construct( WC_Order $woocommerce_order ) {
$this->woocommerce_order = $woocommerce_order;
}

/**
* Get contact name.
*
* @return ContactName
*/
public function get_contact_name() {
$order = $this->woocommerce_order;

$contact_name = new ContactName();
$contact_name->set_first_name( WooCommerce::get_billing_first_name( $order ) );
$contact_name->set_last_name( WooCommerce::get_billing_last_name( $order ) );

return $contact_name;
}

/**
* Get customer.
*
* @return Customer
*/
public function get_customer() {
$order = $this->woocommerce_order;

$customer = new Customer();
$customer->set_name( $this->get_contact_name() );
$customer->set_email( WooCommerce::get_billing_email( $order ) );
$customer->set_phone( WooCommerce::get_billing_phone( $order ) );
$customer->set_user_id( $order->get_user_id() );

// Company name.
$company_name = WooCommerce::get_billing_company( $order );

if ( ! empty( $company_name ) ) {
$customer->set_company_name( $company_name );
}

return $customer;
}

/**
* Get lines.
*
Expand Down
8 changes: 6 additions & 2 deletions src/SubscriptionUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public function update_pronamic_subscription() {
)
);

// Order helper.
$order_helper = new OrderHelper( $woocommerce_subscription );

// Customer.
$pronamic_subscription->set_customer( $order_helper->get_customer() );

// Phases.
$pronamic_subscription->set_phases( [] );

Expand Down Expand Up @@ -180,8 +186,6 @@ public function update_pronamic_subscription() {
$pronamic_subscription->add_phase( $regular_phase );

// Lines
$order_helper = new OrderHelper( $woocommerce_subscription );

$pronamic_subscription->lines = $order_helper->get_lines();
}

Expand Down

0 comments on commit bb97664

Please sign in to comment.