I want to display a call now button that use attribute phone_num

hi, the call doesn’t work, I added the attribute in the sellers but it can’t retrieve the slug, what should I do?

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') { // Assicurati che lo slug corrisponda
                    $phone_number = $field->get_value();
                    break;
                }
            }
            
            if (empty($phone_number)) {
                // Handle error: phone number attribute is empty or doesn't exist
                // Here you can 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
);

Sorry for the delay. If you have the listing object in the callback function, it may be easier to get the attribute value this way:

$listing->get_phone_num()

If you’re sure that the attribute slug is “phone_num” it should be ok. Then you don’t have to loop through all the fields and check their slugs.

Hope this helps

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