term_id, 'mandatory_products', true);
// If not array, initialize it
if (!is_array($mandatory_products)) {
$mandatory_products = array();
}
?>
|
|
$mandatory_product) :
$product_id = isset($mandatory_product['product_id']) ? absint($mandatory_product['product_id']) : 0;
$quantity = isset($mandatory_product['quantity']) ? absint($mandatory_product['quantity']) : 1;
$type = isset($mandatory_product['type']) ? sanitize_text_field($mandatory_product['type']) : 'OnePerCart';
$product_title = '';
if ($product_id > 0) {
$product = wc_get_product($product_id);
if ($product) {
$product_title = $product->get_formatted_name();
}
}
?>
|
$product_id) {
// Skip if product ID is empty
if (empty($product_id)) {
continue;
}
$quantity = isset($quantities[$index]) ? max(1, $quantities[$index]) : 1;
$type = isset($types[$index]) ? $types[$index] : 'OnePerCart';
// Validate type
if (!in_array($type, array('OnePerCart', 'OnePerProduct'))) {
$type = 'OnePerCart';
}
$mandatory_products[] = array(
'product_id' => $product_id,
'quantity' => $quantity,
'type' => $type
);
}
}
update_term_meta($term_id, 'mandatory_products', $mandatory_products);
}
/**
* Enqueue admin scripts and styles
*
* @param string $hook Current admin page
*/
public function enqueue_admin_scripts($hook) {
// Only on category edit page
if ('edit-tags.php' !== $hook && 'term.php' !== $hook) {
return;
}
// Check if we're on the product category screen
$screen = get_current_screen();
if (!$screen || 'product_cat' !== $screen->taxonomy) {
return;
}
// WooCommerce admin scripts
wp_enqueue_style('woocommerce_admin_styles');
wp_enqueue_script('wc-enhanced-select');
// Add our custom JS to properly initialize SelectWoo
wp_enqueue_script(
'studiou-wcmp-admin-js',
STUDIOU_WCMP_PLUGIN_URL . 'assets/js/admin.js',
array('jquery', 'wc-enhanced-select', 'wp-util'),
STUDIOU_WCMP_VERSION,
true
);
}
/**
* Add product meta boxes
*/
public function add_product_meta_boxes() {
add_meta_box(
'studiou-wcmp-mandatory-products',
__('Mandatory Products Info', 'studiou-wc-product-cat-manage'),
array($this, 'render_product_meta_box'),
'product',
'side',
'default'
);
}
/**
* Render product meta box
*
* @param WP_Post $post The post object
*/
public function render_product_meta_box($post) {
$product_id = $post->ID;
// Get plugin instance
$plugin = new StudioU_WC_Mandatory_Products();
// Get mandatory products
$mandatory_products = $plugin->get_mandatory_products($product_id);
if (empty($mandatory_products)) {
echo '' . __('No mandatory products set for this product\'s categories.', 'studiou-wc-mandatory-products') . '
';
return;
}
echo '' . __('This product has mandatory products set in its categories:', 'studiou-wc-mandatory-products') . '
';
echo '';
foreach ($mandatory_products as $mandatory_product) {
$product_id = absint($mandatory_product['product_id']);
$quantity = absint($mandatory_product['quantity']);
$type = sanitize_text_field($mandatory_product['type']);
$mandatory_product_obj = wc_get_product($product_id);
if ($mandatory_product_obj) {
echo '- ';
echo sprintf(
'%s x %d (%s)',
$mandatory_product_obj->get_name(),
$quantity,
$type === 'OnePerCart' ? __('One per cart', 'studiou-wc-mandatory-products') : __('One per product', 'studiou-wc-mandatory-products')
);
echo '
';
}
}
echo '
';
echo '' . __('These products will be automatically added to the customer\'s cart when this product is added.', 'studiou-wc-mandatory-products') . '
';
}
}