• Resolved marseem

    (@waseemsannib)


    Hello everyone,

    I’m wondering whether I can add a custom field under tag to add a related product title.

    As you can see in the story I’m talking about Carla, I need the user reach carla’s page directly if they clicked on her title. you can see the image to have a clear example.

    how can I add this option under tag?

    Thank you

    https://ibb.co/HFryV7y

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Moderator t-p

    (@t-p)

    Looking at your site, you use woocommerce.

    I recommend getting in touch with WooCommerce’s support here about this if you have any of their paid WooCommerce products, or?here?if you do not.

    Thread Starter marseem

    (@waseemsannib)

    I need php code could fix this issue

    Moderator t-p

    (@t-p)

    Since you use woocommerce, I recommend getting in touch with WooCommerce’s support here about this if you have any of their paid WooCommerce products, or?here?if you do not.

    Moderator bcworkz

    (@bcworkz)

    You can add custom fields to the Edit Tag screen using the “{$taxonomy}_edit_form_fields” action hook. In place of variable {$taxonomy} you’d use post_tag. There’s a different, though similar hook for the Add New Tag form on the Tags list table screen: “{$taxonomy}_add_form_fields”

    Using these hooks only adds additional fields. They do not save any entered data nor display the data on a front end page. To save entered data, use the “saved_{$taxonomy}” action hook. To display the saved data, since the page you want this on is a WooCommerce page, you could examine the source code of the related WooCommerce template to learn which action hook to use, or do as t-p had suggested and contact WooCommerce support.

    Thread Starter marseem

    (@waseemsannib)

    Thank you bcworkz for the suggestion. I just need to know where I add this code? In theme function or this CSS code?

    Moderator bcworkz

    (@bcworkz)

    There has been no mention of CSS code, but FWIW where to put CSS code depends on what you’re trying to style: admin, editor, front end — and whose CSS you’re trying to modify: theme, plugin, core.

    re: Adding PHP code — theme functions.php is one option, but unless it’s a theme you’ve developed, you may be better off creating a custom plugin to contain your PHP. It’s not as difficult as it might sound. The one thing where a plugin might not be a good option is if you wish to modify theme templates. For that, a child theme would be the best option. And if you have a child theme anyway, any other custom PHP code could be in child functions.php instead of a plugin.

    I hope that makes sense, if not feel free to ask for clarification. There’s a lot of nuance in what’s “best”, depending upon your specific circumstance. In the end, you can do any of several options to include your PHP code as long as you follow one basic rule: Avoid modifying .php files created by others, always create your own.

    Thread Starter marseem

    (@waseemsannib)

    Thank you bcworkz,

    I understood that there are no CSS code. It is still not clear to me. I wonder whether you understood my issue. Could you recommend to me exactly what I should do to insert a new field line under the tag that includes the target product title to be clickable to redirect the user to the relevant product?
    For instance, Lucas and Clara, each of them mentions the other. So the user needs to click on Clara’s title on Lucas’ page to go to Clara’s page and vice versa. So how do you suggest creating this feature? because the tag should be one word linked to both which is not what I need exactly.
    I have a child theme. I can add a function filter in the theme file if you can provide me with one.

    I don’t know if you got my point.
    Waiting for your recommendation

    Thread Starter marseem

    (@waseemsannib)

    Hello bcworkz,

    I have found a code that could show the text in the meta box. I have changed the phrase to relevant doll and I added the title of the related doll, however how can I make this title clickable to redirect to the related doll page? I don’t know if I’m trying to resolve this issue with the wrong method.

    The problem with this code is it shows the text but not like tag label that could be clickable to reach the related products. It is just text above the SKU. How to force this product title text to be clickable as a tag?

    Thank you

    add_action('woocommerce_product_options_inventory_product_data', function() {

    woocommerce_wp_text_input([

    'id' => '_Relevant_Doll',

    'label' => __('Relevant Doll', 'txtdomain'),

    'wrapper_class' => 'show_if_simple'

    ]);

    });

    add_action('woocommerce_process_product_meta', function($post_id) {

    $product = wc_get_product($post_id);

    $num_package = isset($_POST['_Relevant_Doll']) ? $_POST['_Relevant_Doll'] : '';

    $product->update_meta_data('_Relevant_Doll', sanitize_text_field($num_package));

    $product->save();

    });

    add_action('woocommerce_product_meta_start', function() {

    global $post;

    $product = wc_get_product($post->ID);

    $num_package = $product->get_meta('_Relevant_Doll');

    if (!empty($num_package)) {

    printf('<div class="custom-sku">%s: %s</div>', __('Relevant Doll', 'txtdomain'), $num_package);

    }

    });
    Thread Starter marseem

    (@waseemsannib)

    My main goal is to show the related product title in inventory area (meta box) above SKU to be clickable and redirect to the related product page. What is the best solution for this do you think?

    Thank you again

    Thread Starter marseem

    (@waseemsannib)

    Please see the screenshot:

    https://ibb.co/jvv1jM1

    Moderator bcworkz

    (@bcworkz)

    I don’t know if text in Woo product data can be made to be linkable or not. I suppose you can try making the label into an anchor link and see what happens. For example:
    'label' => '<a href="https://google.com">'. __('Relevant Doll', 'txtdomain').'</a>',

    Obviously google.com is not the link you want, but it’ll prove if making a link is possible or not. If it’s possible we can progress from there to get to what you really want.

    If not possible this way, you’ll either need to find a different area of the page in which to place the link, or you could use JavaScript to modify the text into a link after the page loads.

    Thread Starter marseem

    (@waseemsannib)

    I replaced the code to be as the following:

    add_action('woocommerce_product_options_inventory_product_data', function() {

    woocommerce_wp_text_input([

    'id' => '_Relevant_Doll',

    'label' => __('Relevant Doll', 'txtdomain'),

    'wrapper_class' => 'show_if_simple'

    ]);

    });

    add_action('woocommerce_process_product_meta', function($post_id) {

    $product = wc_get_product($post_id);

    $num_package = isset($_POST['_Relevant_Doll']) ? $_POST['_Relevant_Doll'] : '';

    $product->update_meta_data('_Relevant_Doll', sanitize_text_field($num_package));

    $product->save();

    });

    add_action('woocommerce_product_meta_start', function() {

    global $post;

    $product = wc_get_product($post->ID);

    $num_package = $product->get_meta('_Relevant_Doll');

    if (!empty($num_package)) {

    printf('<div class="custom-sku">%s: %s</div>', __('Relevant Doll', 'txtdomain'), $num_package);?

    }

    'label' => '<a . __('Relevant Doll', 'txtdomain').'</a>',

    });

    It seems the link works only backend not in frontend.

    https://ibb.co/10Fb7QM
    https://ibb.co/BGtJrtR

    What do you suggest?

    • This reply was modified 5 months ago by marseem.
    Moderator bcworkz

    (@bcworkz)

    The label line near the end of your last code snippet doesn’t do anything. If that’s the right action hook, try this instead:
    echo '<a href="https://google.com">'. __('Link to Relevant Doll', 'txtdomain').'</a>';

    Thread Starter marseem

    (@waseemsannib)

    I tried as you suggested, however the snippet doesn’t work.

    echo '<a . __('Link to Relevant Doll', 'txtdomain').'</a>';

    add_action('woocommerce_product_options_inventory_product_data', function() {

    woocommerce_wp_text_input([

    'id' => '_Relevant_Doll',

    'label' => __('Relevant Doll', 'txtdomain'),

    'wrapper_class' => 'show_if_simple'

    ]);

    });

    add_action('woocommerce_process_product_meta', function($post_id) {

    $product = wc_get_product($post_id);

    $num_package = isset($_POST['_Relevant_Doll']) ? $_POST['_Relevant_Doll'] : '';

    $product->update_meta_data('_Relevant_Doll', sanitize_text_field($num_package));

    $product->save();

    });

    add_action('woocommerce_product_meta_start', function() {

    global $post;

    $product = wc_get_product($post->ID);

    $num_package = $product->get_meta('_Relevant_Doll');

    if (!empty($num_package)) {

    printf('<div class="custom-sku">%s: %s</div>', __('Relevant Doll', 'txtdomain'), $num_package);?

    }

    echo '<a . __('Link to Relevant Doll', 'txtdomain').'</a>';

    });
    Moderator bcworkz

    (@bcworkz)

    I’m sorry but that action hook apparently is the wrong one for what we want to accomplish. You could try asking for suggestions in WooCommerce’s dedicated support forum where those more knowledgeable about product page content might be able to help you. Or identify a different part of the page that might be more appropriate where output methods are better understood.

Viewing 15 replies - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.