Skip to content
Advertisement

How to reference a custom product type value in HTML/PHP (WordPress & Woocommerce)

I have a custom button that instead of the ‘add to cart’ button. It should open a link to the brand: id=’_brands_link’ and in the button, and after “Bestel bij” there should be the brand name id=’_brands_name’ (see the created fields in the bone product):

// name
function add_brands_name_field() {
    global $product_object;
    ?>
    <div class='options_group show_if_brands'>
        <?php

        woocommerce_wp_text_input(
            array(
                'id'          => '_brands_name',
                'label'       => __( 'Brand name', 'vicodemedia' ),
                'value'       => $product_object->get_meta( '_brands_name', true ),
                'default'     => '',
                'placeholder' => 'Enter brand name',
                'data_type' => 'text',
            )
        );
        ?>
    </div>
     
<?php
}

// link
function add_brands_link_field() {
    global $product_object;
    ?>
    <div class='options_group show_if_brands'>
        <?php

        woocommerce_wp_text_input(
            array(
                'id'          => '_brands_link',
                'label'       => __( 'Product link', 'vicodemedia' ),
                'value'       => $product_object->get_meta( '_brands_link', true ),
                'default'     => '',
                'placeholder' => 'Enter product link',
                'data_type' => 'text',
            )
        );
        ?>
    </div>
     
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');

I have a function of this buttons now:

if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {

    function brands_add_to_cart() {
      //wc_get_template( 'single-product/add-to-cart/brand.php' );
      do_action( 'woocommerce_before_add_to_cart_form' );
      ?>
        <form class="cart" action="https://www.google.com">
            <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

            <button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
            Bestel bij Google <?php  ?>
            </button>

            <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
        </form>
        <?php
    }
  
    add_action('woocommerce_brands_add_to_cart',  'brands_add_to_cart');
  }

So in action instead of https://www.google.com there should be _brands_link. And instead of “Google”, which is in the button, there should be _brands_name

Here’s all the code for my custom product type, just in case:

// BEGIN CODE
class WC_Product_brands extends WC_Product_Simple {

    // Return the product type
    public function get_type() {
        return 'brands';
    }
}




// add the product type to the dropdown
function add_type_to_dropdown( $types ) {
    $types['brands'] = __( 'Brand product', 'vicodemedia' );
   
    return $types;
}
add_filter( 'product_type_selector', 'add_type_to_dropdown');






// add the product type as a taxonomy
function install_taxonomy() {
    // If there is no brands product type taxonomy, add it.
    if ( ! get_term_by( 'slug', 'brands', 'product_type' ) ) {
        wp_insert_term( 'brands', 'product_type' );
    }
}
register_activation_hook( __FILE__, 'install_taxonomy');




// name
function add_brands_name_field() {
    global $product_object;
    ?>
    <div class='options_group show_if_brands'>
        <?php

        woocommerce_wp_text_input(
            array(
                'id'          => '_brands_name',
                'label'       => __( 'Brand name', 'vicodemedia' ),
                'value'       => $product_object->get_meta( '_brands_name', true ),
                'default'     => '',
                'placeholder' => 'Enter brand name',
                'data_type' => 'text',
            )
        );
        ?>
    </div>
     
<?php
}

// link
function add_brands_link_field() {
    global $product_object;
    ?>
    <div class='options_group show_if_brands'>
        <?php

        woocommerce_wp_text_input(
            array(
                'id'          => '_brands_link',
                'label'       => __( 'Product link', 'vicodemedia' ),
                'value'       => $product_object->get_meta( '_brands_link', true ),
                'default'     => '',
                'placeholder' => 'Enter product link',
                'data_type' => 'text',
            )
        );
        ?>
    </div>
     
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_link_field');
add_action( 'woocommerce_product_options_general_product_data', 'add_brands_name_field');



// Generl Tab not showing up
add_action( 'woocommerce_product_options_general_product_data', function(){
    echo '<div class="options_group show_if_brands clear"></div>';
} );




// add show_if_advanced calass to options_group
function enable_product_js() {
    global $post, $product_object;

    if ( ! $post ) { return; }

    if ( 'product' != $post->post_type ) :
  return;
    endif;

    $is_brands = $product_object && 'brands' === $product_object->get_type() ? true : false;

    ?>
    <script type='text/javascript'>
    jQuery(document).ready(function () {
    //for link tab
    jQuery('#general_product_data .pricing').addClass('show_if_brands');

    <?php if ( $is_brands ) { ?>
        jQuery('#general_product_data .pricing').show();
    <?php } ?>
    });
    </script>
    <style>
        .woocommerce_options_panel .options_group {
            border-top: 0px;
            border-bottom: 0px;
        }
    </style>
    <?php
}
add_action( 'admin_footer', 'enable_product_js');

// save data on submission
function save_brands_link( $post_id ) {
    $link = isset( $_POST['_brands_link'] ) ? sanitize_text_field( $_POST['_brands_link'] ) : '';
    update_post_meta( $post_id, '_brands_link', $link );
    }
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_link');

// save data on submission
function save_brands_name( $post_id ) {
    $name = isset( $_POST['_brands_name'] ) ? sanitize_text_field( $_POST['_brands_name'] ) : '';
    update_post_meta( $post_id, '_brands_name', $name );
    }
add_action( 'woocommerce_process_product_meta_brands', 'save_brands_name');

/*
//button
function custom_product_button() {
    ?>
    <a href="<?php the_field('_brands_link'); ?>" class="button alt">Bestel bij
    <?php the_field('_brands_name'); ?></a> 
    <?php
}
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
*/

if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {

    function brands_add_to_cart() {
      //wc_get_template( 'single-product/add-to-cart/brand.php' );
      do_action( 'woocommerce_before_add_to_cart_form' );
      ?>
        <form class="cart" action="https://www.google.com">
            <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

            <button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
            Bestel bij Google <?php  ?>
            </button>

            <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
        </form>
        <?php
    }
  
    add_action('woocommerce_brands_add_to_cart',  'brands_add_to_cart');
  }

// END CODE

CONCLUSION: I need to understand how to correctly reference the objects of the classes I created for _brands_link and _brands_name. I do not understand how this should be done, so that it would be displayed in HTML as text (_brands_name) and as a link in the form action = “” (_brands_link):

function brands_add_to_cart() {
      do_action( 'woocommerce_before_add_to_cart_form' );
      ?>
        <form class="cart" action="_brands_link">
            <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

            <button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
            Bestel bij _brands_name
            </button>

            <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
        </form>
        <?php
    }

Advertisement

Answer

Since are save them as post meta data, you could get back as meta too

if (! function_exists( 'woocommerce_brands_add_to_cart' ) ) {

    function brands_add_to_cart() {
      //wc_get_template( 'single-product/add-to-cart/brand.php' );
      do_action( 'woocommerce_before_add_to_cart_form' );
      ?>
        <form class="cart" action="<?php echo get_post_meta(get_the_ID(), '_brands_link', true); ?>">
            <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

            <button style="padding: 18px 40px 18px 40px; height: 55px;" type="submit" name="add-to-cart" value="" class="single_add_to_cart_button button alt">
            Bestel bij <?php echo get_post_meta(get_the_ID(), '_brands_name', true); ?>
            </button>

            <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
        </form>
        <?php
    }
  
    add_action('woocommerce_brands_add_to_cart',  'brands_add_to_cart');
  }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement