custom field in custom module

  • Posts: 15
  • Thank you received: 2
1 month 1 day ago #366203

-- HikaShop version -- : 5.1.0
-- Joomla version -- : 5.2.2
-- PHP version -- : 8.1.28
-- Browser(s) name and version -- : chrome latest, firefox, etc.

Hi guys!

I think I have a simple question: how can I call custom fields from a custom module (made by me, not joomla custom html module)?

For example: I want to display a custom field with an external stock quantity (extstore field name) through my custom module, so that it appears on the product page within the show_default.php file. I want to place it after the 'Add to cart button'.

Inserting it as module id (with regular labs) to show_default.php causes the page to crash due to this->element.

Thanks for your help!

Please Log in or Create an account to join the conversation.

  • Posts: 83594
  • Thank you received: 13528
  • MODERATOR
1 month 23 hours ago #366207

Hi,

Why display the custom field via a module ?
Can't you create a custom field of the table "item" via the Display>Custom fields menu and use the display options to just have HikaShop display it before the add to cart button for you ?
Why is that not possible ?
Where does this "external stock quantity" comes from ? Why do you want to add it to the product page ? What is its purpose ?
If you can provide more explanation on what you're trying to achieve, we should be able to tell you what to do.

Please Log in or Create an account to join the conversation.

  • Posts: 15
  • Thank you received: 2
1 month 23 hours ago #366210

Ok, I'll try to explain :)

I would like to create an interface similar to the current shopping cart block, where the quantity of an external stock would be displayed.

If the visitor doesn't have the right quantity in the internal stock, he could use the external stock control to select the extra quantity that suits him and I would send this in an email to the email address set in the admin interface (e.g. using rsform).

So the basic add to cart button would do the usual hikashop procedure, while the second block would do the external warehouse enquiry.

Visitor selects a quantity, adds to list, goes to next product, adds to list. Then at the end he sends this information (e.g. by clicking on a button in a modal) and I can contact him based on this

We have a lot of types of products, few in internal stock, more in external stock.

Please Log in or Create an account to join the conversation.

  • Posts: 83594
  • Thank you received: 13528
  • MODERATOR
1 month 18 hours ago #366217

Hi,

Ok. Thanks for the details.
I think it's a good idea to add your interface to the product details page to handle that.

Adding it in product / show_default can probably be done. One thing you need to be wary off is that the whole product / show_default is inside form tags from the product / show view file. And in HTML, you can't have a form element inside another form element. That's invalid, and it will create erratic problems with the forms. So you might have to move the ending tag of the form from product / show to product / show_default as to encompass the default quantity area of HikaShop, without encompassing your own form.

Also, you can directly put your custom form inside product / show_default. You don't have to create module with your code inside and insert it in the view with another extension. That should simplify work for you.

Finally, if you have an error when you add the tag from modules anywhere in product / show_default, it probably means that you've added it at the wrong place in the code.

Please Log in or Create an account to join the conversation.

  • Posts: 15
  • Thank you received: 2
4 weeks 1 day ago #366255

Following your information, I am now working directly in show_default.php. Let me ask you how do I get the data for the variants? Do you have any description how to call the custom fields between product and variant? My current code looks like this:

<script>
document.addEventListener("DOMContentLoaded", function() {
    
    function getVariantData() {
        let productNameMain = document.querySelector("#hikashop_product_name_main");
        let variantSubName = document.querySelector(".hikashop_product_variant_subname");
        let fullProductName = productNameMain ? productNameMain.textContent.trim() : "N/A";
        let variantName = variantSubName ? variantSubName.textContent.trim() : "";

        let productIdElement = document.querySelector("#hikashop_product_id_main input[name='product_id']");
        let productId = productIdElement ? productIdElement.value : "N/A";

        let extstoreElement = document.querySelector(".hikashop_product_custom_value");
        let extstoreValue = extstoreElement ? extstoreElement.textContent.trim() : "N/A";

       
        document.getElementById('variant-name-display').textContent = fullProductName + ' ' + variantName;
        document.getElementById('extstore-value-display').textContent = extstoreValue;

        
    }

    
    getVariantData();

    
    document.addEventListener("click", function(event) {
       
        if (event.target.closest(".hikashop_product_characteristic")) { 
            setTimeout(getVariantData, 1000); // Frissítés az AJAX miatt
        }
    });

   
    document.addEventListener("change", function(event) {
        if (event.target.matches("select[name='product_id']")) {
            getVariantData();
        }
    });
});

</script>

<div id="variant-data-display">
    <p id="product-name-display">Termék Név: <span id="variant-name-display"></span></p>
    <p id="extstore-display">Extstore érték: <span id="extstore-value-display"></span></p>
</div>

Last edit: 4 weeks 1 day ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 83594
  • Thank you received: 13528
  • MODERATOR
4 weeks 22 hours ago #366257

Hi,

If you want to have your javascript code triggered when the currently selected variant is changed, you can use the event system of HikaShop and register for hkContentChanged:
www.hikashop.com/support/documentation/6...tml#hkContentChanged
The full product name, with the main product name + the variant name will be in the div with the id hikashop_product_name_main
So you can directly take it from there without any processing.
Something like this:

let productNameMain = document.querySelector("#hikashop_product_name_main").innerHTML;

For example:
if(window.Oby) {
	window.Oby.registerAjax(["cart.updated","wishlist.updated"],function(params){
		let productNameMain = document.querySelector("#hikashop_product_name_main").innerHTML;
		document.getElementById('variant-name-display').innerHTML = productNameMain;
	});
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.062 seconds
Powered by Kunena Forum