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 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败