woocommerce offtopic – apply_filter and add_filter conflict

Question

I have a woocommerce website and I use “ProductX – Gutenberg Product Blocks for WooCommerce” to create a product slider.

I need to change the add to cart button to include step quantity for products (I added minimum quantity and step quantity) so I use this function in my functions.php of child theme:

function zp_filter_woocommerce_loop_add_to_cart_link( $quantity, $product, $args ) { 
    if (is_product_category() ) {
    $quantity = 1;
        $product_step = wc_get_product_step_limit( $product->id);   
        if ($product_step > 1){
        $quantity = $product_step;
        }
        $qty = isset( $args['quantity'] ) && $args['quantity'] == $quantity ? $args['quantity'] : $quantity;
        $html = sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( $qty ),
            esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
            isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
            esc_html( $product->add_to_cart_text() )
        );
    return $html;
    }
    };          
    add_filter( 'woocommerce_loop_add_to_cart_link', 'zp_filter_woocommerce_loop_add_to_cart_link', 99, 3 ); 

This function do it’s job very well throughout the site except on the pages where I use the plugin that stops working, for a conflict with this function:

public function get_add_to_cart($product , $cart_text=""){
    $attributes = array(
        'aria-label'       => $product->add_to_cart_description(),
        'data-quantity'    => '1',
        'data-product_id'  => $product->get_id(),
        'data-product_sku' => $product->get_sku(),
        'rel'              => 'nofollow',
        'class'            => 'add_to_cart_button ajax_add_to_cart',
    ); 
     return apply_filters(
        'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
        sprintf(
            '<a href="%s" data-stock="%s" %s>%s</a>',
            esc_url( $product->add_to_cart_url() ),
            esc_attr( $product->get_stock_quantity() ),
            wc_implode_html_attributes( $attributes ),
            $cart_text ? $cart_text : esc_html( $product->add_to_cart_text() )
        ),
        $product
    ); 
}

How can I change my function zp_filter_woocommerce_loop_add_to_cart_link in order to avoid conflict?
I tried to change priority in add_filter but doesn’t work.

Thank you.

0
Saxcbr 2 months 2021-05-30T02:45:05-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse