Untitled
unknown
plain_text
2 years ago
5.5 kB
7
Indexable
// -> Product: Colors Cache Post Meta
function sk_product_cache_colors_meta( $product_id, $post ) {
$language = SK_LANG;
if( $language == 'it' && $post->post_type == 'product' ) {
global $wpdb;
$return = $prodotti_associati = array();
$product = wc_get_product($product_id);
$product_sku = $product->get_sku();
// COLORS
if( !empty($product_sku) && strpos($product_sku, '-') !== false ) {
$sku = explode('-', $product_sku);
$sql = "
SELECT p.ID, p.ID as post_id, tt.term_id as color_id, td_display.term_id as color_display_id, p.post_status
FROM wp_posts p
LEFT JOIN wp_term_relationships tr ON tr.object_id = p.ID
JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'pa_colore'
JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_term_relationships tr_display ON tr_display.object_id = p.ID
JOIN wp_term_taxonomy tt_display ON tt_display.term_taxonomy_id = tr_display.term_taxonomy_id AND tt_display.taxonomy = 'pa_colore-display'
JOIN wp_terms td_display ON td_display.term_id = tt_display.term_id
JOIN wp_postmeta pm ON p.ID = pm.post_id
JOIN wp_icl_translations t_orig ON t_orig.element_id = p.ID
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND pm.meta_key = '_sku' AND pm.meta_value LIKE %s
AND t_orig.language_code = 'it'
ORDER BY IF(p.ID = %d, 1, 0) DESC, tr.term_order";
$colors = $wpdb->get_results( $wpdb->prepare($sql, trim($sku[0]) . '-%', $product_id) );
if( !empty($colors) ) {
foreach( $colors as $color ) {
$associated_products[] = $color->post_id;
if( $color->post_status == 'publish' ) {
$support = array(
'post_id' => $color->post_id,
'color_id' => $color->color_id,
'color_display_id' => $color->color_display_id,
'image_id' => get_post_thumbnail_id($color->post_id),
);
$return[] = $support;
}
}
}
if( !empty($associated_products) ) {
foreach($associated_products as $associated_product) {
$tmp = array();
foreach( $return as $support ) {
if( $support['post_id'] == $associated_product ) {
$tmp[] = $support;
break;
}
}
foreach( $return as $support ) {
if( $support['post_id'] != $associated_product ) {
$tmp[] = $support;
}
}
// UPDATE COLORS
update_post_meta( $associated_product, '_related_colors', json_encode($tmp) );
}
}
}
}
}
add_action('before_delete_post', 'sk_product_cache_colors_meta', 5, 2);
add_action('save_post', 'sk_product_cache_colors_meta', 5, 2);
add_action('woocommerce_new_product', 'sk_product_cache_colors_meta', 5, 2);
add_action('woocommerce_update_product', 'sk_product_cache_colors_meta', 5, 2);
// -> Product: Brands Cache Post Meta
function sk_product_cache_brands_meta( $product_id, $post ) {
$language = SK_LANG;
if( $language == 'it' && $post->post_type == 'product' ) {
global $wpdb;
// BRANDS
$slugs = array( 'brands', 'brands-uomo', 'brands-bambini' );
if( has_term( $slugs, 'product_cat', $product_id ) ) {
$sql = "
SELECT t.term_id
FROM wp_term_relationships tr
JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms t ON t.term_id = tt.term_id
JOIN wp_term_taxonomy tt_parent ON tt_parent.term_taxonomy_id = tt.parent
JOIN wp_terms t_parent ON t_parent.term_id = tt_parent.term_id
WHERE tt.taxonomy = 'product_cat'
AND tt_parent.taxonomy = 'product_cat'
AND (t_parent.slug = 'brands' OR t_parent.slug = 'brands-uomo' OR t_parent.slug = 'brands-bambini')
AND tr.object_id = %d";
$brands = $wpdb->get_results( $wpdb->prepare($sql, $product_id) );
if( !empty($brands) ) {
$brand_tmp = array();
foreach( $brands as $brand_id ) {
$brand = get_term_by('id', $brand_id->term_id, 'product_cat');
$brand_support = array(
'term_id' => $brand->term_id,
'name' => $brand->name,
);
$brand_tmp[] = $brand_support;
break;
}
// UPDATE BRANDS
update_post_meta( $product_id, '_related_brands', json_encode($brand_tmp) );
}
}
}
}
add_action('before_delete_post', 'sk_product_cache_brands_meta', 5, 2);
add_action('save_post', 'sk_product_cache_brands_meta', 5, 2);
add_action('woocommerce_new_product', 'sk_product_cache_brands_meta', 5, 2);
add_action('woocommerce_update_product', 'sk_product_cache_brands_meta', 5, 2);Editor is loading...