You've already forked wc-tier-and-package-prices
Release version 1.2.0 - Add complete variable product support
This major feature release adds full support for WooCommerce variable products with variation-level pricing configuration. ## Key Features - Each product variation can have independent tier and package pricing - AJAX-based dynamic pricing table loading on variation selection - Admin UI integrated into WooCommerce variation panels - Full backward compatibility with existing simple product functionality - WooCommerce Blocks compatibility maintained ## Implementation Highlights - Effective ID pattern throughout codebase for variation handling - Variation-specific meta boxes with field prefix support - Template system updated to support both simple and variation products - JavaScript enhancements for variation selector integration - Cart logic updated to handle variation pricing correctly ## Files Changed - Core: wc-tier-and-package-prices.php (version 1.2.0), composer.json - Cart: includes/class-wc-tpp-cart.php (effective ID logic) - Frontend: includes/class-wc-tpp-frontend.php (AJAX endpoint, variation detection) - Admin: includes/class-wc-tpp-product-meta.php (variation hooks and methods) - Templates: templates/admin/*.twig (field prefix support, table structure) - JavaScript: assets/js/*.js (variation support) - Documentation: CHANGELOG.md, README.md, CLAUDE.md 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -40,6 +40,8 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
|
||||
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
|
||||
$product_id = $cart_item['product_id'];
|
||||
$variation_id = isset($cart_item['variation_id']) ? absint($cart_item['variation_id']) : 0;
|
||||
$effective_id = $variation_id > 0 ? $variation_id : $product_id;
|
||||
$quantity = $cart_item['quantity'];
|
||||
$product = $cart_item['data'];
|
||||
|
||||
@@ -48,10 +50,10 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for exact package match first
|
||||
// Check for exact package match first (use effective ID for variations)
|
||||
$package_price = null;
|
||||
if (get_option('wc_tpp_enable_package_pricing') === 'yes') {
|
||||
$package_price = WC_TPP_Frontend::get_package_price($product_id, $quantity);
|
||||
$package_price = WC_TPP_Frontend::get_package_price($effective_id, $quantity, $variation_id);
|
||||
}
|
||||
|
||||
if ($package_price !== null) {
|
||||
@@ -62,9 +64,9 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
WC()->cart->cart_contents[$cart_item_key]['wc_tpp_pricing_type'] = 'package';
|
||||
WC()->cart->cart_contents[$cart_item_key]['wc_tpp_total_price'] = $package_price;
|
||||
} else {
|
||||
// Apply tier pricing if no package match
|
||||
// Apply tier pricing if no package match (use effective ID for variations)
|
||||
if (get_option('wc_tpp_enable_tier_pricing') === 'yes') {
|
||||
$tier_price = WC_TPP_Frontend::get_tier_price($product_id, $quantity);
|
||||
$tier_price = WC_TPP_Frontend::get_tier_price($effective_id, $quantity, $variation_id);
|
||||
if ($tier_price !== null) {
|
||||
$product->set_price($tier_price);
|
||||
// Store pricing information in cart item for display
|
||||
@@ -99,16 +101,20 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
}
|
||||
|
||||
public function validate_package_quantity($passed, $product_id, $quantity) {
|
||||
// Check if restriction is enabled globally or for this product
|
||||
// Check for variation ID in request (for variable products)
|
||||
$variation_id = isset($_REQUEST['variation_id']) ? absint($_REQUEST['variation_id']) : 0;
|
||||
$effective_id = $variation_id > 0 ? $variation_id : $product_id;
|
||||
|
||||
// Check if restriction is enabled globally or for this product/variation
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
$product_restrict = get_post_meta($effective_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
|
||||
if (!$global_restrict && !$product_restrict) {
|
||||
return $passed;
|
||||
}
|
||||
|
||||
// Get packages for this product
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
// Get packages for this product/variation
|
||||
$packages = get_post_meta($effective_id, '_wc_tpp_packages', true);
|
||||
|
||||
if (empty($packages) || !is_array($packages)) {
|
||||
return $passed;
|
||||
@@ -147,18 +153,20 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
|
||||
public function maybe_hide_cart_quantity_input($product_quantity, $cart_item_key, $cart_item) {
|
||||
$product_id = $cart_item['product_id'];
|
||||
$variation_id = isset($cart_item['variation_id']) ? absint($cart_item['variation_id']) : 0;
|
||||
$effective_id = $variation_id > 0 ? $variation_id : $product_id;
|
||||
|
||||
// Check if restriction is enabled globally or for this product
|
||||
// Check if restriction is enabled globally or for this product/variation
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
$product_restrict = get_post_meta($effective_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
|
||||
// Get packages for this product
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
// Get packages for this product/variation
|
||||
$packages = get_post_meta($effective_id, '_wc_tpp_packages', true);
|
||||
|
||||
// If restriction is enabled and packages exist, show quantity as text only
|
||||
if (($global_restrict || $product_restrict) && !empty($packages)) {
|
||||
return sprintf('<span class="wc-tpp-cart-quantity wc-tpp-restricted-qty" data-product-id="%d">%s</span>',
|
||||
$product_id,
|
||||
$effective_id,
|
||||
$cart_item['quantity']
|
||||
);
|
||||
}
|
||||
@@ -168,18 +176,20 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
|
||||
public function maybe_hide_mini_cart_quantity_input($product_quantity, $cart_item, $cart_item_key) {
|
||||
$product_id = $cart_item['product_id'];
|
||||
$variation_id = isset($cart_item['variation_id']) ? absint($cart_item['variation_id']) : 0;
|
||||
$effective_id = $variation_id > 0 ? $variation_id : $product_id;
|
||||
|
||||
// Check if restriction is enabled globally or for this product
|
||||
// Check if restriction is enabled globally or for this product/variation
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
$product_restrict = get_post_meta($effective_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
|
||||
// Get packages for this product
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
// Get packages for this product/variation
|
||||
$packages = get_post_meta($effective_id, '_wc_tpp_packages', true);
|
||||
|
||||
// If restriction is enabled and packages exist, show quantity as text only
|
||||
if (($global_restrict || $product_restrict) && !empty($packages)) {
|
||||
return sprintf('<span class="wc-tpp-cart-quantity wc-tpp-restricted-qty" data-product-id="%d">%s ×</span>',
|
||||
$product_id,
|
||||
$effective_id,
|
||||
$cart_item['quantity']
|
||||
);
|
||||
}
|
||||
@@ -196,12 +206,15 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
$restricted_products = array();
|
||||
foreach (WC()->cart->get_cart() as $cart_item) {
|
||||
$product_id = $cart_item['product_id'];
|
||||
$variation_id = isset($cart_item['variation_id']) ? absint($cart_item['variation_id']) : 0;
|
||||
$effective_id = $variation_id > 0 ? $variation_id : $product_id;
|
||||
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
$product_restrict = get_post_meta($effective_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
$packages = get_post_meta($effective_id, '_wc_tpp_packages', true);
|
||||
|
||||
if (($global_restrict || $product_restrict) && !empty($packages)) {
|
||||
$restricted_products[] = $product_id;
|
||||
$restricted_products[] = $effective_id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +239,7 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
* Make quantity non-editable for restricted products in WooCommerce blocks
|
||||
*
|
||||
* @param bool $editable Whether the quantity is editable
|
||||
* @param WC_Product $product Product object
|
||||
* @param WC_Product $product Product object (can be variation)
|
||||
* @return bool
|
||||
*/
|
||||
public function block_quantity_editable($editable, $product) {
|
||||
@@ -241,9 +254,12 @@ if (!class_exists('WC_TPP_Cart')) {
|
||||
return $editable;
|
||||
}
|
||||
|
||||
// For variations, use the variation ID directly (get_id() returns variation ID for WC_Product_Variation)
|
||||
$effective_id = $product_id;
|
||||
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
$product_restrict = get_post_meta($effective_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
$packages = get_post_meta($effective_id, '_wc_tpp_packages', true);
|
||||
|
||||
// If restriction is enabled and packages exist, make quantity non-editable
|
||||
if (($global_restrict || $product_restrict) && !empty($packages)) {
|
||||
|
||||
@@ -19,6 +19,10 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
|
||||
// Modify catalog add to cart button for restricted products
|
||||
add_filter('woocommerce_loop_add_to_cart_link', array($this, 'modify_catalog_add_to_cart_button'), 10, 2);
|
||||
|
||||
// AJAX endpoints for variation pricing
|
||||
add_action('wp_ajax_wc_tpp_get_variation_pricing', array($this, 'ajax_get_variation_pricing'));
|
||||
add_action('wp_ajax_nopriv_wc_tpp_get_variation_pricing', array($this, 'ajax_get_variation_pricing'));
|
||||
}
|
||||
|
||||
public function enqueue_scripts() {
|
||||
@@ -31,8 +35,10 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
if (is_product()) {
|
||||
wp_enqueue_script('wc-tpp-frontend', WC_TPP_PLUGIN_URL . 'assets/js/frontend.js', array('jquery'), WC_TPP_VERSION, true);
|
||||
|
||||
// Localize script with currency settings
|
||||
// Localize script with currency settings and AJAX data
|
||||
wp_localize_script('wc-tpp-frontend', 'wcTppData', array(
|
||||
'ajax_url' => admin_url('admin-ajax.php'),
|
||||
'nonce' => wp_create_nonce('wc_tpp_variation_pricing'),
|
||||
'currency_symbol' => esc_js(get_woocommerce_currency_symbol()),
|
||||
'currency_position' => esc_js(get_option('woocommerce_currency_pos', 'left')),
|
||||
'price_decimals' => absint(wc_get_price_decimals()),
|
||||
@@ -67,6 +73,11 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// For variable products, quantity hiding is handled per-variation via JS
|
||||
if ($product->is_type('variable')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$product_id = $product->get_id();
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
@@ -85,6 +96,13 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// For variable products, show a placeholder that will be populated by JS when variation is selected
|
||||
if ($product->is_type('variable')) {
|
||||
echo '<div class="wc-tpp-pricing-table-container" data-product-type="variable" style="display:none;"></div>';
|
||||
return;
|
||||
}
|
||||
|
||||
// For simple products, display pricing table directly
|
||||
$product_id = $product->get_id();
|
||||
$tiers = get_post_meta($product_id, '_wc_tpp_tiers', true);
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
@@ -103,8 +121,17 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
));
|
||||
}
|
||||
|
||||
public static function get_tier_price($product_id, $quantity) {
|
||||
$tiers = get_post_meta($product_id, '_wc_tpp_tiers', true);
|
||||
/**
|
||||
* Get tier price for a product or variation
|
||||
*
|
||||
* @param int $product_id Product ID (parent for simple, parent for variable)
|
||||
* @param int $quantity Quantity
|
||||
* @param int $variation_id Variation ID (0 for simple products)
|
||||
* @return float|null Tier price or null if not applicable
|
||||
*/
|
||||
public static function get_tier_price($product_id, $quantity, $variation_id = 0) {
|
||||
$effective_id = $variation_id > 0 ? $variation_id : $product_id;
|
||||
$tiers = get_post_meta($effective_id, '_wc_tpp_tiers', true);
|
||||
|
||||
if (empty($tiers) || !is_array($tiers)) {
|
||||
return null;
|
||||
@@ -120,8 +147,17 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
return $applicable_price;
|
||||
}
|
||||
|
||||
public static function get_package_price($product_id, $quantity) {
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
/**
|
||||
* Get package price for a product or variation
|
||||
*
|
||||
* @param int $product_id Product ID (parent for simple, parent for variable)
|
||||
* @param int $quantity Quantity
|
||||
* @param int $variation_id Variation ID (0 for simple products)
|
||||
* @return float|null Package price or null if not applicable
|
||||
*/
|
||||
public static function get_package_price($product_id, $quantity, $variation_id = 0) {
|
||||
$effective_id = $variation_id > 0 ? $variation_id : $product_id;
|
||||
$packages = get_post_meta($effective_id, '_wc_tpp_packages', true);
|
||||
|
||||
if (empty($packages) || !is_array($packages)) {
|
||||
return null;
|
||||
@@ -164,8 +200,24 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
|
||||
$product_id = $product->get_id();
|
||||
|
||||
// Check if product has quantity restrictions
|
||||
if (!self::has_quantity_restriction($product_id)) {
|
||||
// For variable products, check if ANY variation has restrictions
|
||||
// For simple products, check the product itself
|
||||
$has_restriction = false;
|
||||
|
||||
if ($product->is_type('variable')) {
|
||||
// Check if any variation has package restrictions
|
||||
$variations = $product->get_available_variations();
|
||||
foreach ($variations as $variation_data) {
|
||||
if (self::has_quantity_restriction($variation_data['variation_id'])) {
|
||||
$has_restriction = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$has_restriction = self::has_quantity_restriction($product_id);
|
||||
}
|
||||
|
||||
if (!$has_restriction) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
@@ -173,15 +225,72 @@ if (!class_exists('WC_TPP_Frontend')) {
|
||||
$product_url = esc_url($product->get_permalink());
|
||||
$button_text = esc_html__('View Options', 'wc-tier-package-prices');
|
||||
|
||||
// Use correct product type class
|
||||
$product_type_class = $product->is_type('variable') ? 'product_type_variable' : 'product_type_simple';
|
||||
|
||||
$new_html = sprintf(
|
||||
'<a href="%s" class="button wc-tpp-view-options product_type_simple" aria-label="%s">%s</a>',
|
||||
'<a href="%s" class="button wc-tpp-view-options %s" aria-label="%s">%s</a>',
|
||||
$product_url,
|
||||
esc_attr($product_type_class),
|
||||
esc_attr(sprintf(__('View options for %s', 'wc-tier-package-prices'), $product->get_name())),
|
||||
$button_text
|
||||
);
|
||||
|
||||
return $new_html;
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX handler to get variation pricing data
|
||||
*/
|
||||
public function ajax_get_variation_pricing() {
|
||||
// Verify nonce
|
||||
check_ajax_referer('wc_tpp_variation_pricing', 'nonce');
|
||||
|
||||
$variation_id = isset($_POST['variation_id']) ? absint($_POST['variation_id']) : 0;
|
||||
|
||||
if (!$variation_id) {
|
||||
wp_send_json_error(array('message' => __('Invalid variation ID', 'wc-tier-package-prices')));
|
||||
}
|
||||
|
||||
// Get variation data
|
||||
$variation = wc_get_product($variation_id);
|
||||
|
||||
if (!$variation || !$variation->is_type('variation')) {
|
||||
wp_send_json_error(array('message' => __('Variation not found', 'wc-tier-package-prices')));
|
||||
}
|
||||
|
||||
// Get tier and package pricing
|
||||
$tiers = get_post_meta($variation_id, '_wc_tpp_tiers', true);
|
||||
$packages = get_post_meta($variation_id, '_wc_tpp_packages', true);
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($variation_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
|
||||
if (empty($tiers) && empty($packages)) {
|
||||
// No pricing data for this variation
|
||||
wp_send_json_success(array(
|
||||
'has_pricing' => false,
|
||||
'html' => ''
|
||||
));
|
||||
}
|
||||
|
||||
// Render the pricing table HTML
|
||||
ob_start();
|
||||
WC_TPP_Template_Loader::get_instance()->display('frontend/pricing-table.twig', array(
|
||||
'product' => $variation,
|
||||
'tiers' => $tiers,
|
||||
'packages' => $packages,
|
||||
'restrict_to_packages' => $global_restrict || $product_restrict
|
||||
));
|
||||
$html = ob_get_clean();
|
||||
|
||||
wp_send_json_success(array(
|
||||
'has_pricing' => true,
|
||||
'html' => $html,
|
||||
'tiers' => $tiers ? $tiers : array(),
|
||||
'packages' => $packages ? $packages : array(),
|
||||
'restrict_to_packages' => $global_restrict || $product_restrict
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
new WC_TPP_Frontend();
|
||||
|
||||
@@ -11,9 +11,14 @@ if (!class_exists('WC_TPP_Product_Meta')) {
|
||||
class WC_TPP_Product_Meta {
|
||||
|
||||
public function __construct() {
|
||||
// Simple product hooks
|
||||
add_action('woocommerce_product_options_pricing', array($this, 'add_tier_pricing_fields'));
|
||||
add_action('woocommerce_product_options_pricing', array($this, 'add_package_pricing_fields'));
|
||||
add_action('woocommerce_process_product_meta', array($this, 'save_tier_package_fields'));
|
||||
|
||||
// Variable product variation hooks
|
||||
add_action('woocommerce_variation_options_pricing', array($this, 'add_variation_pricing_fields'), 10, 3);
|
||||
add_action('woocommerce_save_product_variation', array($this, 'save_variation_pricing_fields'), 10, 2);
|
||||
}
|
||||
|
||||
public function add_tier_pricing_fields() {
|
||||
@@ -25,18 +30,28 @@ if (!class_exists('WC_TPP_Product_Meta')) {
|
||||
<span class="description"><?php _e('Set quantity-based pricing tiers. Customers get discounted prices when buying in larger quantities.', 'wc-tier-package-prices'); ?></span>
|
||||
</p>
|
||||
|
||||
<div class="wc-tpp-tiers-container">
|
||||
<?php
|
||||
$tiers = get_post_meta($post->ID, '_wc_tpp_tiers', true);
|
||||
if (!is_array($tiers)) {
|
||||
$tiers = array();
|
||||
}
|
||||
<table class="widefat wc-tpp-tiers-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e('Min Quantity', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Price', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Label (optional)', 'wc-tier-package-prices'); ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="wc-tpp-tiers-container">
|
||||
<?php
|
||||
$tiers = get_post_meta($post->ID, '_wc_tpp_tiers', true);
|
||||
if (!is_array($tiers)) {
|
||||
$tiers = array();
|
||||
}
|
||||
|
||||
foreach ($tiers as $index => $tier) {
|
||||
$this->render_tier_row($index, $tier);
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
foreach ($tiers as $index => $tier) {
|
||||
$this->render_tier_row($index, $tier);
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p class="form-field">
|
||||
<button type="button" class="button wc-tpp-add-tier"><?php _e('Add Tier', 'wc-tier-package-prices'); ?></button>
|
||||
@@ -54,18 +69,28 @@ if (!class_exists('WC_TPP_Product_Meta')) {
|
||||
<span class="description"><?php _e('Set fixed-price packages with specific quantities. For example: 10 pieces for $50, 25 pieces for $100.', 'wc-tier-package-prices'); ?></span>
|
||||
</p>
|
||||
|
||||
<div class="wc-tpp-packages-container">
|
||||
<?php
|
||||
$packages = get_post_meta($post->ID, '_wc_tpp_packages', true);
|
||||
if (!is_array($packages)) {
|
||||
$packages = array();
|
||||
}
|
||||
<table class="widefat wc-tpp-packages-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e('Quantity', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Price', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Label (optional)', 'wc-tier-package-prices'); ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="wc-tpp-packages-container">
|
||||
<?php
|
||||
$packages = get_post_meta($post->ID, '_wc_tpp_packages', true);
|
||||
if (!is_array($packages)) {
|
||||
$packages = array();
|
||||
}
|
||||
|
||||
foreach ($packages as $index => $package) {
|
||||
$this->render_package_row($index, $package);
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
foreach ($packages as $index => $package) {
|
||||
$this->render_package_row($index, $package);
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p class="form-field">
|
||||
<button type="button" class="button wc-tpp-add-package"><?php _e('Add Package', 'wc-tier-package-prices'); ?></button>
|
||||
@@ -105,6 +130,135 @@ if (!class_exists('WC_TPP_Product_Meta')) {
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add tier and package pricing fields to product variations
|
||||
*
|
||||
* @param int $loop Position in the loop
|
||||
* @param array $variation_data Variation data
|
||||
* @param WP_Post $variation Variation post object
|
||||
*/
|
||||
public function add_variation_pricing_fields($loop, $variation_data, $variation) {
|
||||
$variation_id = $variation->ID;
|
||||
|
||||
// Retrieve variation-specific data
|
||||
$tiers = get_post_meta($variation_id, '_wc_tpp_tiers', true);
|
||||
$packages = get_post_meta($variation_id, '_wc_tpp_packages', true);
|
||||
$restrict = get_post_meta($variation_id, '_wc_tpp_restrict_to_packages', true);
|
||||
|
||||
if (!is_array($tiers)) {
|
||||
$tiers = array();
|
||||
}
|
||||
if (!is_array($packages)) {
|
||||
$packages = array();
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="form-row form-row-full wc-tpp-variation-pricing" data-variation-loop="<?php echo esc_attr($loop); ?>">
|
||||
<h4><?php _e('Tier & Package Pricing', 'wc-tier-package-prices'); ?></h4>
|
||||
|
||||
<!-- Tier Pricing Section -->
|
||||
<div class="wc-tpp-variation-tiers">
|
||||
<p><strong><?php _e('Tier Pricing', 'wc-tier-package-prices'); ?></strong></p>
|
||||
<table class="widefat wc-tpp-tiers-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e('Min Quantity', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Price', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Label (optional)', 'wc-tier-package-prices'); ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="wc-tpp-tiers-container">
|
||||
<?php foreach ($tiers as $index => $tier) : ?>
|
||||
<?php $this->render_variation_tier_row($loop, $index, $tier); ?>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="button" class="button wc-tpp-add-tier" data-loop="<?php echo esc_attr($loop); ?>">
|
||||
<?php _e('Add Tier', 'wc-tier-package-prices'); ?>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Package Pricing Section -->
|
||||
<div class="wc-tpp-variation-packages" style="margin-top: 15px;">
|
||||
<p><strong><?php _e('Package Pricing', 'wc-tier-package-prices'); ?></strong></p>
|
||||
<table class="widefat wc-tpp-packages-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e('Quantity', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Price', 'wc-tier-package-prices'); ?></th>
|
||||
<th><?php _e('Label (optional)', 'wc-tier-package-prices'); ?></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="wc-tpp-packages-container">
|
||||
<?php foreach ($packages as $index => $package) : ?>
|
||||
<?php $this->render_variation_package_row($loop, $index, $package); ?>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="button" class="button wc-tpp-add-package" data-loop="<?php echo esc_attr($loop); ?>">
|
||||
<?php _e('Add Package', 'wc-tier-package-prices'); ?>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Restriction Checkbox -->
|
||||
<div style="margin-top: 15px;">
|
||||
<?php
|
||||
woocommerce_wp_checkbox(array(
|
||||
'id' => 'wc_tpp_restrict_to_packages_' . $loop,
|
||||
'name' => 'wc_tpp_restrict_to_packages[' . $loop . ']',
|
||||
'label' => __('Restrict to Package Quantities', 'wc-tier-package-prices'),
|
||||
'description' => __('Only allow quantities defined in packages above', 'wc-tier-package-prices'),
|
||||
'value' => $restrict === 'yes' ? 'yes' : 'no',
|
||||
'cbvalue' => 'yes',
|
||||
'wrapper_class' => 'form-row form-row-full'
|
||||
));
|
||||
?>
|
||||
</div>
|
||||
|
||||
<!-- Templates for JavaScript -->
|
||||
<script type="text/html" id="wc-tpp-variation-tier-row-template-<?php echo esc_attr($loop); ?>">
|
||||
<?php $this->render_variation_tier_row($loop, '{{INDEX}}', array('min_qty' => '', 'price' => '', 'label' => '')); ?>
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="wc-tpp-variation-package-row-template-<?php echo esc_attr($loop); ?>">
|
||||
<?php $this->render_variation_package_row($loop, '{{INDEX}}', array('qty' => '', 'price' => '', 'label' => '')); ?>
|
||||
</script>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a tier row for variations
|
||||
*
|
||||
* @param int $loop Variation loop index
|
||||
* @param int $index Tier index
|
||||
* @param array $tier Tier data
|
||||
*/
|
||||
private function render_variation_tier_row($loop, $index, $tier) {
|
||||
WC_TPP_Template_Loader::get_instance()->display('admin/tier-row.twig', array(
|
||||
'index' => $index,
|
||||
'tier' => $tier,
|
||||
'field_prefix' => 'wc_tpp_tiers[' . $loop . ']'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a package row for variations
|
||||
*
|
||||
* @param int $loop Variation loop index
|
||||
* @param int $index Package index
|
||||
* @param array $package Package data
|
||||
*/
|
||||
private function render_variation_package_row($loop, $index, $package) {
|
||||
WC_TPP_Template_Loader::get_instance()->display('admin/package-row.twig', array(
|
||||
'index' => $index,
|
||||
'package' => $package,
|
||||
'field_prefix' => 'wc_tpp_packages[' . $loop . ']'
|
||||
));
|
||||
}
|
||||
|
||||
public function save_tier_package_fields($post_id) {
|
||||
// Verify nonce for security
|
||||
if (!isset($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce($_POST['woocommerce_meta_nonce'], 'woocommerce_save_data')) {
|
||||
@@ -167,6 +321,68 @@ if (!class_exists('WC_TPP_Product_Meta')) {
|
||||
$restrict_to_packages = isset($_POST['_wc_tpp_restrict_to_packages']) ? 'yes' : 'no';
|
||||
update_post_meta($post_id, '_wc_tpp_restrict_to_packages', $restrict_to_packages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save tier and package pricing for variations
|
||||
*
|
||||
* @param int $variation_id Variation ID
|
||||
* @param int $loop Position in loop
|
||||
*/
|
||||
public function save_variation_pricing_fields($variation_id, $loop) {
|
||||
// Security check
|
||||
if (!current_user_can('edit_products')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save tier pricing for this variation
|
||||
if (isset($_POST['wc_tpp_tiers'][$loop])) {
|
||||
$tiers = array();
|
||||
foreach ($_POST['wc_tpp_tiers'][$loop] as $tier) {
|
||||
if (!empty($tier['min_qty']) && !empty($tier['price'])) {
|
||||
$tiers[] = array(
|
||||
'min_qty' => absint($tier['min_qty']),
|
||||
'price' => wc_format_decimal($tier['price']),
|
||||
'label' => sanitize_text_field($tier['label'] ?? '')
|
||||
);
|
||||
}
|
||||
}
|
||||
// Sort by minimum quantity
|
||||
usort($tiers, function($a, $b) {
|
||||
return $a['min_qty'] - $b['min_qty'];
|
||||
});
|
||||
update_post_meta($variation_id, '_wc_tpp_tiers', $tiers);
|
||||
} else {
|
||||
delete_post_meta($variation_id, '_wc_tpp_tiers');
|
||||
}
|
||||
|
||||
// Save package pricing for this variation
|
||||
if (isset($_POST['wc_tpp_packages'][$loop])) {
|
||||
$packages = array();
|
||||
foreach ($_POST['wc_tpp_packages'][$loop] as $package) {
|
||||
if (!empty($package['qty']) && !empty($package['price'])) {
|
||||
$packages[] = array(
|
||||
'qty' => absint($package['qty']),
|
||||
'price' => wc_format_decimal($package['price']),
|
||||
'label' => sanitize_text_field($package['label'] ?? '')
|
||||
);
|
||||
}
|
||||
}
|
||||
// Sort by quantity
|
||||
usort($packages, function($a, $b) {
|
||||
return $a['qty'] - $b['qty'];
|
||||
});
|
||||
update_post_meta($variation_id, '_wc_tpp_packages', $packages);
|
||||
} else {
|
||||
delete_post_meta($variation_id, '_wc_tpp_packages');
|
||||
}
|
||||
|
||||
// Save restriction setting for this variation
|
||||
if (isset($_POST['wc_tpp_restrict_to_packages'][$loop]) && $_POST['wc_tpp_restrict_to_packages'][$loop] === 'yes') {
|
||||
update_post_meta($variation_id, '_wc_tpp_restrict_to_packages', 'yes');
|
||||
} else {
|
||||
delete_post_meta($variation_id, '_wc_tpp_restrict_to_packages');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WC_TPP_Product_Meta();
|
||||
|
||||
Reference in New Issue
Block a user