Home > Enterprise >  Woocommerce add_filter vs. add_action when asking about specific product meta
Woocommerce add_filter vs. add_action when asking about specific product meta

Time:01-25

I'm struggling with a basic php function. I implemented a backend checkbox in wordpress / woocommerce general tab which works fine:

// Add checkbox in Backend
function action_woocommerce_product_options_general_product_data() {
    // Checkbox
    woocommerce_wp_checkbox( array( 
        'id'             => '_prevent_add_to_cart_button', // Required, it's the meta_key for storing the value (is checked or not)
        'label'          => __( 'Warenkorb Button', 'woocommerce' ), // Text in the editor label
        'desc_tip'       => false, // true or false, show description directly or as tooltip
        'description'    => __( 'Warenkorb Button ausblenden', 'woocommerce' ) // Provide something useful here
    ) );
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
        
// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
    // Isset, yes or no
    $checkbox = isset( $_POST['_prevent_add_to_cart_button'] ) ? 'yes' : 'no';

    // Update meta
    $product->update_meta_data( '_prevent_add_to_cart_button', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

Now i want to ask for the value of that checkbox and do things based on it.

It works in this function as expected:

// Add function for category / listing page add to cart button hook
function hide_listingpage_button( $add_to_cart_html, $product, $args ){

    $hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );

    if ( $hide_add_to_cart_button == 'yes' ) {
        $before = '<span style="display:none;">';
        $after = '</span>';
        return $before . $add_to_cart_html . $after;
    }
    else {
        return $add_to_cart_html;
    }
    
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'hide_listingpage_button', 10, 3 );

but it does not work in this function(s):

// Add functions for singlepage before / after add to cart button hook
function beforehide_singlepage_button( $product ) {
    $hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
    if ( $hide_add_to_cart_button == 'yes' ) {
        echo 'test after';
    }   
}
function afterhide_singlepage_button( $product ) {
    $hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
    if ( $hide_add_to_cart_button == 'yes' ) {
        echo 'test after';
    }   
}
add_action( 'woocommerce_before_add_to_cart_button', 'beforehide_singlepage_button' );
add_action( 'woocommerce_after_add_to_cart_button', 'afterhide_singlepage_button' );

Im my opinion im doing basically the same things? I give the $product object as argument in the function, ask then for yes / no boolean with $hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' ); and then echo something with the if clause.

however, i get no output of the page from that line, and also no error message.

What am i missing?

thank you!

CodePudding user response:

You can either use the global $product; variable inside the function, or you can pass the product object as an argument to the function and then use it inside the function.

function beforehide_singlepage_button() {
    global $product;
    $hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
    if ( $hide_add_to_cart_button == 'yes' ) {
        echo 'test after';
    }   
}
  • Related