Files
magdev c6a48d6404 Fix critical UI bugs in admin and frontend
Fixes three critical bugs reported in CLAUDE.md:

1. Admin rendering bug - Fixed CSS to prevent both General and Composable
   Options tabs from showing simultaneously on initial page load
   - Enhanced CSS specificity with !important flags
   - Added body.product-type-composable selectors for proper visibility control
   - Hides Composable Options tab by default, shows only when composable type selected

2. Frontend product selector not appearing - Fixed WooCommerce integration
   - Added hide_default_add_to_cart() method to Cart_Handler
   - Hooks woocommerce_is_purchasable filter to return false for composable products
   - This hides WooCommerce's default add-to-cart button
   - Allows our custom product selector to be the only interface

3. Localized price formatting - Implemented proper WooCommerce price formatting
   - Added wc_price Twig function in Plugin.php
   - Updated Product_Selector to pass formatted price HTML to template
   - Added price_format data to JavaScript localization
   - Implemented formatPrice() method in frontend.js
   - Supports all WooCommerce price formats (currency position, decimals, separators)
   - Template now uses {{ fixed_price_html|raw }} and {{ zero_price_html|raw }}
   - JavaScript dynamically formats prices using locale-specific settings

Technical improvements:
- Cart_Handler.php: +14 lines (hide_default_add_to_cart method)
- Plugin.php: +7 lines (wc_price function, price format localization)
- Product_Selector.php: +2 lines (formatted price HTML context)
- templates/product-selector.twig: Modified to use formatted price HTML
- assets/css/admin.css: +24 lines (enhanced tab visibility control)
- assets/js/frontend.js: +28 lines (formatPrice method with WooCommerce format support)

All PHP files pass lint checks. Frontend now properly displays localized prices
with correct currency symbols, decimal separators, and thousand separators for
all WooCommerce-supported locales (CHF for Switzerland, € for Europe, etc.).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-31 22:08:08 +01:00

234 lines
7.3 KiB
PHP

<?php
/**
* Main Plugin Class
*
* @package WC_Composable_Product
*/
namespace WC_Composable_Product;
defined('ABSPATH') || exit;
/**
* Main plugin class - Singleton pattern
*/
class Plugin {
/**
* The single instance of the class
*
* @var Plugin
*/
protected static $instance = null;
/**
* Twig environment
*
* @var \Twig\Environment
*/
private $twig = null;
/**
* Main Plugin Instance
*
* Ensures only one instance is loaded or can be loaded.
*
* @return Plugin
*/
public static function instance() {
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->init_hooks();
$this->init_twig();
$this->includes();
}
/**
* Hook into WordPress and WooCommerce
*/
private function init_hooks() {
// Register product type
add_filter('product_type_selector', [$this, 'add_product_type']);
add_filter('woocommerce_product_class', [$this, 'product_class'], 10, 2);
// Enqueue scripts and styles
add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_scripts']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
// Admin settings
add_filter('woocommerce_get_settings_pages', [$this, 'add_settings_page']);
}
/**
* Initialize Twig template engine
*/
private function init_twig() {
$loader = new \Twig\Loader\FilesystemLoader(WC_COMPOSABLE_PRODUCT_PATH . 'templates');
$this->twig = new \Twig\Environment($loader, [
'cache' => WC_COMPOSABLE_PRODUCT_PATH . 'cache',
'auto_reload' => true,
'debug' => defined('WP_DEBUG') && WP_DEBUG,
]);
// Add WordPress functions to Twig
$this->twig->addFunction(new \Twig\TwigFunction('__', function($text) {
return __($text, 'wc-composable-product');
}));
$this->twig->addFunction(new \Twig\TwigFunction('esc_html', 'esc_html'));
$this->twig->addFunction(new \Twig\TwigFunction('esc_attr', 'esc_attr'));
$this->twig->addFunction(new \Twig\TwigFunction('esc_url', 'esc_url'));
$this->twig->addFunction(new \Twig\TwigFunction('wc_price', 'wc_price'));
// Add WordPress escaping functions as Twig filters
$this->twig->addFilter(new \Twig\TwigFilter('esc_html', 'esc_html'));
$this->twig->addFilter(new \Twig\TwigFilter('esc_attr', 'esc_attr'));
$this->twig->addFilter(new \Twig\TwigFilter('esc_url', 'esc_url'));
}
/**
* Include required files
*/
private function includes() {
// Note: Settings.php is NOT included here because it extends WC_Settings_Page
// which isn't loaded until later. It's included in add_settings_page() instead.
require_once WC_COMPOSABLE_PRODUCT_PATH . 'includes/Admin/Product_Data.php';
require_once WC_COMPOSABLE_PRODUCT_PATH . 'includes/Product_Type.php';
require_once WC_COMPOSABLE_PRODUCT_PATH . 'includes/Stock_Manager.php';
require_once WC_COMPOSABLE_PRODUCT_PATH . 'includes/Cart_Handler.php';
require_once WC_COMPOSABLE_PRODUCT_PATH . 'includes/Product_Selector.php';
// Initialize components
new Admin\Product_Data();
new Cart_Handler();
}
/**
* Add composable product type to selector
*
* @param array $types Product types
* @return array
*/
public function add_product_type($types) {
$types['composable'] = __('Composable product', 'wc-composable-product');
return $types;
}
/**
* Use custom product class for composable products
*
* @param string $classname Product class name
* @param string $product_type Product type
* @return string
*/
public function product_class($classname, $product_type) {
if ($product_type === 'composable') {
$classname = 'WC_Composable_Product\Product_Type';
}
return $classname;
}
/**
* Enqueue frontend scripts and styles
*/
public function enqueue_frontend_scripts() {
if (is_product()) {
wp_enqueue_style(
'wc-composable-product',
WC_COMPOSABLE_PRODUCT_URL . 'assets/css/frontend.css',
[],
WC_COMPOSABLE_PRODUCT_VERSION
);
wp_enqueue_script(
'wc-composable-product',
WC_COMPOSABLE_PRODUCT_URL . 'assets/js/frontend.js',
['jquery'],
WC_COMPOSABLE_PRODUCT_VERSION,
true
);
wp_localize_script('wc-composable-product', 'wcComposableProduct', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('wc_composable_product_nonce'),
'i18n' => [
'select_items' => __('Please select items', 'wc-composable-product'),
'max_items' => __('Maximum items selected', 'wc-composable-product'),
'min_items' => __('Please select at least one item', 'wc-composable-product'),
],
'price_format' => [
'currency_symbol' => get_woocommerce_currency_symbol(),
'decimal_separator' => wc_get_price_decimal_separator(),
'thousand_separator' => wc_get_price_thousand_separator(),
'decimals' => wc_get_price_decimals(),
'price_format' => get_woocommerce_price_format(),
],
]);
}
}
/**
* Enqueue admin scripts and styles
*/
public function enqueue_admin_scripts($hook) {
if ('post.php' === $hook || 'post-new.php' === $hook) {
global $post_type;
if ('product' === $post_type) {
wp_enqueue_style(
'wc-composable-product-admin',
WC_COMPOSABLE_PRODUCT_URL . 'assets/css/admin.css',
[],
WC_COMPOSABLE_PRODUCT_VERSION
);
wp_enqueue_script(
'wc-composable-product-admin',
WC_COMPOSABLE_PRODUCT_URL . 'assets/js/admin.js',
['jquery', 'wc-admin-product-meta-boxes'],
WC_COMPOSABLE_PRODUCT_VERSION,
true
);
}
}
}
/**
* Add settings page to WooCommerce
*
* @param array $settings WooCommerce settings pages
* @return array
*/
public function add_settings_page($settings) {
// Include Settings.php here, when WC_Settings_Page is guaranteed to be loaded
require_once WC_COMPOSABLE_PRODUCT_PATH . 'includes/Admin/Settings.php';
$settings[] = new Admin\Settings();
return $settings;
}
/**
* Get Twig environment
*
* @return \Twig\Environment
*/
public function get_twig() {
return $this->twig;
}
/**
* Render a Twig template
*
* @param string $template Template name
* @param array $context Template variables
* @return string
*/
public function render_template($template, $context = []) {
return $this->twig->render($template, $context);
}
}