| 1 |
<?php |
| 2 |
namespace Sellkit\Blocks\Render; |
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit\Blocks\Sellkit_Blocks; |
| 6 |
|
| 7 |
/** |
| 8 |
* Order cart details block. |
| 9 |
* |
| 10 |
* @since 2.3.0 |
| 11 |
*/ |
| 12 |
class Order_Cart_Details { |
| 13 |
/** |
| 14 |
* Post ID. |
| 15 |
* |
| 16 |
* @var int |
| 17 |
* @since 2.3.0 |
| 18 |
* @access private |
| 19 |
*/ |
| 20 |
private $post_id; |
| 21 |
|
| 22 |
/** |
| 23 |
* Order. |
| 24 |
* |
| 25 |
* @var \WC_Order |
| 26 |
* @since 2.3.0 |
| 27 |
* @access private |
| 28 |
*/ |
| 29 |
private $order; |
| 30 |
|
| 31 |
/** |
| 32 |
* Order_Cart_Details constructor. |
| 33 |
* |
| 34 |
* @since 2.3.0 |
| 35 |
*/ |
| 36 |
public function __construct( $post_id ) { |
| 37 |
$this->post_id = $post_id; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Check block activation. |
| 42 |
* |
| 43 |
* @since 2.3.0 |
| 44 |
* @return boolean |
| 45 |
*/ |
| 46 |
public function is_active() { |
| 47 |
if ( empty( $this->post_id ) ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
$step_data = get_post_meta( $this->post_id, 'step_data', true ); |
| 52 |
|
| 53 |
if ( ! empty( $step_data['type']['key'] ) && 'thankyou' === $step_data['type']['key'] ) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
if ( |
| 58 |
function_exists( 'is_checkout' ) && function_exists( 'is_wc_endpoint_url' ) && |
| 59 |
is_checkout() && ! empty( is_wc_endpoint_url( 'order-received' ) ) |
| 60 |
) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Register block from meta. |
| 69 |
* |
| 70 |
* @since 2.3.0 |
| 71 |
*/ |
| 72 |
public function register_block_meta() { |
| 73 |
if ( ! $this->is_active() ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
register_block_type_from_metadata( |
| 78 |
__DIR__, |
| 79 |
[ |
| 80 |
'render_callback' => [ $this, 'render' ], |
| 81 |
] |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check block has inner blocks. |
| 87 |
* |
| 88 |
* @since 2.3.0 |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
public function has_inner_blocks() { |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get inner blocks. |
| 97 |
* |
| 98 |
* @since 2.3.0 |
| 99 |
* @return boolean |
| 100 |
*/ |
| 101 |
public function get_inner_block() { |
| 102 |
return [ |
| 103 |
'order-products' => 'block-editor/blocks/order-cart-details/inner-blocks/order-products/class', |
| 104 |
'order-details' => 'block-editor/blocks/order-cart-details/inner-blocks/order-details/class', |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Render block in front-end. |
| 110 |
* |
| 111 |
* @param array $attributes Block attributes. |
| 112 |
* @param string $content Block content. |
| 113 |
* @param \WP_Block $block Block object. |
| 114 |
* @since 2.3.0 |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
public function render( $attributes, $content, $block ) { |
| 118 |
$block_html = ''; |
| 119 |
$wrapper_attributes = get_block_wrapper_attributes(); |
| 120 |
|
| 121 |
$order = $this->generate_cart_items(); |
| 122 |
|
| 123 |
if ( empty( $order ) ) { |
| 124 |
return $block_html; |
| 125 |
} |
| 126 |
|
| 127 |
foreach ( $block->inner_blocks as $inner_block ) { |
| 128 |
$block_name = $inner_block->parsed_block['blockName']; |
| 129 |
|
| 130 |
if ( 'core/heading' !== $block_name ) { |
| 131 |
$block_name = str_replace( 'sellkit-inner-blocks/', '', $block_name ); |
| 132 |
$block_name = str_replace( '-', '_', $block_name ); |
| 133 |
} |
| 134 |
|
| 135 |
$block_content = $inner_block->render(); |
| 136 |
$class_name = 'Sellkit\Blocks\Inner_Block\\' . ucwords( $block_name ); |
| 137 |
|
| 138 |
if ( ! class_exists( $class_name ) ) { |
| 139 |
$block_html .= $inner_block->render(); |
| 140 |
|
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
$new_class = new $class_name( $this->order ); |
| 145 |
|
| 146 |
if ( method_exists( $new_class, $block_name ) ) { |
| 147 |
$block_html .= $new_class->$block_name( $block_content, $order ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if ( empty( $block_html ) ) { |
| 152 |
return ''; |
| 153 |
} |
| 154 |
|
| 155 |
$block_html = sprintf( |
| 156 |
'<div %1$s>%2$s</div>', |
| 157 |
wp_kses_data( $wrapper_attributes ), |
| 158 |
wp_kses_post( $block_html ) |
| 159 |
); |
| 160 |
|
| 161 |
return $block_html; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Generate cart items. |
| 167 |
* |
| 168 |
* @since 2.3.0 |
| 169 |
*/ |
| 170 |
private function generate_cart_items() { |
| 171 |
$order = $this->get_order_products(); |
| 172 |
|
| 173 |
if ( empty( $order ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$order_items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) ); |
| 178 |
$order_items_total = $order->get_order_item_totals(); |
| 179 |
|
| 180 |
return [ |
| 181 |
'items' => $order_items, |
| 182 |
'prices' => $order_items_total, |
| 183 |
]; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get order products. |
| 188 |
* |
| 189 |
* @since 2.3.0 |
| 190 |
* @return mixed |
| 191 |
*/ |
| 192 |
private function get_order_products() { |
| 193 |
$key = filter_input( INPUT_GET, 'order-key', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 194 |
|
| 195 |
if ( empty( $key ) ) { |
| 196 |
$key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 197 |
} |
| 198 |
|
| 199 |
$order_id = null; |
| 200 |
|
| 201 |
if ( $key ) { |
| 202 |
$order_id = wc_get_order_id_by_order_key( $key ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! empty( $order_id ) ) { |
| 206 |
$this->order = $this->get_order_object( $order_id ); |
| 207 |
} |
| 208 |
|
| 209 |
return $this->order; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Get order object. |
| 214 |
* |
| 215 |
* @return mixed |
| 216 |
* @param string $order_id Order id. |
| 217 |
* @since 2.3.0 |
| 218 |
*/ |
| 219 |
public function get_order_object( $order_id ) { |
| 220 |
$order = wc_get_order( $order_id ); |
| 221 |
$main_order_id = $order->get_meta( 'sellkit_main_order_id' ); |
| 222 |
|
| 223 |
if ( empty( $main_order_id ) ) { |
| 224 |
return $order; |
| 225 |
} |
| 226 |
|
| 227 |
$main_order = new \WC_Order( $main_order_id ); |
| 228 |
$total_order = new \WC_Order(); |
| 229 |
|
| 230 |
foreach ( $order->get_items() as $item ) { |
| 231 |
$total_order->add_item( $item ); |
| 232 |
} |
| 233 |
|
| 234 |
foreach ( $main_order->get_items() as $item ) { |
| 235 |
$total_order->add_item( $item ); |
| 236 |
} |
| 237 |
|
| 238 |
$total_price = $order->get_total() + $main_order->get_total(); |
| 239 |
|
| 240 |
$total_order->set_total( $total_price ); |
| 241 |
$total_order->set_payment_method( $main_order->get_payment_method() ); |
| 242 |
$total_order->set_address( $order->get_address() ); |
| 243 |
$total_order->set_address( $order->get_address( 'shipping' ), 'shipping' ); |
| 244 |
|
| 245 |
return $total_order; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|