post_type !== 'product') { return; } wp_enqueue_style( 'bundle-admin', BUNDLE_URL . 'assets/css/admin.css', [], \Bundle\VERSION, ); } /** * Register the "Bundle" product-data tab. * * @param array> $tabs * @return array> */ public function addTab(array $tabs): array { // Simple products only. The tab used to show on variable products too, // so a merchant could configure a bundle there, but the one-click add // sends no variation, and WooCommerce answers a variable product with // "please choose product options" instead of putting it in the cart. The // shopper got a cart missing the very product the box was selling. $tabs['bundle'] = [ 'label' => __('Bundle', 'bundaro'), 'target' => 'bundle_product_data', 'class' => ['show_if_simple'], 'priority' => 65, ]; return $tabs; } public function renderPanel(): void { global $post; $product = $post instanceof \WP_Post ? wc_get_product($post->ID) : null; $items = ''; $percent = ''; if ($product instanceof \WC_Product) { $raw = $product->get_meta(BundleService::META_BUNDLE); if (is_array($raw)) { $items = implode(', ', array_map('absint', (array) ($raw['items'] ?? []))); $percent = isset($raw['discount_percent']) ? (string) (float) $raw['discount_percent'] : ''; } } ?> 0 && $itemId !== $postId && ! in_array($itemId, $items, true)) { // Only what one click can put in the cart is kept: a simple // product, or a variation with every attribute chosen. A // variable product needs the shopper's options, an external // or grouped product has no cart line, and an ID that is not // a product at all was saved and then advertised in the box. // Each is refused here and reported, rather than saved to // fail on the storefront. if (! self::isLinkable(wc_get_product($itemId))) { $skipped[] = $itemId; continue; } $items[] = $itemId; } if (count($items) + count($skipped) >= self::MAX_ITEMS) { break; } } $percent = isset($_POST['bundle_discount_percent']) ? (float) sanitize_text_field(wp_unslash($_POST['bundle_discount_percent'])) : 0.0; $percent = max(0.0, min(100.0, $percent)); if ($items === []) { $product->delete_meta_data(BundleService::META_BUNDLE); } else { $product->update_meta_data(BundleService::META_BUNDLE, [ 'items' => $items, 'discount_percent' => $percent, ]); } $product->save(); if ($skipped !== []) { set_transient(self::SKIPPED_TRANSIENT . get_current_user_id(), $skipped, MINUTE_IN_SECONDS); } } /** * Whether a product can be added to the cart without the shopper choosing * anything: a simple product, or a variation with no "Any" attribute. */ private static function isLinkable(mixed $product): bool { if ($product instanceof \WC_Product_Variation) { return ! in_array('', $product->get_variation_attributes(), true); } return $product instanceof \WC_Product && $product->is_type('simple'); } /** * Tell the merchant which IDs were dropped on the last save, so a product * silently vanishing from the bundle list is never a mystery. */ public function renderSkippedNotice(): void { $key = self::SKIPPED_TRANSIENT . get_current_user_id(); $skipped = get_transient($key); if (! is_array($skipped) || $skipped === []) { return; } delete_transient($key); printf( '

%s

', esc_html( sprintf( /* translators: %s: comma-separated list of product IDs. */ __('Bundle: these product IDs were not saved because one click cannot add them to the cart: %s. Link simple products, or variations with every option chosen.', 'bundaro'), implode(', ', array_map('absint', $skipped)) ) ) ); } }