Files
old-new-wiaas/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php

150 lines
4.4 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
if (false === $template_term_exists = get_term_by('slug', 'wiaastemplate', 'product_type')) {
wp_insert_term('wiaastemplate', 'product_type');
}
function construct_template_products_class() {
class WC_Product_Template extends WC_Product {
public function __construct($product) {
parent::__construct($product);
}
/**
* Get internal type.
*
* @return string
*/
public function get_type() {
return 'wiaastemplate';
}
/**
* Get template categories from template product meta
*
* @param $template_id integer
* @param $template_category_slug string the name of the main wiaas category
* @return mixed
*/
public static function get_template_categories_from_meta($template_id, $template_category_slug) {
return get_post_meta($template_id, $template_category_slug, true);
}
public static function extract_template_product_categories($category_meta) {
$category_objects = array();
if (empty($category_meta)) {
return array();
}
foreach ($category_meta as $meta) {
if (!empty($meta)) {
$category_template_id = $meta['template_category_id'];
$template_category_object = new Wiaas_Template_Category_Object(
$category_template_id,
$meta['template_category_title'],
$meta['quantity']);
$category_objects[$category_template_id] = $template_category_object;
}
}
return empty($category_objects) ? null : $category_objects;
}
/**
* Get template categories from bundled products items
*
* @param $bundled_items array
* @return array
*/
public static function extract_bundled_product_categories($bundled_items) {
$template_category_objects = array();
foreach ($bundled_items as $item_id => $item) {
$item_data = $item->get_data();
$post_terms = wp_get_object_terms($item->get_product_id(), 'template_category', array('fields' => 'id=>name'));
if (!empty($post_terms) && !is_wp_error($post_terms)) {
$cat = '';
$category_template_id = '';
foreach ($post_terms as $id => $term) {
$cat = $term;
$category_template_id = $id;
}
$template_category_object = new Wiaas_Template_Category_Object(
$category_template_id,
$cat,
$item_data['quantity_max'],
$item->get_id(),
$item->get_title());
$template_category_objects[$category_template_id] = $template_category_object;
}
}
return $template_category_objects;
}
/**
* Save selected template to bundled product meta
*
* @param $selected_template integer
* @param $post_id integer
*/
public static function bind_selected_template_to_product($selected_template, $post_id) {
if (!empty($selected_template))
update_post_meta($post_id, '_select_template', esc_attr($selected_template));
else {
update_post_meta($post_id, '_select_template', '');
}
}
/**
* Save template categories to template product meta
*
* @param $post_id integer
* @param $option string a main wiaas category
* @param $processed_template_data array actual template category data
*/
public static function save_template_product_meta($post_id, $option, $processed_template_data) {
update_post_meta($post_id, '_template_items_' . $option, $processed_template_data);
}
}
}
add_action('init', 'construct_template_products_class');
function woocommerce_product_class($classname, $product_type) {
if ($product_type == 'wiaastemplate') {
$classname = 'WC_Product_Template';
}
return $classname;
}
add_filter('woocommerce_product_class', 'woocommerce_product_class', 10, 2);