You've already forked wc-composable-product
Initial implementation of WooCommerce Composable Products plugin
Implemented custom WooCommerce product type allowing customers to build their own product bundles by selecting from predefined sets of products. Features: - Custom "Composable Product" type with admin interface - Product selection by category, tag, or SKU - Configurable selection limits (global and per-product) - Dual pricing modes: fixed price or sum of selected products - Modern responsive frontend with Twig templates - AJAX add-to-cart functionality - Full internationalization support (.pot file) - WooCommerce settings integration - Comprehensive documentation Technical implementation: - PHP 8.3+ with PSR-4 autoloading - Twig 3.0 templating engine via Composer - Vanilla JavaScript with jQuery for frontend interactions - WordPress and WooCommerce hooks for seamless integration - Security: input sanitization, validation, and output escaping - Translation-ready with text domain 'wc-composable-product' Documentation: - README.md: Project overview and features - INSTALL.md: Installation and usage guide - IMPLEMENTATION.md: Technical architecture - CHANGELOG.md: Version history 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
190
includes/Admin/Product_Data.php
Normal file
190
includes/Admin/Product_Data.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
/**
|
||||
* Product Data Tab
|
||||
*
|
||||
* @package WC_Composable_Product
|
||||
*/
|
||||
|
||||
namespace WC_Composable_Product\Admin;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Product Data Tab Class
|
||||
*/
|
||||
class Product_Data {
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter('woocommerce_product_data_tabs', [$this, 'add_product_data_tab']);
|
||||
add_action('woocommerce_product_data_panels', [$this, 'add_product_data_panel']);
|
||||
add_action('woocommerce_process_product_meta_composable', [$this, 'save_product_data']);
|
||||
add_action('woocommerce_product_options_general_product_data', [$this, 'add_general_fields']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add composable products tab
|
||||
*
|
||||
* @param array $tabs Product data tabs
|
||||
* @return array
|
||||
*/
|
||||
public function add_product_data_tab($tabs) {
|
||||
$tabs['composable'] = [
|
||||
'label' => __('Composable Options', 'wc-composable-product'),
|
||||
'target' => 'composable_product_data',
|
||||
'class' => ['show_if_composable'],
|
||||
'priority' => 21,
|
||||
];
|
||||
return $tabs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add fields to general tab
|
||||
*/
|
||||
public function add_general_fields() {
|
||||
global $product_object;
|
||||
|
||||
if ($product_object && $product_object->get_type() === 'composable') {
|
||||
echo '<div class="options_group show_if_composable">';
|
||||
|
||||
woocommerce_wp_text_input([
|
||||
'id' => '_composable_selection_limit',
|
||||
'label' => __('Selection Limit', 'wc-composable-product'),
|
||||
'description' => __('Maximum number of items customers can select. Leave empty to use global default.', 'wc-composable-product'),
|
||||
'desc_tip' => true,
|
||||
'type' => 'number',
|
||||
'custom_attributes' => [
|
||||
'min' => '1',
|
||||
'step' => '1',
|
||||
],
|
||||
]);
|
||||
|
||||
woocommerce_wp_select([
|
||||
'id' => '_composable_pricing_mode',
|
||||
'label' => __('Pricing Mode', 'wc-composable-product'),
|
||||
'description' => __('How to calculate the price. Leave empty to use global default.', 'wc-composable-product'),
|
||||
'desc_tip' => true,
|
||||
'options' => [
|
||||
'' => __('Use global default', 'wc-composable-product'),
|
||||
'sum' => __('Sum of selected products', 'wc-composable-product'),
|
||||
'fixed' => __('Fixed price', 'wc-composable-product'),
|
||||
],
|
||||
]);
|
||||
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add product data panel
|
||||
*/
|
||||
public function add_product_data_panel() {
|
||||
global $post;
|
||||
?>
|
||||
<div id="composable_product_data" class="panel woocommerce_options_panel hidden">
|
||||
<div class="options_group">
|
||||
<?php
|
||||
woocommerce_wp_select([
|
||||
'id' => '_composable_criteria_type',
|
||||
'label' => __('Selection Criteria', 'wc-composable-product'),
|
||||
'description' => __('How to select available products.', 'wc-composable-product'),
|
||||
'desc_tip' => true,
|
||||
'options' => [
|
||||
'category' => __('By Category', 'wc-composable-product'),
|
||||
'tag' => __('By Tag', 'wc-composable-product'),
|
||||
'sku' => __('By SKU', 'wc-composable-product'),
|
||||
],
|
||||
'value' => get_post_meta($post->ID, '_composable_criteria_type', true) ?: 'category',
|
||||
]);
|
||||
?>
|
||||
</div>
|
||||
|
||||
<div class="options_group composable_criteria_group" id="composable_criteria_category">
|
||||
<p class="form-field">
|
||||
<label for="_composable_categories"><?php esc_html_e('Select Categories', 'wc-composable-product'); ?></label>
|
||||
<select id="_composable_categories" name="_composable_categories[]" class="wc-enhanced-select" multiple="multiple" style="width: 50%;">
|
||||
<?php
|
||||
$selected_categories = get_post_meta($post->ID, '_composable_categories', true) ?: [];
|
||||
$categories = get_terms(['taxonomy' => 'product_cat', 'hide_empty' => false]);
|
||||
foreach ($categories as $category) {
|
||||
printf(
|
||||
'<option value="%s" %s>%s</option>',
|
||||
esc_attr($category->term_id),
|
||||
selected(in_array($category->term_id, (array) $selected_categories), true, false),
|
||||
esc_html($category->name)
|
||||
);
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<span class="description"><?php esc_html_e('Select product categories to include.', 'wc-composable-product'); ?></span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="options_group composable_criteria_group" id="composable_criteria_tag" style="display: none;">
|
||||
<p class="form-field">
|
||||
<label for="_composable_tags"><?php esc_html_e('Select Tags', 'wc-composable-product'); ?></label>
|
||||
<select id="_composable_tags" name="_composable_tags[]" class="wc-enhanced-select" multiple="multiple" style="width: 50%;">
|
||||
<?php
|
||||
$selected_tags = get_post_meta($post->ID, '_composable_tags', true) ?: [];
|
||||
$tags = get_terms(['taxonomy' => 'product_tag', 'hide_empty' => false]);
|
||||
foreach ($tags as $tag) {
|
||||
printf(
|
||||
'<option value="%s" %s>%s</option>',
|
||||
esc_attr($tag->term_id),
|
||||
selected(in_array($tag->term_id, (array) $selected_tags), true, false),
|
||||
esc_html($tag->name)
|
||||
);
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<span class="description"><?php esc_html_e('Select product tags to include.', 'wc-composable-product'); ?></span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="options_group composable_criteria_group" id="composable_criteria_sku" style="display: none;">
|
||||
<?php
|
||||
woocommerce_wp_textarea_input([
|
||||
'id' => '_composable_skus',
|
||||
'label' => __('Product SKUs', 'wc-composable-product'),
|
||||
'description' => __('Enter product SKUs separated by commas.', 'wc-composable-product'),
|
||||
'desc_tip' => true,
|
||||
'placeholder' => __('SKU-1, SKU-2, SKU-3', 'wc-composable-product'),
|
||||
]);
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Save product data
|
||||
*
|
||||
* @param int $post_id Post ID
|
||||
*/
|
||||
public function save_product_data($post_id) {
|
||||
// Save selection limit
|
||||
$selection_limit = isset($_POST['_composable_selection_limit']) ? absint($_POST['_composable_selection_limit']) : '';
|
||||
update_post_meta($post_id, '_composable_selection_limit', $selection_limit);
|
||||
|
||||
// Save pricing mode
|
||||
$pricing_mode = isset($_POST['_composable_pricing_mode']) ? sanitize_text_field($_POST['_composable_pricing_mode']) : '';
|
||||
update_post_meta($post_id, '_composable_pricing_mode', $pricing_mode);
|
||||
|
||||
// Save criteria type
|
||||
$criteria_type = isset($_POST['_composable_criteria_type']) ? sanitize_text_field($_POST['_composable_criteria_type']) : 'category';
|
||||
update_post_meta($post_id, '_composable_criteria_type', $criteria_type);
|
||||
|
||||
// Save categories
|
||||
$categories = isset($_POST['_composable_categories']) ? array_map('absint', $_POST['_composable_categories']) : [];
|
||||
update_post_meta($post_id, '_composable_categories', $categories);
|
||||
|
||||
// Save tags
|
||||
$tags = isset($_POST['_composable_tags']) ? array_map('absint', $_POST['_composable_tags']) : [];
|
||||
update_post_meta($post_id, '_composable_tags', $tags);
|
||||
|
||||
// Save SKUs
|
||||
$skus = isset($_POST['_composable_skus']) ? sanitize_textarea_field($_POST['_composable_skus']) : '';
|
||||
update_post_meta($post_id, '_composable_skus', $skus);
|
||||
}
|
||||
}
|
||||
108
includes/Admin/Settings.php
Normal file
108
includes/Admin/Settings.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin Settings
|
||||
*
|
||||
* @package WC_Composable_Product
|
||||
*/
|
||||
|
||||
namespace WC_Composable_Product\Admin;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Settings class
|
||||
*/
|
||||
class Settings extends \WC_Settings_Page {
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->id = 'composable_products';
|
||||
$this->label = __('Composable Products', 'wc-composable-product');
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get settings array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_settings() {
|
||||
$settings = [
|
||||
[
|
||||
'title' => __('Composable Products Settings', 'wc-composable-product'),
|
||||
'type' => 'title',
|
||||
'desc' => __('Configure default settings for composable products.', 'wc-composable-product'),
|
||||
'id' => 'wc_composable_settings',
|
||||
],
|
||||
[
|
||||
'title' => __('Default Selection Limit', 'wc-composable-product'),
|
||||
'desc' => __('Default number of items customers can select.', 'wc-composable-product'),
|
||||
'id' => 'wc_composable_default_limit',
|
||||
'type' => 'number',
|
||||
'default' => '5',
|
||||
'custom_attributes' => [
|
||||
'min' => '1',
|
||||
'step' => '1',
|
||||
],
|
||||
'desc_tip' => true,
|
||||
],
|
||||
[
|
||||
'title' => __('Default Pricing Mode', 'wc-composable-product'),
|
||||
'desc' => __('How to calculate the price of composable products.', 'wc-composable-product'),
|
||||
'id' => 'wc_composable_default_pricing',
|
||||
'type' => 'select',
|
||||
'default' => 'sum',
|
||||
'options' => [
|
||||
'sum' => __('Sum of selected products', 'wc-composable-product'),
|
||||
'fixed' => __('Fixed price', 'wc-composable-product'),
|
||||
],
|
||||
'desc_tip' => true,
|
||||
],
|
||||
[
|
||||
'title' => __('Show Product Images', 'wc-composable-product'),
|
||||
'desc' => __('Display product images in the selection interface.', 'wc-composable-product'),
|
||||
'id' => 'wc_composable_show_images',
|
||||
'type' => 'checkbox',
|
||||
'default' => 'yes',
|
||||
],
|
||||
[
|
||||
'title' => __('Show Product Prices', 'wc-composable-product'),
|
||||
'desc' => __('Display individual product prices in the selection interface.', 'wc-composable-product'),
|
||||
'id' => 'wc_composable_show_prices',
|
||||
'type' => 'checkbox',
|
||||
'default' => 'yes',
|
||||
],
|
||||
[
|
||||
'title' => __('Show Total Price', 'wc-composable-product'),
|
||||
'desc' => __('Display the total price as customers make selections.', 'wc-composable-product'),
|
||||
'id' => 'wc_composable_show_total',
|
||||
'type' => 'checkbox',
|
||||
'default' => 'yes',
|
||||
],
|
||||
[
|
||||
'type' => 'sectionend',
|
||||
'id' => 'wc_composable_settings',
|
||||
],
|
||||
];
|
||||
|
||||
return apply_filters('wc_composable_settings', $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the settings
|
||||
*/
|
||||
public function output() {
|
||||
$settings = $this->get_settings();
|
||||
\WC_Admin_Settings::output_fields($settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save settings
|
||||
*/
|
||||
public function save() {
|
||||
$settings = $this->get_settings();
|
||||
\WC_Admin_Settings::save_fields($settings);
|
||||
}
|
||||
}
|
||||
181
includes/Cart_Handler.php
Normal file
181
includes/Cart_Handler.php
Normal file
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/**
|
||||
* Cart Handler
|
||||
*
|
||||
* @package WC_Composable_Product
|
||||
*/
|
||||
|
||||
namespace WC_Composable_Product;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Cart Handler Class
|
||||
*
|
||||
* Handles adding composable products to cart and calculating prices
|
||||
*/
|
||||
class Cart_Handler {
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter('woocommerce_add_to_cart_validation', [$this, 'validate_add_to_cart'], 10, 3);
|
||||
add_filter('woocommerce_add_cart_item_data', [$this, 'add_cart_item_data'], 10, 2);
|
||||
add_filter('woocommerce_get_cart_item_from_session', [$this, 'get_cart_item_from_session'], 10, 2);
|
||||
add_filter('woocommerce_get_item_data', [$this, 'display_cart_item_data'], 10, 2);
|
||||
add_action('woocommerce_before_calculate_totals', [$this, 'calculate_cart_item_price']);
|
||||
add_action('woocommerce_single_product_summary', [$this, 'render_product_selector'], 25);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render product selector on product page
|
||||
*/
|
||||
public function render_product_selector() {
|
||||
global $product;
|
||||
|
||||
if ($product && $product->get_type() === 'composable') {
|
||||
Product_Selector::render($product);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate add to cart
|
||||
*
|
||||
* @param bool $passed Validation status
|
||||
* @param int $product_id Product ID
|
||||
* @param int $quantity Quantity
|
||||
* @return bool
|
||||
*/
|
||||
public function validate_add_to_cart($passed, $product_id, $quantity) {
|
||||
$product = wc_get_product($product_id);
|
||||
|
||||
if (!$product || $product->get_type() !== 'composable') {
|
||||
return $passed;
|
||||
}
|
||||
|
||||
// Check if selected products are provided
|
||||
if (!isset($_POST['composable_products']) || empty($_POST['composable_products'])) {
|
||||
wc_add_notice(__('Please select at least one product.', 'wc-composable-product'), 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
$selected_products = array_map('absint', $_POST['composable_products']);
|
||||
$selection_limit = $product->get_selection_limit();
|
||||
|
||||
// Validate selection limit
|
||||
if (count($selected_products) > $selection_limit) {
|
||||
/* translators: %d: selection limit */
|
||||
wc_add_notice(sprintf(__('You can select a maximum of %d products.', 'wc-composable-product'), $selection_limit), 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (count($selected_products) === 0) {
|
||||
wc_add_notice(__('Please select at least one product.', 'wc-composable-product'), 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate that selected products are valid
|
||||
$available_products = $product->get_available_products();
|
||||
$available_ids = array_map(function($p) {
|
||||
return $p->get_id();
|
||||
}, $available_products);
|
||||
|
||||
foreach ($selected_products as $selected_id) {
|
||||
if (!in_array($selected_id, $available_ids)) {
|
||||
wc_add_notice(__('One or more selected products are not available.', 'wc-composable-product'), 'error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $passed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add cart item data
|
||||
*
|
||||
* @param array $cart_item_data Cart item data
|
||||
* @param int $product_id Product ID
|
||||
* @return array
|
||||
*/
|
||||
public function add_cart_item_data($cart_item_data, $product_id) {
|
||||
$product = wc_get_product($product_id);
|
||||
|
||||
if (!$product || $product->get_type() !== 'composable') {
|
||||
return $cart_item_data;
|
||||
}
|
||||
|
||||
if (isset($_POST['composable_products']) && !empty($_POST['composable_products'])) {
|
||||
$selected_products = array_map('absint', $_POST['composable_products']);
|
||||
$cart_item_data['composable_products'] = $selected_products;
|
||||
|
||||
// Make cart item unique
|
||||
$cart_item_data['unique_key'] = md5(json_encode($selected_products) . time());
|
||||
}
|
||||
|
||||
return $cart_item_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart item from session
|
||||
*
|
||||
* @param array $cart_item Cart item
|
||||
* @param array $values Values from session
|
||||
* @return array
|
||||
*/
|
||||
public function get_cart_item_from_session($cart_item, $values) {
|
||||
if (isset($values['composable_products'])) {
|
||||
$cart_item['composable_products'] = $values['composable_products'];
|
||||
}
|
||||
|
||||
return $cart_item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display cart item data
|
||||
*
|
||||
* @param array $item_data Item data
|
||||
* @param array $cart_item Cart item
|
||||
* @return array
|
||||
*/
|
||||
public function display_cart_item_data($item_data, $cart_item) {
|
||||
if (isset($cart_item['composable_products']) && !empty($cart_item['composable_products'])) {
|
||||
$product_names = [];
|
||||
foreach ($cart_item['composable_products'] as $product_id) {
|
||||
$product = wc_get_product($product_id);
|
||||
if ($product) {
|
||||
$product_names[] = $product->get_name();
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($product_names)) {
|
||||
$item_data[] = [
|
||||
'key' => __('Selected Products', 'wc-composable-product'),
|
||||
'value' => implode(', ', $product_names),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $item_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate cart item price
|
||||
*
|
||||
* @param \WC_Cart $cart Cart object
|
||||
*/
|
||||
public function calculate_cart_item_price($cart) {
|
||||
if (is_admin() && !defined('DOING_AJAX')) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
|
||||
if (isset($cart_item['data']) && $cart_item['data']->get_type() === 'composable') {
|
||||
if (isset($cart_item['composable_products'])) {
|
||||
$product = $cart_item['data'];
|
||||
$price = $product->calculate_composed_price($cart_item['composable_products']);
|
||||
$cart_item['data']->set_price($price);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
216
includes/Plugin.php
Normal file
216
includes/Plugin.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Include required files
|
||||
*/
|
||||
private function includes() {
|
||||
require_once WC_COMPOSABLE_PRODUCT_PATH . 'includes/Admin/Settings.php';
|
||||
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/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'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
65
includes/Product_Selector.php
Normal file
65
includes/Product_Selector.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Product Selector
|
||||
*
|
||||
* @package WC_Composable_Product
|
||||
*/
|
||||
|
||||
namespace WC_Composable_Product;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Product Selector Class
|
||||
*
|
||||
* Handles rendering the product selection interface
|
||||
*/
|
||||
class Product_Selector {
|
||||
/**
|
||||
* Render product selector
|
||||
*
|
||||
* @param Product_Type $product Composable product
|
||||
*/
|
||||
public static function render($product) {
|
||||
if (!$product || $product->get_type() !== 'composable') {
|
||||
return;
|
||||
}
|
||||
|
||||
$available_products = $product->get_available_products();
|
||||
$selection_limit = $product->get_selection_limit();
|
||||
$pricing_mode = $product->get_pricing_mode();
|
||||
|
||||
$show_images = get_option('wc_composable_show_images', 'yes') === 'yes';
|
||||
$show_prices = get_option('wc_composable_show_prices', 'yes') === 'yes';
|
||||
$show_total = get_option('wc_composable_show_total', 'yes') === 'yes';
|
||||
|
||||
// Prepare product data for template
|
||||
$products_data = [];
|
||||
foreach ($available_products as $available_product) {
|
||||
$products_data[] = [
|
||||
'id' => $available_product->get_id(),
|
||||
'name' => $available_product->get_name(),
|
||||
'price' => $available_product->get_price(),
|
||||
'price_html' => $available_product->get_price_html(),
|
||||
'image_url' => wp_get_attachment_image_url($available_product->get_image_id(), 'thumbnail'),
|
||||
'permalink' => $available_product->get_permalink(),
|
||||
];
|
||||
}
|
||||
|
||||
$context = [
|
||||
'product_id' => $product->get_id(),
|
||||
'products' => $products_data,
|
||||
'selection_limit' => $selection_limit,
|
||||
'pricing_mode' => $pricing_mode,
|
||||
'show_images' => $show_images,
|
||||
'show_prices' => $show_prices,
|
||||
'show_total' => $show_total,
|
||||
'fixed_price' => $product->get_price(),
|
||||
'currency_symbol' => get_woocommerce_currency_symbol(),
|
||||
];
|
||||
|
||||
// Render template
|
||||
$plugin = Plugin::instance();
|
||||
echo $plugin->render_template('product-selector.twig', $context);
|
||||
}
|
||||
}
|
||||
213
includes/Product_Type.php
Normal file
213
includes/Product_Type.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
/**
|
||||
* Composable Product Type
|
||||
*
|
||||
* @package WC_Composable_Product
|
||||
*/
|
||||
|
||||
namespace WC_Composable_Product;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Composable Product Type Class
|
||||
*/
|
||||
class Product_Type extends \WC_Product {
|
||||
/**
|
||||
* Product type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $product_type = 'composable';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param mixed $product Product ID or object
|
||||
*/
|
||||
public function __construct($product = 0) {
|
||||
$this->supports[] = 'ajax_add_to_cart';
|
||||
parent::__construct($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_type() {
|
||||
return 'composable';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get selection limit
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_selection_limit() {
|
||||
$limit = $this->get_meta('_composable_selection_limit', true);
|
||||
if (empty($limit)) {
|
||||
$limit = get_option('wc_composable_default_limit', 5);
|
||||
}
|
||||
return absint($limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pricing mode
|
||||
*
|
||||
* @return string 'fixed' or 'sum'
|
||||
*/
|
||||
public function get_pricing_mode() {
|
||||
$mode = $this->get_meta('_composable_pricing_mode', true);
|
||||
if (empty($mode)) {
|
||||
$mode = get_option('wc_composable_default_pricing', 'sum');
|
||||
}
|
||||
return $mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product selection criteria
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_selection_criteria() {
|
||||
return [
|
||||
'type' => $this->get_meta('_composable_criteria_type', true) ?: 'category',
|
||||
'categories' => $this->get_meta('_composable_categories', true) ?: [],
|
||||
'tags' => $this->get_meta('_composable_tags', true) ?: [],
|
||||
'skus' => $this->get_meta('_composable_skus', true) ?: '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if product is purchasable
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_purchasable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if product is sold individually
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_sold_individually() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available products based on criteria
|
||||
*
|
||||
* @return array Array of WC_Product objects
|
||||
*/
|
||||
public function get_available_products() {
|
||||
$criteria = $this->get_selection_criteria();
|
||||
$args = [
|
||||
'post_type' => 'product',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'tax_query' => [],
|
||||
];
|
||||
|
||||
// Exclude composable products from selection
|
||||
$args['meta_query'] = [
|
||||
[
|
||||
'key' => '_product_type',
|
||||
'value' => 'composable',
|
||||
'compare' => '!=',
|
||||
],
|
||||
];
|
||||
|
||||
switch ($criteria['type']) {
|
||||
case 'category':
|
||||
if (!empty($criteria['categories'])) {
|
||||
$args['tax_query'][] = [
|
||||
'taxonomy' => 'product_cat',
|
||||
'field' => 'term_id',
|
||||
'terms' => $criteria['categories'],
|
||||
'operator' => 'IN',
|
||||
];
|
||||
}
|
||||
break;
|
||||
|
||||
case 'tag':
|
||||
if (!empty($criteria['tags'])) {
|
||||
$args['tax_query'][] = [
|
||||
'taxonomy' => 'product_tag',
|
||||
'field' => 'term_id',
|
||||
'terms' => $criteria['tags'],
|
||||
'operator' => 'IN',
|
||||
];
|
||||
}
|
||||
break;
|
||||
|
||||
case 'sku':
|
||||
if (!empty($criteria['skus'])) {
|
||||
$skus = array_map('trim', explode(',', $criteria['skus']));
|
||||
$args['meta_query'][] = [
|
||||
'key' => '_sku',
|
||||
'value' => $skus,
|
||||
'compare' => 'IN',
|
||||
];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$query = new \WP_Query($args);
|
||||
$products = [];
|
||||
|
||||
if ($query->have_posts()) {
|
||||
foreach ($query->posts as $post) {
|
||||
$product = wc_get_product($post->ID);
|
||||
if ($product && $product->is_in_stock() && $product->is_purchasable()) {
|
||||
$products[] = $product;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wp_reset_postdata();
|
||||
return $products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate price based on selected products
|
||||
*
|
||||
* @param array $selected_products Array of product IDs
|
||||
* @return float
|
||||
*/
|
||||
public function calculate_composed_price($selected_products) {
|
||||
$pricing_mode = $this->get_pricing_mode();
|
||||
|
||||
if ($pricing_mode === 'fixed') {
|
||||
return floatval($this->get_regular_price());
|
||||
}
|
||||
|
||||
$total = 0;
|
||||
foreach ($selected_products as $product_id) {
|
||||
$product = wc_get_product($product_id);
|
||||
if ($product) {
|
||||
$total += floatval($product->get_price());
|
||||
}
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to cart validation
|
||||
*
|
||||
* @param int $product_id Product ID
|
||||
* @param int $quantity Quantity
|
||||
* @param int $variation_id Variation ID
|
||||
* @param array $variations Variations
|
||||
* @param array $cart_item_data Cart item data
|
||||
* @return bool
|
||||
*/
|
||||
public function add_to_cart_validation($product_id, $quantity, $variation_id = 0, $variations = [], $cart_item_data = []) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user