I want to display a call now button that use the attribute phone-num

idk why my code is not working when i click call now theres no phone number showin up :

add_filter(
    'hivepress/v1/templates/listing_view_page/blocks',
    function( $blocks, $template ) {
        $listing = $template->get_context( 'listing' );

        if ( $listing ) {
            // Get the phone number from the listing attribute
            $phone_number = trim( $listing->get_attribute( 'phone-num' ) );
            // Remove all non-numeric characters from the phone number
            $phone_number = preg_replace( '/[^0-9]/', '', $phone_number );
            // Generate the phone number link
            $phone_number_link = 'tel:' . $phone_number;

            $blocks = hivepress()->helper->merge_trees(
                [ 'blocks' => $blocks ],
                [
                    'blocks' => [
                        'listing_actions_primary' => [
                            'blocks' => [
                                'vendor_phone_link' => [
                                    'type'    => 'content',
                                    'content' => '<a href="' . esc_attr( $phone_number_link ) . '">Call now</a>',
                                    '_order'  => 5,
                                ],
                            ],
                        ],
                    ],
                ]
            )['blocks'];
        }

        return $blocks;
    },
    1000,
    2
);
1 Like

i fixed the code here u are guys i figured it out u need to add an attribute for the vendor called phone-num and display it primary on front page and right after add this code to function.php :

add_filter(
    'hivepress/v1/templates/listing_view_page/blocks',
    function( $blocks, $template ) {
        // Get the current listing
        $listing = $template->get_context( 'listing' );

        // Check if a listing exists
        if ( $listing ) {
            // Get the phone number from the listing attribute
			$phone_number = '';
			foreach ( $listing->_get_fields( 'view_page_primary' ) as $field ) {
				if ( $field->get_slug() === 'phone-num' ) {
					$phone_number = $field->get_value();
					break;
				}
			}
			
			if ( empty( $phone_number ) ) {
				// Handle error: phone number attribute is empty or doesn't exist
				// For example: return an error message or use a default phone number
				$phone_number = '555-5555';
			}
			
			// Remove all non-numeric characters from the phone number
			$phone_number = preg_replace( '/[^0-9]/', '', $phone_number );
			
			// Generate the phone number link
			$phone_number_link = 'tel:' . $phone_number;

            // Add the new block to the listing_actions_primary block
            $blocks = hivepress()->helper->merge_trees(
                [ 'blocks' => $blocks ],
                [
                    'blocks' => [
                        'listing_actions_primary' => [
                            'blocks' => [
                                'vendor_phone_link' => [
                                    'type'    => 'content',
                                    'content' => '<a href="' . esc_attr( $phone_number_link ) . '"><button type="button" class="hp-listing__action hp-listing__action--call button button--primary button--large" style="background-color: green; color: white;">   Contact  Now   </button></a>',
                                    '_order'  => 5,
                                ],
                                'second_button' => [
                                    'type'    => 'content',
                                    'content' => '<button type="button" class="hp-listing__action hp-listing__action--other button button--primary button--large" style="background-color: white; color: black; pointer-events: none;"> Or </button>',
                                    '_order'  => 6,
                                ],
                            ],
                        ],
                    ],
                ]
            )['blocks'];
        }
        return $blocks;
    },
    1000,
    2
);
2 Likes

theres a hided button under so u can speerate the “conact now buttoon” and “send message” one

Thank you for posting possible solution. Also, it is possible to get the phone number value by changing $listing->get_attribute on $listing->get_ + your_attribute_field_name(). For example, if the phone number attribute has field name which is equal to phone_num then please try to get phone number attribute value in this way $listing->get_phone_num

Thanks for sharing . . . I don’t have much experience in coding & have Q 4 you, how about getting phone number from vendor attribute (code snippet) . . . I think Once z vendor complete the registration form it would be nice if displayed in every listing page as you do

If you have a $vendor object, you can get any attribute this way:

$vendor->get_attributenamehere()

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.