You've already forked wc-tier-and-package-prices
Release version 1.1.2 - Catalog button modification
Enhanced package quantity restriction enforcement by replacing "Add to Cart" buttons with "View Options" links on catalog pages for products with quantity restrictions. This prevents customers from attempting to add restricted products directly from shop/category pages. Changes: - Added catalog button modification for restricted products - Implemented "View Options" button with eye icon styling - Created has_quantity_restriction() helper method - Extended CSS loading to all WooCommerce pages - Added modify_catalog_add_to_cart_button() filter method - Updated translations with 2 new strings (en_US, de_DE, de_CH_informal) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,11 +15,19 @@ class WC_TPP_Frontend {
|
||||
add_action('woocommerce_after_add_to_cart_button', array($this, 'display_pricing_table_after'), 10);
|
||||
add_action('woocommerce_single_product_summary', array($this, 'display_pricing_table_after_price'), 15);
|
||||
add_action('woocommerce_before_add_to_cart_quantity', array($this, 'maybe_hide_quantity_input'));
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
public function enqueue_scripts() {
|
||||
if (is_product()) {
|
||||
// Enqueue CSS on all WooCommerce pages (for catalog buttons and cart)
|
||||
if (is_woocommerce() || is_cart() || is_checkout() || is_product()) {
|
||||
wp_enqueue_style('wc-tpp-frontend', WC_TPP_PLUGIN_URL . 'assets/css/frontend.css', array(), WC_TPP_VERSION);
|
||||
}
|
||||
|
||||
// Enqueue JS only on product pages
|
||||
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
|
||||
@@ -126,6 +134,53 @@ class WC_TPP_Frontend {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a product has quantity restrictions enabled
|
||||
*
|
||||
* @param int $product_id Product ID
|
||||
* @return bool
|
||||
*/
|
||||
public static function has_quantity_restriction($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);
|
||||
|
||||
return ($global_restrict || $product_restrict) && !empty($packages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify catalog add to cart button for products with quantity restrictions
|
||||
*
|
||||
* @param string $html Add to cart button HTML
|
||||
* @param WC_Product $product Product object
|
||||
* @return string Modified HTML
|
||||
*/
|
||||
public function modify_catalog_add_to_cart_button($html, $product) {
|
||||
if (!$product || !is_a($product, 'WC_Product')) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$product_id = $product->get_id();
|
||||
|
||||
// Check if product has quantity restrictions
|
||||
if (!self::has_quantity_restriction($product_id)) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
// Replace add to cart button with "View Options" link
|
||||
$product_url = esc_url($product->get_permalink());
|
||||
$button_text = esc_html__('View Options', 'wc-tier-package-prices');
|
||||
|
||||
$new_html = sprintf(
|
||||
'<a href="%s" class="button wc-tpp-view-options product_type_simple" aria-label="%s">%s</a>',
|
||||
$product_url,
|
||||
esc_attr(sprintf(__('View options for %s', 'wc-tier-package-prices'), $product->get_name())),
|
||||
$button_text
|
||||
);
|
||||
|
||||
return $new_html;
|
||||
}
|
||||
}
|
||||
|
||||
new WC_TPP_Frontend();
|
||||
|
||||
Reference in New Issue
Block a user