$data Add-to-cart data. * @param \WP_REST_Request $request Store API request. * @return array */ public function capture( $data, $request ) { $raw = $request instanceof \WP_REST_Request ? $request->get_param( 'thridify' ) : null; if ( ! is_array( $raw ) ) { return $data; } $summary = isset( $raw['summary'] ) ? substr( sanitize_text_field( (string) $raw['summary'] ), 0, self::MAX_SUMMARY ) : ''; $reference = isset( $raw['reference'] ) ? self::sanitize_reference( (string) $raw['reference'] ) : ''; if ( '' === $summary && '' === $reference ) { return $data; } $data['cart_item_data'] = isset( $data['cart_item_data'] ) && is_array( $data['cart_item_data'] ) ? $data['cart_item_data'] : array(); $data['cart_item_data'][ self::ITEM_KEY ] = array( 'summary' => $summary, 'reference' => $reference, ); return $data; } /** * A configuration reference: `` or `#cfg=` * (the recipe is lz-string "encoded URI component" text). * * @param string $reference Raw reference. * @return string */ public static function sanitize_reference( $reference ) { $reference = substr( trim( $reference ), 0, self::MAX_REFERENCE ); return preg_match( '/^[A-Za-z0-9_-]+(#cfg=[A-Za-z0-9+$_.~=-]+)?$/', $reference ) ? $reference : ''; } /** * Show the configuration under the item in cart and checkout. * * @param array> $item_data Display rows. * @param array $cart_item Cart item. * @return array> */ public function display( $item_data, $cart_item ) { if ( ! empty( $cart_item[ self::ITEM_KEY ]['summary'] ) ) { $item_data[] = array( 'key' => __( 'Configuration', 'thridify' ), 'value' => (string) $cart_item[ self::ITEM_KEY ]['summary'], ); } return $item_data; } /** * Keep it on the order: a visible "Configuration" line and the hidden * reference used by sales attribution and by the merchant to reopen the * exact configuration in Thridify. * * @param \WC_Order_Item_Product $item Order item. * @param string $cart_item_key Cart item key. * @param array $values Cart item. * @return void */ public function to_order_item( $item, $cart_item_key, $values ) { if ( empty( $values[ self::ITEM_KEY ] ) || ! is_array( $values[ self::ITEM_KEY ] ) ) { return; } $config = $values[ self::ITEM_KEY ]; if ( ! empty( $config['summary'] ) ) { $item->add_meta_data( __( 'Configuration', 'thridify' ), (string) $config['summary'], true ); } $item->add_meta_data( self::META_CONFIG, '' !== $config['reference'] ? (string) $config['reference'] : '1', true ); } }