add shipping when creating purchase units from cart

This commit is contained in:
David Remer 2020-04-09 12:14:19 +03:00
parent 1b04e79f78
commit 38e105b00c
4 changed files with 39 additions and 2 deletions

View file

@ -2,6 +2,7 @@
"name": "inpsyde/ppcp-api-client",
"type": "inpsyde-module",
"require": {
"ext-json": "*",
"dhii/module-interface": "0.2.x-dev",
"inpsyde/ppcp-session": "dev-master"
},

View file

@ -13,6 +13,18 @@ class AddressFactory
{
}
public function fromWcCustomer(\WC_Customer $customer, string $type = 'shipping') : Address
{
return new Address(
($type === 'shipping') ? $customer->get_shipping_country() : $customer->get_billing_country(),
($type === 'shipping') ? $customer->get_shipping_address_1() : $customer->get_billing_address_1(),
($type === 'shipping') ? $customer->get_shipping_address_2() : $customer->get_billing_address_2(),
($type === 'shipping') ? $customer->get_shipping_state() : $customer->get_billing_state(),
($type === 'shipping') ? $customer->get_shipping_city() : $customer->get_billing_city(),
($type === 'shipping') ? $customer->get_shipping_postcode() : $customer->get_billing_postcode(),
);
}
public function fromWcOrder(\WC_Order $order) : Address
{
return new Address(

View file

@ -190,8 +190,17 @@ class PurchaseUnitFactory
$cart->get_cart_contents()
);
//ToDo: Do we need shipping here?
/**
* // ToDo:
* When we send a shipping information while creating the order, this does
* currently not mean, this address will be shown as default.
*
* Maybe discuss.
*/
$shipping = null;
if (is_a(\WC()->customer, \WC_Customer::class)) {
$shipping = $this->shippingFactory->fromWcCustomer(\WC()->customer);
}
$referenceId = 'default';
$description = '';

View file

@ -15,9 +15,24 @@ class ShippingFactory
$this->addressFactory = $addressFactory;
}
public function fromWcCustomer(\WC_Customer $customer) : Shipping
{
// Replicates the Behavior of \WC_Order::get_formatted_shipping_full_name()
$fullName = sprintf(
_x( '%1$s %2$s', 'full name', 'woocommerce' ),
$customer->get_shipping_first_name(),
$customer->get_shipping_last_name()
);
$address = $this->addressFactory->fromWcCustomer($customer);
return new Shipping(
$fullName,
$address
);
}
public function fromWcOrder(\WC_Order $order) : Shipping
{
$fullName = $order->get_formatted_billing_full_name();
$fullName = $order->get_formatted_shipping_full_name();
$address = $this->addressFactory->fromWcOrder($order);
return new Shipping(
$fullName,