-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
215 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Store_API_Extension class. | ||
* | ||
* A class to extend the store public API with Table Rate Shipping Abort Message functionality. | ||
* | ||
* @package WooCommerce_Table_Rate_Shipping | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
use Automattic\WooCommerce\StoreApi\StoreApi; | ||
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; | ||
use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema; | ||
|
||
/** | ||
* Store API Extension. | ||
*/ | ||
class WC_Connect_Store_API_Extension { | ||
/** | ||
* Stores Rest Extending instance. | ||
* | ||
* @var ExtendSchema | ||
*/ | ||
private static $extend; | ||
|
||
/** | ||
* Plugin Identifier, unique to each plugin. | ||
* | ||
* @var string | ||
*/ | ||
const IDENTIFIER = 'woocommerce_services'; | ||
|
||
/** | ||
* Bootstraps the class and hooks required data. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public static function init() { | ||
self::$extend = StoreApi::container()->get( ExtendSchema::class ); | ||
self::extend_store(); | ||
} | ||
|
||
/** | ||
* Registers the data into each endpoint. | ||
*/ | ||
public static function extend_store() { | ||
|
||
self::$extend->register_endpoint_data( | ||
array( | ||
'endpoint' => CartSchema::IDENTIFIER, | ||
'namespace' => self::IDENTIFIER, | ||
'data_callback' => array( static::class, 'data' ), | ||
'schema_callback' => array( static::class, 'schema' ), | ||
'schema_type' => ARRAY_A, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Store API extension data callback. | ||
* | ||
* @return array | ||
*/ | ||
public static function data() { | ||
$notices = WC()->session->get( WC_Connect_TaxJar_Integration::NOTICE_KEY ); | ||
$notices = is_array( $notices ) ? $notices : array(); | ||
|
||
return array( | ||
'error_notices' => $notices, | ||
); | ||
} | ||
|
||
/** | ||
* Store API extension schema callback. | ||
* | ||
* @return array Registered schema. | ||
*/ | ||
public static function schema() { | ||
return array( | ||
'error_notices' => array( | ||
'description' => __( 'Error notices from TaxJar operation.', 'woocommerce-services' ), | ||
'type' => 'array', | ||
'context' => array( 'view', 'edit' ), | ||
'readonly' => true, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Block Notices | ||
* | ||
* This file is responsible for rendering Abort Messages from the Table Rate Shipping plugin. | ||
* | ||
* @package WooCommerce_Services | ||
*/ | ||
|
||
window.addEventListener("load", () => { | ||
const { useSelect } = window.wp.data; | ||
const { registerPlugin } = window.wp.plugins; | ||
const { ExperimentalOrderShippingPackages, StoreNotice } = window.wc.blocksCheckout; | ||
const { RawHTML } = window.wp.element; | ||
|
||
const createStoreNotice = ( notice, index, type = 'info' ) => { | ||
if ( 'debug' === type ) { | ||
type = 'info'; | ||
} | ||
|
||
const message = <RawHTML>{notice}</RawHTML>; | ||
|
||
return ( | ||
<StoreNotice key={index} status={type} isDismissible={false}> | ||
{message} | ||
</StoreNotice> | ||
); | ||
}; | ||
|
||
const Notices = ({ messages }) => { | ||
if ( ! messages['notice'] ) { | ||
return null; | ||
} | ||
|
||
const currentMessage = messages['notice']; | ||
|
||
return ( | ||
<div className="woocommerce-services-block-notices"> | ||
Check failure on line 37 in client/checkout-notices/index.js GitHub Actions / Build Container and Run E2E Tests
|
||
{createStoreNotice( currentMessage, 0, 'info' )} | ||
</div> | ||
); | ||
}; | ||
|
||
const render = () => { | ||
const { errorNotices } = useSelect((select) => { | ||
const storeCartData = select( 'wc/store/cart' ).getCartData(); | ||
|
||
if ( ! storeCartData.extensions && ! storeCartData.extensions['woocommerce_services'] && ! storeCartData.extensions['woocommerce_services'].error_notices ) { | ||
Check failure on line 47 in client/checkout-notices/index.js GitHub Actions / Build Container and Run E2E Tests
|
||
return {}; | ||
} | ||
const errorNotices = storeCartData.extensions['woocommerce_services'].error_notices; | ||
Check failure on line 50 in client/checkout-notices/index.js GitHub Actions / Build Container and Run E2E Tests
|
||
|
||
return { | ||
errorNotices, | ||
}; | ||
}, []); | ||
|
||
// Ensure we only show abort messages if no shipping rates are available. | ||
if ( ! errorNotices ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ExperimentalOrderShippingPackages> | ||
<Notices messages={errorNotices} /> | ||
</ExperimentalOrderShippingPackages> | ||
); | ||
}; | ||
|
||
registerPlugin('woocommerce-services-notices', { | ||
render, | ||
scope: 'woocommerce-checkout', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters