Add a select YES / NO to a form and display the result in product WooCommerce

Add a select YES / NO to a form and display the result in product WooCommerce.

I use WordPress and WooCommerce

Read More

I have an online form that allows you to add products.

In this form, I want to add the choice YES or NO to indicate whether the product is exchangeable.

  • If “NO” is displayed on the product page:
    “Exchangeable product: NO
      This product is not exchangeable “

  • If “YES” is displayed on the product page:
      “Exchangeable product: YES
       This product is exchangeable, click here to make an offer “(click here is a clickable link)

Here is my code which does not work well:

In my product-edit.php template:

<?php 
WCVendors_Pro_Form_Helper::select( 
array( 
    'post_id'           => $object_id, 
    'id'                => 'wcv_custom_product_echangeable', 
    'label'             => __( 'Produit échangeable', 'wcvendors-pro' ), 
    'wrapper_start'     => '<div class="all-100">',
    'wrapper_end'       => '</div>', 
    'options'           => array(
        'non'       => __( 'non', 'wcvendors-pro' ),
        'oui'       => _x( 'oui', 'wcvendors-pro' ),
        ) 
    ) 
); 
?>

In my theme functions.php file:

add_action('woocommerce_product_meta_start', 'wcv_echangeable', 2);
function wcv_echangeable() {
$output = get_post_meta( get_the_ID(),     'wcv_custom_product_echangeable', true );
echo 'Produit échangeable: ' . $output . '<br>';

$pos = strpos( $output, 'oui' );
if ( $pos === false ) {
    echo 'Ce produit est échangeable,<a href="#">Cliquez ici </a> pour faire une offre <br><br>';
} else {
    echo 'Ce produit n'est pas échangeable.<br><br>';
 }
}

Can anyone help me?
Thank you in advance

Related posts