• Resolved RichardMisencik

    (@richardmisencik)


    Hi, i’m having an issue where some php snippets are not working once i move them from functions.php to a snippet. Location is set to run everywhere (this is another problem that location not always works)

    Example snippet (a stripped down version):

    add_action( 'wc_before_payment_method_label' , 'add_payment_gateway_fee_label', 10, 1 );
    function add_payment_gateway_fee_label( $id ) {
    
    		if( $id == "paypal" ){
    			echo '<span class="custom-gateway-label paypal">' . $id . '</span>';
    		}
    		elseif( $id == "cod" ){
    			echo '<span class="custom-gateway-label cod">' . $id . '</span>';
    		}
    		elseif( $id == "bacs" ){
    			echo '<span class="custom-gateway-label bacs">' . $id . '</span>';
    		}
    		else {
    			echo '<span class="custom-gateway-label free">' . "free" . '</span>';
    		}
    }

    Am I missing something? How can I run these snippets?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @richardmisencik,

    From what I can see, that snippet should have no issue executing. Did you make sure to remove the “add_payment_gateway_fee_label” function from functions.php first to avoid an error where the function is defined in 2 places?

    Thread Starter RichardMisencik

    (@richardmisencik)

    Hello,
    @gripgrip thank you for a quick reply. Yes i remove the snippet from functions first and then save&enable in snippets, i’m even testing this on a bare minimum setup (flatsome theme with only woocommerce and code snippets enabled).

    The previous snippet works now (template issue) but here’s another.
    Can you please clarify how the snippet insert location works? For example this cart snippet works if “run everywhere” is chosen but not if “frontend only” is chosen.

    function action_after_shipping_rate ( $method, $index ) {
        if( 'flat_rate:2' === $method->id ) {
    		echo '<p class="shipping-method-description">' . __( 'test', 'domain' ) . '</p>';
        }
        if( 'flat_rate:1' === $method->id ) {
    		echo '<p class="shipping-method-description">' . __( 'test', 'domain' ) . '</p>';
        }
    }
    add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate', 20, 2 );
    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @richardmisencik,

    The frontend only check prevents code from executing if “is_admin“.
    In this case, it might be that the action runs on an AJAX call that is executed in an admin environment due to the way WP ajax calls are structured.

    Thread Starter RichardMisencik

    (@richardmisencik)

    @gripgrip I see, thanks for the clarification. So the safe bet is to run all snippets everywhere unless I know for sure the specific action/filter doesn’t use wp ajax

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Snippet not executing’ is closed to new replies.