Skip to content

Instantly share code, notes, and snippets.

@drhema
Created May 12, 2025 12:17
Show Gist options
  • Save drhema/9bfdeeddb96e85ef5476f46cdf4bfc7e to your computer and use it in GitHub Desktop.
Save drhema/9bfdeeddb96e85ef5476f46cdf4bfc7e to your computer and use it in GitHub Desktop.
/**
* Plugin Name: WooCommerce Installation Service Add-on
* Description: Adds installation service option for specific product categories
* Version: 1.1
*/
if (!defined('ABSPATH')) {
exit;
}
// Check if product is in category or its subcategories recursively
function ws_is_in_category_or_sub($product_id, $parent_cat_id) {
$terms = get_the_terms($product_id, 'product_cat');
if (!$terms || is_wp_error($terms)) return false;
foreach ($terms as $term) {
$term_id = $term->term_id;
while ($term_id) {
if ($term_id == $parent_cat_id) return true;
$term_id = get_term($term_id, 'product_cat')->parent;
}
}
return false;
}
// Add the installation checkbox to product page
add_action('woocommerce_before_add_to_cart_button', 'ws_add_installation_checkbox');
function ws_add_installation_checkbox() {
global $product;
// Check if product is in parent category or its subcategories
if (!ws_is_in_category_or_sub($product->get_id(), 89)) {
return;
}
// Get installation product and price
$installation_product = wc_get_product(14868);
$installation_price = $installation_product ? $installation_product->get_price() : 10;
wp_enqueue_script('jquery');
?>
<style>
.tamara-summary-widget__content {
border: 1px solid #e2e2e2;
border-radius: 8px;
padding: 15px;
margin: 20px 0;
background-color: #fff;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.installation-service-wrapper {
display: flex;
align-items: center;
}
.installation-service-wrapper label {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
}
.installation-service-wrapper input[type="checkbox"] {
margin: 0;
}
</style>
<div class="tamara-summary-widget__content">
<div class="installation-service-wrapper">
<label>
<input type="checkbox" name="installation_service" value="yes" id="installation_service">
اضف خدمه التوصيل والتركيب (<?php echo wc_price($installation_price); ?>)
</label>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#installation_service').on('change', function() {
if ($(this).is(':checked')) {
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'add_to_cart'),
data: {
product_id: 14868,
quantity: 1
},
success: function(response) {
if (response.fragments) {
$.each(response.fragments, function(key, value) {
$(key).replaceWith(value);
});
}
}
});
} else {
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'remove_from_cart'),
data: {
cart_item_key: find_installation_key()
},
success: function(response) {
if (response.fragments) {
$.each(response.fragments, function(key, value) {
$(key).replaceWith(value);
});
}
}
});
}
});
function find_installation_key() {
var installation_key = '';
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'get_cart'),
async: false,
success: function(response) {
$.each(response, function(key, item) {
if (item.product_id == 14868) {
installation_key = key;
return false;
}
});
}
});
return installation_key;
}
});
</script>
<?php
}
// Display installation service in cart
add_filter('woocommerce_cart_item_name', 'ws_display_installation_name', 10, 3);
function ws_display_installation_name($name, $cart_item, $cart_item_key) {
if($cart_item['product_id'] == 14868) {
return 'خدمه التوصيل والتركيب';
}
return $name;
}
// Remove quantity selector for installation service
add_filter('woocommerce_cart_item_quantity', 'ws_hide_installation_quantity', 10, 3);
function ws_hide_installation_quantity($html, $cart_item_key, $cart_item ) {
if($cart_item['product_id'] == 14868) {
return '<span class="quantity">1</span>';
}
return $html;
}
// Prevent direct access to installation product
add_action('template_redirect', 'ws_prevent_installation_access');
function ws_prevent_installation_access() {
if(!is_admin() && is_single() && get_the_ID() == 14868) {
wp_redirect(home_url());
exit();
}
}
// Keep installation product quantity at 1
add_filter('woocommerce_stock_amount', 'ws_installation_quantity_limit', 10, 3);
function ws_installation_quantity_limit($quantity, $product_id = null) {
if($product_id == 14868) {
return 1;
}
return $quantity;
}
// Add custom meta to order
add_action('woocommerce_checkout_create_order_line_item', 'ws_add_installation_meta', 10, 4);
function ws_add_installation_meta($item, $cart_item_key, $values, $order) {
if($values['product_id'] == 14868) {
$item->add_meta_data('_is_installation_service', 'yes');
}
}
// Check installation checkbox if product is in cart
add_action('woocommerce_before_single_product', 'ws_check_installation_in_cart');
function ws_check_installation_in_cart() {
if(WC()->cart) {
foreach(WC()->cart->get_cart() as $cart_item) {
if($cart_item['product_id'] == 14868) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#installation_service').prop('checked', true);
});
</script>
<?php
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment