Announcement

Collapse
No announcement yet.

Attribute Machine: Is there a way to use MivaEvents.SubscribeToEvent()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Attribute Machine: Is there a way to use MivaEvents.SubscribeToEvent()

    This is more Javascript than MivaScript so I'll try to be clear

    My client wants to be able to display variant level inventory messages on the product page as swatches are clicked. The current behavior is that the button is disabled and "Add to Cart" changes to the generic "Sold Out" which is hard coded into the javascript.

    Instead In the admin, the client wants to change the inventory variant "Out of Stock" to "Available Fall 2016" and display that instead.

    When the page loads, a Json object is created, but the variant inventory message is not output in the page. (although I'm sure it was loaded by the component)

    When attribute selection change AttributeMachine loads that data, as explained below, but does not use the variant information so it is discarded and not saved with the AttributeMachine object.

    When a swatch is selected Attribute_Machine triggers AttributeMachine.prototype.Swatch_Click(). This eventually triggers an Ajax call to
    /mm5/json.mvc?Store_Code=VS&Function=Runtime_AttributeL ist_Load_ProductVariant_Possible. It eventually disables the button if the item is out of stock.

    Later another function in script.js sees the button is disabled and changes the message.

    My partial solution was to modify AttributeMachine.prototype.Swatch_Click() in the page template to do another duplicate Ajax call adding the variant data to the AttributeMachine object so I could use it later. That partially works but has some problems I won't go into here.

    1. Should I use MivaEvents.SubscribeToEvent() instead? (provide example)
    2. Is there a way to hook into the AttributeMachine to retrieve the entire contents of the Ajax response?

    Code:
    function Load_ProductVariantInventory (selected_option_id, input, attribute, option) {
            var selection = attrMachCall.Build_Selection();
            var request = Runtime_AttributeList_Load_ProductVariant_Possible(
                attrMachCall.settings.product_code, attrMachCall.settings.dependency_resolution,
                attribute.id, attribute.template_attribute ? attribute.template_attribute.id : 0,
                selected_option_id,
                selection.selected_attr_ids, selection.selected_attmpat_ids,
                selection.selected_option_ids, selection.selected_attr_types,
                selection.unselected_attr_ids, selection.unselected_attmpat_ids,
                function( response ) {
                    callback_Save_Variant(response);
                } );
    }
    
    function callback_Save_Variant (response) {
        if ((response.success != 1) && (response.data.variant.inv_active != 1)) {
            return;
        }
        attrMachCall.currentVariantInventory = response.data.variant;
        console.log(attrMachCall.currentVariantInventory);
    }


    Ray Yates
    "If I have seen further, it is by standing on the shoulders of giants."
    --- Sir Isaac Newton

    #2
    If I remember correctly the variant inventory messages are never loaded by Attribute Machine. It always returns the inventory message from the master product not the variant.

    So you can use MivaEvents.SubscribeToEvent() as your hook, but then you will need to send the variant id selected to a custom page you create which looks up the correct part product based on the variant selected and returns its inventory message.

    Code:
    MivaEvents.SubscribeToEvent(‘variant_changed’, function (product_data) {
    // make an ajax call to a custom page passing in product_data to lookup the variant selected
    });
    We've also done similar functionality by loading all inventory variant data into our own JSON object on page load then used the function above to lookup the correct inventory message based on the variant id selected.

    For your second question, the attribute machine object uses the product id. Here is how we access it:

    Code:
    <script>
                if (typeof am&mvt:product:id; !== 'undefined') {
                    var attrMachCall = am&mvt:product:id;;
                };
     </script>
    ​
    Then you can do whatever you want to your attrMachCall JS Object.

    Brennan Heyde
    VP Product
    Miva, Inc.
    [email protected]
    https://www.miva.com

    Comment

    Working...
    X