Skip to content

Commit a021ec8

Browse files
fix: show image-based fields in cart block (#530)
Updated the cart meta item values to titles to render the image-based field on the cart block.
1 parent d4c09b5 commit a021ec8

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

inc/functions.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,16 @@ function ppom_generate_html_for_files( $file_names, $input_type, $item ) {
16501650

16511651
// return html for images selected
16521652
function ppom_generate_html_for_images( $images ) {
1653+
global $post;
16531654

1655+
static $ppom_has_cart_block = null;
1656+
if ( is_null( $ppom_has_cart_block ) ) {
1657+
$ppom_has_cart_block = has_block( 'woocommerce/cart', $post );
1658+
}
1659+
1660+
if ( $ppom_has_cart_block ) {
1661+
return ppom_get_image_name( $images );
1662+
}
16541663

16551664
$ppom_html = '<table class="table table-bordered">';
16561665
foreach ( $images as $id => $images_meta ) {
@@ -2359,7 +2368,7 @@ function ppom_get_conditional_data_attributes( $meta ) {
23592368

23602369
$bound = isset( $conditions['bound'] ) ? ppom_wpml_translate( $conditions['bound'], 'PPOM' ) : '';
23612370
$visibility = isset( $conditions['visibility'] ) ? ppom_wpml_translate( $conditions['visibility'], 'PPOM' ) : '';
2362-
2371+
23632372
$conditions['rules'] = array_filter(
23642373
$conditions['rules'],
23652374
function( $rule ) {
@@ -2559,3 +2568,29 @@ function ppom_posted_field_max_min_value_validation( $posted_fields, $field ) {
25592568

25602569
return '';
25612570
}
2571+
2572+
/**
2573+
* Get image name from images array.
2574+
* @param mixed $images
2575+
* @return string
2576+
*/
2577+
function ppom_get_image_name( $images ) {
2578+
$updated_value = '';
2579+
2580+
if ( is_array( $images ) ) {
2581+
$total_images = count( $images );
2582+
2583+
foreach ( $images as $image_key => $image ) {
2584+
$image_data = json_decode( stripslashes( $image ), true );
2585+
if ( isset( $image_data['raw'] ) ) {
2586+
$updated_value .= $image_data['raw'];
2587+
}
2588+
2589+
if ( $image_key < $total_images - 1 ) {
2590+
$updated_value .= ', ';
2591+
}
2592+
}
2593+
}
2594+
2595+
return $updated_value;
2596+
}

inc/woocommerce.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,10 @@ function ppom_woocommerce_add_item_meta( $item_meta, $cart_item ) {
665665

666666
if ( ! empty( $meta_name ) ) {
667667

668+
if ( ! is_scalar( $meta_value ) ) {
669+
$meta_value = wp_json_encode( $meta_value );
670+
}
671+
668672
if ( apply_filters( 'ppom_show_option_price_cart', false ) && isset( $meta['price'] ) ) {
669673
$meta_value .= ' (' . wc_price( $meta['price'] ) . ')';
670674
}

0 commit comments

Comments
 (0)