PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.4.0
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.4.0
2.4.1 2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 All 68 releases
better-payment / includes / Classes / ProLayoutProducts.php

ProLayoutProducts.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.4.0, at includes/Classes/ProLayoutProducts.php

150 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Classes;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * The product list Pro's Payment Form layout 6 shows beside the form.
11 *
12 * Only Pro renders layout 6, so this copy exists for Pro releases that do not
13 * build the list themselves. `partials/layout-vars.php` calls it while
14 * lite_builds() is true; once Pro declares `layout_6_product_details` on
15 * `better_payment/pro_provides`, Pro's own partial builds the list and Lite skips
16 * this. A later Lite release deletes the class.
17 *
18 * Pro's `Frontend\Layout6Products` carries identical woocommerce() and
19 * fluentcart() bodies, and Pro's Layout6ProductsParityTest fails when they drift.
20 *
21 * What a layout-6 form may charge is not decided here: PaymentRequestGuard prices
22 * the products server-side. This only feeds the order summary.
23 *
24 * @since 2.4.0
25 */
26 class ProLayoutProducts {
27
28 /**
29 * Feature slug Pro declares once it builds the list itself.
30 */
31 const FEATURE = 'layout_6_product_details';
32
33 /**
34 * Whether Lite's partial still builds the layout-6 product list.
35 *
36 * Only for an active Pro that does not build it itself; never on a free
37 * install, which renders no layout 6.
38 *
39 * @return bool
40 */
41 public static function lite_builds(): bool {
42 return ProCompatibility::lite_copy_applies( self::FEATURE );
43 }
44
45 /**
46 * Product details and price total for WooCommerce product ids.
47 *
48 * @param mixed $product_ids WooCommerce product ids.
49 * @return array{details: array, total: float|int}
50 */
51 public static function woocommerce( $product_ids ): array {
52 $product_details = [];
53 $product_price_total = 0;
54
55 if ( ! function_exists( 'wc_get_product' ) ) {
56 return [
57 'details' => $product_details,
58 'total' => $product_price_total,
59 ];
60 }
61
62 foreach ( (array) $product_ids as $single_product_id ) {
63 $product = wc_get_product( $single_product_id );
64
65 if ( ! $product ) {
66 continue;
67 }
68
69 $product_image_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), 'single-post-thumbnail' );
70
71 $product_details[ $single_product_id ] = [
72 'product_name' => $product->get_name(),
73 'product_permalink' => get_permalink( $product->get_id() ),
74 'product_price' => $product->get_price(),
75 'product_image_src_array' => $product_image_src_array,
76 'product_image_src' => is_array( $product_image_src_array ) ? $product_image_src_array[0] : '',
77 ];
78
79 $product_price_total += floatval( $product_details[ $single_product_id ]['product_price'] );
80 }
81
82 return [
83 'details' => $product_details,
84 'total' => $product_price_total,
85 ];
86 }
87
88 /**
89 * Product details and price total for FluentCart product ids.
90 *
91 * @param mixed $product_ids FluentCart product ids.
92 * @return array{details: array, total: float|int}
93 */
94 public static function fluentcart( $product_ids ): array {
95 $product_details = [];
96 $product_price_total = 0;
97
98 if ( ! function_exists( 'fluentCart' ) || ! class_exists( '\FluentCart\App\Models\Product' ) ) {
99 return [
100 'details' => $product_details,
101 'total' => $product_price_total,
102 ];
103 }
104
105 foreach ( (array) $product_ids as $single_product_id ) {
106 try {
107 $fluentcart_product = \FluentCart\App\Models\Product::query()
108 ->with( [ 'detail', 'variants' ] )
109 ->find( $single_product_id );
110
111 if ( ! $fluentcart_product ) {
112 continue;
113 }
114
115 // FluentCart stores prices in cents.
116 $fluentcart_price = 0;
117 if ( ! empty( $fluentcart_product->variants ) && count( $fluentcart_product->variants ) > 0 ) {
118 $fluentcart_price = $fluentcart_product->variants[0]->item_price;
119 } elseif ( ! empty( $fluentcart_product->detail ) ) {
120 $fluentcart_price = $fluentcart_product->detail->min_price;
121 }
122
123 if ( ! empty( $fluentcart_price ) ) {
124 $fluentcart_price = floatval( $fluentcart_price / 100 );
125 }
126
127 $product_image_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $fluentcart_product->ID ), 'single-post-thumbnail' );
128
129 $product_details[ $single_product_id ] = [
130 'product_name' => $fluentcart_product->post_title,
131 'product_permalink' => get_permalink( $fluentcart_product->ID ),
132 'product_price' => $fluentcart_price,
133 'product_image_src_array' => $product_image_src_array,
134 'product_image_src' => is_array( $product_image_src_array ) ? $product_image_src_array[0] : '',
135 ];
136
137 $product_price_total += floatval( $fluentcart_price );
138 } catch ( \Exception $e ) {
139 // A product FluentCart cannot load is left out of the list.
140 continue;
141 }
142 }
143
144 return [
145 'details' => $product_details,
146 'total' => $product_price_total,
147 ];
148 }
149 }
150