Agentic
4 months ago
BlockTemplates
4 weeks ago
EmailImprovements
1 year ago
EmailPreview
4 weeks ago
Emails
1 year ago
ImportExport
1 year ago
Logging
1 year ago
Marketing
2 years ago
Notes
4 weeks ago
Onboarding
5 months ago
Orders
4 weeks ago
ProductForm
2 years ago
ProductReviews
4 weeks ago
RemoteFreeExtensions
4 weeks ago
Schedulers
4 weeks ago
Settings
2 weeks ago
Suggestions
4 weeks ago
WCPayPromotion
10 months ago
ActivityPanels.php
3 years ago
Analytics.php
4 weeks ago
CategoryLookup.php
4 years ago
Coupons.php
1 year ago
CouponsMovedTrait.php
5 months ago
CustomerEffortScoreTracks.php
7 months ago
Events.php
2 months ago
FeaturePlugin.php
4 months ago
Homescreen.php
1 month ago
Loader.php
4 weeks ago
Marketing.php
1 year ago
Marketplace.php
5 months ago
MobileAppBanner.php
4 years ago
OrderMilestoneEasterEgg.php
4 weeks ago
RemoteInboxNotifications.php
3 years ago
Settings.php
2 weeks ago
ShippingLabelBanner.php
1 year ago
ShippingLabelBannerDisplayRules.php
1 year ago
SiteHealth.php
3 years ago
Survey.php
4 years ago
SystemStatusReport.php
1 year ago
Translations.php
1 year ago
WCAdminAssets.php
2 weeks ago
WCAdminSharedSettings.php
1 year ago
WCAdminUser.php
9 months ago
WcPayWelcomePage.php
1 year ago
ShippingLabelBanner.php
161 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Shipping Label banner. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin; |
| 7 | |
| 8 | use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager; |
| 9 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 10 | use function WP_CLI\Utils\get_plugin_name; |
| 11 | |
| 12 | /** |
| 13 | * Shows print shipping label banner on edit order page. |
| 14 | */ |
| 15 | class ShippingLabelBanner { |
| 16 | |
| 17 | /** |
| 18 | * Singleton for the display rules class |
| 19 | * |
| 20 | * @var ShippingLabelBannerDisplayRules |
| 21 | */ |
| 22 | private $shipping_label_banner_display_rules; |
| 23 | |
| 24 | private const MIN_COMPATIBLE_WCST_VERSION = '2.7.0'; |
| 25 | private const MIN_COMPATIBLE_WCSHIPPING_VERSION = '1.1.0'; |
| 26 | |
| 27 | /** |
| 28 | * Constructor |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | if ( ! is_admin() ) { |
| 32 | return; |
| 33 | } |
| 34 | add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 6, 2 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Check if WooCommerce Shipping makes sense for this merchant. |
| 39 | * |
| 40 | * @return bool |
| 41 | */ |
| 42 | private function should_show_meta_box() { |
| 43 | if ( ! $this->shipping_label_banner_display_rules ) { |
| 44 | $dotcom_connected = null; |
| 45 | $wcs_version = null; |
| 46 | |
| 47 | if ( class_exists( Jetpack_Connection_Manager::class ) ) { |
| 48 | $dotcom_connected = ( new Jetpack_Connection_Manager() )->has_connected_owner(); |
| 49 | } |
| 50 | |
| 51 | if ( class_exists( '\Automattic\WCShipping\Utils' ) ) { |
| 52 | $wcs_version = \Automattic\WCShipping\Utils::get_wcshipping_version(); |
| 53 | } |
| 54 | |
| 55 | $incompatible_plugins = class_exists( '\WC_Shipping_Fedex_Init' ) || |
| 56 | class_exists( '\WC_Shipping_UPS_Init' ) || |
| 57 | class_exists( '\WC_Integration_ShippingEasy' ) || |
| 58 | class_exists( '\WC_ShipStation_Integration' ); |
| 59 | |
| 60 | $this->shipping_label_banner_display_rules = |
| 61 | new ShippingLabelBannerDisplayRules( |
| 62 | $dotcom_connected, |
| 63 | $wcs_version, |
| 64 | $incompatible_plugins |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | return $this->shipping_label_banner_display_rules->should_display_banner(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Add metabox to order page. |
| 73 | */ |
| 74 | public function add_meta_boxes() { |
| 75 | if ( ! OrderUtil::is_order_edit_screen() ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if ( $this->should_show_meta_box() ) { |
| 80 | add_meta_box( |
| 81 | 'woocommerce-admin-print-label', |
| 82 | __( 'Shipping Label', 'woocommerce' ), |
| 83 | array( $this, 'meta_box' ), |
| 84 | null, |
| 85 | 'normal', |
| 86 | 'high', |
| 87 | array( |
| 88 | 'context' => 'shipping_label', |
| 89 | ) |
| 90 | ); |
| 91 | add_action( 'admin_enqueue_scripts', array( $this, 'add_print_shipping_label_script' ) ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Count shippable items |
| 97 | * |
| 98 | * @param \WC_Order $order Current order. |
| 99 | * @return int |
| 100 | */ |
| 101 | private function count_shippable_items( \WC_Order $order ) { |
| 102 | $count = 0; |
| 103 | foreach ( $order->get_items() as $item ) { |
| 104 | if ( $item instanceof \WC_Order_Item_Product ) { |
| 105 | $product = $item->get_product(); |
| 106 | if ( $product && $product->needs_shipping() ) { |
| 107 | $count += $item->get_quantity(); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return $count; |
| 112 | } |
| 113 | /** |
| 114 | * Adds JS to order page to render shipping banner. |
| 115 | * |
| 116 | * @param string $hook current page hook. |
| 117 | */ |
| 118 | public function add_print_shipping_label_script( $hook ) { |
| 119 | WCAdminAssets::register_style( 'print-shipping-label-banner', 'style', array( 'wp-components' ) ); |
| 120 | WCAdminAssets::register_script( 'wp-admin-scripts', 'print-shipping-label-banner', true ); |
| 121 | $wcst_version = null; |
| 122 | $wcshipping_installed_version = null; |
| 123 | $order = wc_get_order(); |
| 124 | if ( class_exists( '\WC_Connect_Loader' ) ) { |
| 125 | $wcst_version = \WC_Connect_Loader::get_wcs_version(); |
| 126 | } |
| 127 | |
| 128 | $wc_shipping_plugin_file = WP_PLUGIN_DIR . '/woocommerce-shipping/woocommerce-shipping.php'; |
| 129 | if ( file_exists( $wc_shipping_plugin_file ) ) { |
| 130 | $plugin_data = get_plugin_data( $wc_shipping_plugin_file ); |
| 131 | $wcshipping_installed_version = $plugin_data['Version']; |
| 132 | } |
| 133 | |
| 134 | $payload = array( |
| 135 | // If WCS&T is not installed, it's considered compatible. |
| 136 | 'is_wcst_compatible' => $wcst_version ? (int) version_compare( $wcst_version, self::MIN_COMPATIBLE_WCST_VERSION, '>=' ) : 1, |
| 137 | 'order_id' => $order ? $order->get_id() : null, |
| 138 | // The banner is shown if the plugin is installed but not active, so we need to check if the installed version is compatible. |
| 139 | 'is_incompatible_wcshipping_installed' => $wcshipping_installed_version ? |
| 140 | (int) version_compare( $wcshipping_installed_version, self::MIN_COMPATIBLE_WCSHIPPING_VERSION, '<' ) |
| 141 | : 0, |
| 142 | ); |
| 143 | |
| 144 | wp_localize_script( 'wc-admin-print-shipping-label-banner', 'wcShippingCoreData', $payload ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Render placeholder metabox. |
| 149 | * |
| 150 | * @param \WP_Post $post current post. |
| 151 | * @param array $args empty args. |
| 152 | */ |
| 153 | public function meta_box( $post, $args ) { |
| 154 | |
| 155 | ?> |
| 156 | <div id="wc-admin-shipping-banner-root" class="woocommerce <?php echo esc_attr( 'wc-admin-shipping-banner' ); ?>" data-args="<?php echo esc_attr( wp_json_encode( $args['args'] ) ); ?>"> |
| 157 | </div> |
| 158 | <?php |
| 159 | } |
| 160 | } |
| 161 |