skip to Main Content

I have created a new FUNCTION for my product page.

It says WHEN _shipping_costs is empty (no digit) than change the button on "price on request".

Archive page

On the archive page you can clearly see it works perfectly.

But I stuggle on a single product page to change the button.

Single product page

This is build up the next way.

New button function:

function custom_add_price_on_request_button() {
    global $product;
    $shipping_cost = get_post_meta($product->get_id(), '_shipping_cost', true);
    if ($shipping_cost === '') {
        echo '<a class="button price-on-request-button" href="mailto:[email protected]"><span>Prijs op</span><span>aanvraag</span></a>';
    }
}
add_action('woocommerce_after_shop_loop_item', 'custom_add_price_on_request_button', 11);

Remove button + add new button function:

function custom_remove_and_add_cart_button_single() {
    global $product;
    $shipping_cost = get_post_meta($product->get_id(), '_shipping_cost', true);

    if ($shipping_cost === '') {
        // Remove the default add to cart button
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
        remove_action('woocommerce_single_product_summary', 'woocommerce_single_variation_add_to_cart_button', 30);
        
        // Add a custom 'Prijs op aanvraag' button
        add_action('woocommerce_single_product_summary', 'custom_add_price_on_request_button', 35);
    }
}
add_action('woocommerce_before_single_product_summary', 'custom_remove_and_add_cart_button_single', 1);

As you can see it says IF shippingcosts === ” (empty) than remove "Add to cart" button and change it to the "new function button".

As you aswell can see on the archive page it works fine but not on the single product page. Anyone see what is wrong with the Hook of Woocommerce I use?

I have found a few websites with the Wooks information but I don’t see to mange to fix it: https://wcsuccessacademy.com/woocommerce-visual-hook-guide-single-product-page/

2

Answers


  1. Chosen as BEST ANSWER

    I have tried aswell with adding debugs but ofcourse they don't show because it's not an active call to the log directly by opening the page.

    function custom_remove_cart_form_single() {
        global $product;
        $shipping_cost = get_post_meta($product->get_id(), '_shipping_cost', true);
    
        if (is_admin()) {
            return; // Prevent logging in the admin area
        }
    
        echo '<div class="custom-debug">';
        echo 'Product ID: ' . $product->get_id() . '<br>';
        echo 'Shipping Cost Meta: ' . print_r($shipping_cost, true) . '<br>';
    
        if ($shipping_cost === '') {
            echo 'Removing Add to Cart button for product ID: ' . $product->get_id() . '<br>';
    
            // Remove the default add to cart button
            remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
            remove_action('woocommerce_single_product_summary', 'woocommerce_single_variation_add_to_cart_button', 30);
    
            // Add custom 'Prijs op aanvraag' button
            add_action('woocommerce_single_product_summary', 'custom_add_price_on_request_button', 35);
        }
    
        echo '</div>';
    }
    add_action('woocommerce_before_single_product_summary', 'custom_remove_cart_form_single', 1);
    

  2. Try the following instead (using different hooks):

    add_action('woocommerce_single_product_summary', 'custom_remove_and_add_cart_button_single', 1);
    function custom_remove_and_add_cart_button_single() {
        global $product;
    
        // Targeting products with no shipping cost custom metadata
        if ( ! $product->get_meta('_shipping_cost') ) {
            // Remove the default add to cart button
            remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            
            // Add a custom "price on request" replacement button
            add_action('woocommerce_simple_add_to_cart', 'single_product_add_price_on_request_button', 30);
            add_action('woocommerce_single_variation', 'single_product_add_price_on_request_button', 20);
        }
    }
    
    function single_product_add_price_on_request_button() {
        echo '<a class="button price-on-request-button" href="mailto:[email protected]">
            <span>Prijs op</span><span>aanvraag</span>
        </a>';
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Works on Storefront theme.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search