dongtiannai0654 2017-07-10 09:58
浏览 206
已采纳

获取WooCommerce属性值(url)以将其用作href链接

I have an attribute in WooCommerce: demo_url. Every product has it's own URL inserted there.

I have also added a button near ADD TO CART so the user can easily view the product on his head (glasses shop). Clicking this button will redirect the user to outside application. Every glasses will have a different link for clicking.

I have tried something like that:

echo '<a href="'.$demo_url.'" target="_blank">Text</a>';

But it is not working at all. I've tried it with different global variables (like $currentday) but to no available.

I desperately need this to work. Could You please help?

Thanks

  • 写回答

1条回答 默认 最新

  • doubingjiu3199 2017-07-10 11:53
    关注

    Instead an using a product attribute you should use a custom field (a custom field in the product pages general settings tabs).

    Here is that code:

    // Create and display the custom field in product general setting tab
    add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_general_product_fields' );
    function add_custom_field_general_product_fields(){
        global $post;
    
        $value = get_post_meta( $post->ID, '_demo_url', true );
    
        if(empty($value))
            $value = '';
    
        echo '<div class="options_group">';
    
        woocommerce_wp_text_input( array(
            'id'          => 'demo_url',
            'label'       => __( 'Demo Url', 'woocommerce' ),
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the Demo  Url.', 'woocommerce' ),
            'value'       => $value
        ) );
    
        echo '</div>';
    }
    
    // Save the custom field
    add_action( 'woocommerce_process_product_meta', 'save_custom_field_general_product_fields' );
    function save_custom_field_general_product_fields( $post_id){
    
        // Text Field
        $demo_url = $_POST['demo_url'];
        if( !empty( $demo_url ) )
            update_post_meta( $post_id, '_demo_url', esc_attr( $demo_url ) );
    }
    

    Code goes in any php file of your active child theme (or theme) or also in any plugin php file.

    This code is tested and works... Then you will get this:

    enter image description here

    Now you can add in your code:

    // If you dont have the product id use this:
    $global $product;
    $product_id = $product->get_id();
    
    // Getting your custom product demo URL
    $demo_url = get_post_meta( $product_id, '_demo_url', true );
    
    // Add it to your button:
    echo '<a href="'.$demo_url.'" target="_blank">Text</a>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)