Skip to content

Instantly share code, notes, and snippets.

@pacotole
Last active September 15, 2023 17:45
Show Gist options
  • Save pacotole/f6075be765a4d9f76960174616f7cfd7 to your computer and use it in GitHub Desktop.
Save pacotole/f6075be765a4d9f76960174616f7cfd7 to your computer and use it in GitHub Desktop.
Propagate product category custom Joinchat settings to products with that category and propagate to Cart and Checkout Pages if contains that product.
<?php
/**
* Joinchat use Product category settings on product page
*
* Use custom Joinchat settings from the first category of the product
* with custom Joinchat settings.
*
* @param array $settings
* @return array
*/
function joinchat_use_category_settings_on_product( $settings ) {
if ( is_product() ) {
$terms = get_the_terms( get_the_ID(), 'product_cat' );
foreach ( $terms as $term ) {
$term_settings = get_term_meta( $term->term_id, '_joinchat', true );
if ( ! empty( $term_settings ) ) {
break;
}
}
}
return ( ! empty( $term_settings ) ) ? array_merge( $settings, $term_settings ) : $settings;
}
add_filter( 'joinchat_get_settings_site', 'joinchat_use_category_settings_on_product', 12 );
/**
* Joinchat use Product category settings on Cart & Checkout pages
*
* Use custom Joinchat settings from the first category of the first product
* in cart/order with custom Joinchat settings.
*
* @param array $settings
* @return array
*/
function joinchat_use_category_settings_on_cart_or_checkout( $settings ) {
if ( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
// Get terms from products on cart.
foreach ( WC()->cart->get_cart() as $cart_item ) {
$terms = get_the_terms( $cart_item['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
$term_settings = get_term_meta( $term->term_id, '_joinchat', true );
if ( ! empty( $term_settings ) ) {
break 2;
}
}
}
} elseif ( is_wc_endpoint_url( 'order-received' ) && isset( $_GET['key'] ) ) {
// Get terms from products on order.
$order = false;
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( get_query_var( 'order-received' ) ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( wp_unslash( $_GET['key'] ) ) ); // WPCS: input var ok, CSRF ok.
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( ! $order || ! hash_equals( $order->get_order_key(), $order_key ) ) {
$order = false;
}
}
if ( ! empty( $order ) ) {
foreach ( $order->get_items() as $item ) {
if ( $item->is_type( 'line_item' ) ) {
$product = $item->get_product();
$terms = get_the_terms( $product->get_id(), 'product_cat' );
foreach ( $terms as $term ) {
$term_settings = get_term_meta( $term->term_id, '_joinchat', true );
if ( ! empty( $term_settings ) ) {
break 2;
}
}
}
}
}
}
return ( ! empty( $term_settings ) ) ? array_merge( $settings, $term_settings ) : $settings;
}
add_filter( 'joinchat_get_settings_site', 'joinchat_use_category_settings_on_cart_or_checkout', 12 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment