Blocks
2 weeks ago
Concerns
7 months ago
Traits
2 months ago
Wordpress
1 year ago
AbandonedCheckout.php
1 year ago
AbandonedCheckoutProtocol.php
3 years ago
Account.php
11 months ago
AccountPortalSession.php
4 years ago
Activation.php
1 year ago
Affiliation.php
1 year ago
AffiliationProduct.php
1 year ago
AffiliationProtocol.php
2 years ago
AffiliationRequest.php
1 year ago
ApiToken.php
7 months ago
AutoFee.php
7 months ago
AutoFeeProtocol.php
7 months ago
BalanceTransaction.php
1 year ago
Batch.php
1 month ago
Brand.php
5 months ago
BulkAction.php
2 years ago
Bump.php
1 year ago
BundleItem.php
1 month ago
BuyLink.php
2 years ago
CancellationAct.php
1 year ago
CancellationReason.php
3 years ago
Charge.php
11 months ago
Checkout.php
1 month ago
Click.php
1 year ago
Collection.php
4 months ago
CommissionStructure.php
2 years ago
Component.php
6 months ago
Coupon.php
4 months ago
Customer.php
2 months ago
CustomerLink.php
4 years ago
CustomerNotificationProtocol.php
4 years ago
CustomerPortalProtocol.php
1 year ago
DatabaseModel.php
4 months ago
Discount.php
1 year ago
DisplayCurrency.php
1 year ago
Dispute.php
11 months ago
Download.php
4 years ago
Event.php
4 years ago
Export.php
2 years ago
ExternalApiModel.php
4 months ago
Fee.php
1 year ago
Form.php
3 years ago
Fulfillment.php
1 week ago
FulfillmentItem.php
1 year ago
GalleryItem.php
11 months ago
GalleryItemAttachment.php
6 months ago
GalleryItemImageAttachment.php
4 months ago
GalleryItemMedia.php
1 year ago
GalleryItemProductMedia.php
2 months ago
GalleryItemVideoAttachment.php
4 months ago
Import.php
7 months ago
ImportRow.php
3 months ago
IncomingWebhook.php
1 year ago
Integration.php
2 years ago
IntegrationCatalog.php
1 month ago
Invoice.php
1 year ago
License.php
1 year ago
LineItem.php
1 month ago
ManualPaymentMethod.php
1 year ago
Media.php
2 months ago
Model.php
1 month ago
ModelInterface.php
4 years ago
Order.php
2 months ago
OrderProtocol.php
4 years ago
ParcelTemplate.php
1 week ago
PaymentFailure.php
1 year ago
PaymentInstrument.php
3 months ago
PaymentIntent.php
1 year ago
PaymentMethod.php
1 year ago
Payout.php
1 year ago
PayoutGroup.php
2 years ago
Period.php
1 year ago
PlatformFee.php
1 year ago
PortalSession.php
4 years ago
Price.php
8 months ago
Processor.php
1 year ago
Product.php
2 weeks ago
ProductCollection.php
1 year ago
ProductCollectionImport.php
7 months ago
ProductGroup.php
1 year ago
ProductImport.php
7 months ago
ProductMedia.php
6 months ago
Promotion.php
1 year ago
ProvisionalAccount.php
3 months ago
Purchase.php
2 years ago
Referral.php
1 year ago
ReferralItem.php
2 years ago
Refund.php
1 year ago
RefundItem.php
1 year ago
RegisteredWebhook.php
3 months ago
ReturnItem.php
2 years ago
ReturnReason.php
2 years ago
ReturnRequest.php
1 year ago
Review.php
6 months ago
ReviewProtocol.php
6 months ago
RuleSchema.php
7 months ago
ServiceFee.php
1 year ago
Shipment.php
1 week ago
ShippingChoice.php
1 year ago
ShippingMethod.php
3 years ago
ShippingProfile.php
3 years ago
ShippingProtocol.php
3 years ago
ShippingProviderType.php
1 week ago
ShippingRate.php
3 years ago
ShippingZone.php
3 years ago
Statistic.php
4 years ago
Subscription.php
2 months ago
SubscriptionProtocol.php
4 years ago
Swap.php
1 year ago
TaxOverride.php
2 years ago
TaxProtocol.php
4 years ago
TaxRegistration.php
1 year ago
TaxZone.php
4 years ago
Upload.php
4 years ago
Upsell.php
1 year ago
UpsellFunnel.php
1 year ago
User.php
3 weeks ago
Variant.php
1 month ago
VariantOption.php
2 years ago
VariantOptionValue.php
1 year ago
VariantValue.php
2 years ago
VerificationCode.php
3 years ago
Warehouse.php
1 week ago
Webhook.php
1 year ago
WebhookRegistration.php
2 years ago
Shipment.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Traits\HasDates; |
| 6 | |
| 7 | /** |
| 8 | * Shipment model. |
| 9 | */ |
| 10 | class Shipment extends Model { |
| 11 | use HasDates; |
| 12 | |
| 13 | /** |
| 14 | * Rest API endpoint |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $endpoint = 'shipments'; |
| 19 | |
| 20 | /** |
| 21 | * Object name |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $object_name = 'shipment'; |
| 26 | |
| 27 | /** |
| 28 | * Purchase a shipping label. |
| 29 | * |
| 30 | * @param string $id Model id. |
| 31 | * @return $this|false|\WP_Error |
| 32 | */ |
| 33 | protected function purchaseLabel( $id = null ) { |
| 34 | if ( $id ) { |
| 35 | $this->setAttribute( 'id', $id ); |
| 36 | } |
| 37 | |
| 38 | if ( $this->fireModelEvent( 'purchasing_label' ) === false ) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | if ( empty( $this->attributes['id'] ) ) { |
| 43 | return new \WP_Error( 'not_saved', __( 'Please create the shipment.', 'surecart' ) ); |
| 44 | } |
| 45 | |
| 46 | $attributes = $this->attributes; |
| 47 | unset( $attributes['id'] ); |
| 48 | |
| 49 | $purchased = $this->makeRequest( |
| 50 | [ |
| 51 | 'method' => 'PATCH', |
| 52 | 'query' => $this->query, |
| 53 | 'body' => [ |
| 54 | $this->object_name => $attributes, |
| 55 | ], |
| 56 | ], |
| 57 | $this->endpoint . '/' . rawurlencode( $this->attributes['id'] ) . '/purchase/' |
| 58 | ); |
| 59 | |
| 60 | if ( is_wp_error( $purchased ) ) { |
| 61 | return $purchased; |
| 62 | } |
| 63 | |
| 64 | $this->resetAttributes(); |
| 65 | |
| 66 | $this->fill( $purchased ); |
| 67 | |
| 68 | $this->fireModelEvent( 'purchased_label' ); |
| 69 | |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Void a shipping label. |
| 75 | * |
| 76 | * @param string $id Model id. |
| 77 | * @return $this|false|\WP_Error |
| 78 | */ |
| 79 | protected function voidLabel( $id = null ) { |
| 80 | if ( $id ) { |
| 81 | $this->setAttribute( 'id', $id ); |
| 82 | } |
| 83 | |
| 84 | if ( $this->fireModelEvent( 'voiding_label' ) === false ) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if ( empty( $this->attributes['id'] ) ) { |
| 89 | return new \WP_Error( 'not_saved', __( 'Please create the shipment.', 'surecart' ) ); |
| 90 | } |
| 91 | |
| 92 | $voided = $this->makeRequest( |
| 93 | [ |
| 94 | 'method' => 'PATCH', |
| 95 | 'query' => $this->query, |
| 96 | ], |
| 97 | $this->endpoint . '/' . rawurlencode( $this->attributes['id'] ) . '/void/' |
| 98 | ); |
| 99 | |
| 100 | if ( is_wp_error( $voided ) ) { |
| 101 | return $voided; |
| 102 | } |
| 103 | |
| 104 | $this->resetAttributes(); |
| 105 | |
| 106 | $this->fill( $voided ); |
| 107 | |
| 108 | $this->fireModelEvent( 'voided_label' ); |
| 109 | |
| 110 | return $this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Quote shipment rates. |
| 115 | * |
| 116 | * @param string $id Model id. |
| 117 | * @return $this|false|\WP_Error |
| 118 | */ |
| 119 | protected function quoteRates( $id = null ) { |
| 120 | if ( $id ) { |
| 121 | $this->setAttribute( 'id', $id ); |
| 122 | } |
| 123 | |
| 124 | if ( $this->fireModelEvent( 'quoting_rates' ) === false ) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if ( empty( $this->attributes['id'] ) ) { |
| 129 | return new \WP_Error( 'not_saved', __( 'Please create the shipment.', 'surecart' ) ); |
| 130 | } |
| 131 | |
| 132 | $quoted = $this->makeRequest( |
| 133 | [ |
| 134 | 'method' => 'PATCH', |
| 135 | 'query' => $this->query, |
| 136 | ], |
| 137 | $this->endpoint . '/' . rawurlencode( $this->attributes['id'] ) . '/quote/' |
| 138 | ); |
| 139 | |
| 140 | if ( is_wp_error( $quoted ) ) { |
| 141 | return $quoted; |
| 142 | } |
| 143 | |
| 144 | $this->resetAttributes(); |
| 145 | |
| 146 | $this->fill( $quoted ); |
| 147 | |
| 148 | $this->fireModelEvent( 'quoted_rates' ); |
| 149 | |
| 150 | return $this; |
| 151 | } |
| 152 | } |
| 153 |