News:

Support the VirtueMart project and become a member

Main Menu

Bug with custom fields in re-order module

Started by overseas, June 24, 2015, 11:50:45 AM

Previous topic - Next topic

overseas

Hello,

I have bought a module for re-ordering in virtuemart with a button "re-order" in user order view.

Everything works well excepted for virtumart customfields that not follow in my cart. When user clic on reorder, product appear in cart without its custom fields.

I have try to understand the code wich seems to be easy but I haven't found the solution:


defaut.php:

if(isset($products) && is_array($products) && count($products) > 0){
?>
<div class="vmReorderModule <?php echo $moduleclass_sfx; ?>" id="vmReorderModule">
  <form method="post" class="product js-recalculate" action="index.php" id="vmReorderModuleForm">
    <p><?php echo $pretext; ?></p>
    <div class="addtocart-bar">
      <span class="addtocart-button">
        <input type="submit" name="addtocart" class="addtocart-button" value="<?php echo $buttontext; ?>" title="<?php echo $buttontext; ?>">
      </span>
    </div>
   <?php
               // Product custom_fields
               if (!empty($product->custom_attributes)) {
                  ?>
                  <div class="product-fields">
                     <?php foreach ($product->custom_attributes as $field) { ?>

                     <div style="display:inline-block;" class="product-field product-field-type-<?php echo $field->field_type ?>">
                        <?php if($field->show_title == 1) { ?>
                           <span class="product-fields-title"><b><?php echo $field->custom_title ?></b></span>
                           <?php echo JHTML::tooltip ($field->custom_tip, $field->custom_title, 'tooltip.png'); ?>
                        <?php } ?>
                        <span class="product-field-display"><?php echo $field->display ?></span>
                        <span class="product-field-desc"><?php echo $field->custom_field_desc ?></span>
                     </div>

                     <?php } ?>
                  </div>
                  <?php } ?>
    <?php
    foreach($products as $key => $product){
      $addToCartUrl = JURI::root().'?option=com_virtuemart&controller=cart&task=addJS&virtuemart_product_id[]='.$product->virtuemart_product_id.'&quantity[]='.$product->product_quantity;
      // echo "<pre>";var_dump($product);echo "</pre>";
      if(is_array($product->attributes) && count($product->attributes) > 0)
      {
        foreach($product->attributes as $attribute)
        {
          $addToCartUrl .= '&customPrice['.$key.']['.$attribute->custom_id.']='.$attribute->id;;
          ?>
          <input type="hidden" name="customPrice[<?php echo $key; ?>][<?php echo $attribute->custom_id; ?>]" value="<?php echo $attribute->id; ?>">
          <?php
        }
      }
    ?>
    <input type="hidden" name="quantity[]" value="<?php echo $product->product_quantity; ?>">
    <input type="hidden" name="virtuemart_product_id[]" value="<?php echo $product->virtuemart_product_id; ?>">
    <input type="hidden" name="reorderUrl" class="reorderUrl" value="<?php echo $addToCartUrl; ?>">
    <?php } ?>
    <input type="hidden" name="option" value="com_virtuemart">
    <input type="hidden" name="view" value="cart">
    <input type="hidden" name="task" value="add">
    <input type="hidden" name="virtuemart_manufacturer_id" value="Array">
  </form>
</div>

<script type="text/javascript">

// if jQuery isn't loaded, load it.
if (typeof jQuery == 'undefined') {
 
  function getScript(url, success) {
 
    var script = document.createElement('script');
    script.src = url;
    var head = document.getElementsByTagName('head')[0],
    done = false;
   
    // Attach handlers for all browsers
    script.onload = script.onreadystatechange = function() {
      if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
      done = true;
        success();
        script.onload = script.onreadystatechange = null;
        head.removeChild(script);
      };
    };
    head.appendChild(script);
  };
 
  getScript('http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js');
 
}

// jQuery was already loaded
jQuery('#vmReorderModuleForm').submit(function(event){
    event.preventDefault();
    var productToReorder = jQuery('.reorderUrl');
    var productCount = productToReorder.length;
    var productCounter = 0;
    jQuery('.reorderUrl').each(function(){
        var productUrl = jQuery(this).val();
        jQuery.ajax({
            url: productUrl,
            async: false,
            context: document.body,
            success: function(resultData){
                var result = jQuery.parseJSON(resultData);
                productCounter++;
                jQuery('#vmReorderModule').append('<div class="alert alert-success">'+result.msg+'<div>');
                if(productCounter == productCount){
                    window.location.href = "<?php echo JURI::root() ?>?option=com_virtuemart&view=cart";
                }
            }
        });
    });
});

</script>

<?php }


mod_....php :

if (!class_exists( 'mod_virtuemart_reorder' )) require('helper.php');

$orderNumber = JRequest::getVar('order_number');

if($orderNumber)
{
    $helper = new mod_virtuemart_reorder();
    $order  = $helper->getOrderProducts($orderNumber);
    $products = $order['items'];

    $custom_attributes = json_decode($product->customPlugin);
    // echo "<pre>";var_dump($products);echo "</pre>";

    foreach($products as $pkey => $product)
    {
      
      $attributes = (array)json_decode($product->product_attribute);

      if(is_array($attributes) && count($attributes) > 0)
      {
         $products[$pkey]->attributes = array();

         foreach($attributes as $key => $attr)
         {
            $attr = explode('</span>', $attr);
            $attribute = new stdClass();
            $attribute->id = $key;
            $attribute->name = trim(strip_tags($attr[0]));
            $attribute->value = trim(strip_tags($attr[1]));
            $attribute->custom_id = $helper->getCustomIdByTitle($attribute->name);
            $products[$pkey]->attributes[] = $attribute;
         }
      }
   }

    $pretext = $params->get('pretext', '');
    $buttontext = $params->get('buttontext', '');
    $moduleclass_sfx = $params->get('moduleclass_sfx', '');
    require(JModuleHelper::getLayoutPath('mod_virtuemart_reorder'));
}


Do you have an idea?

My joomla version : 2.5.27

My virtuemart version : 2.6.18

overseas

This is an interpretation of the code in my case.

We see that there is no custom field in re-order URL :

  <p>Recommander vos articles.</p>
    <div class="addtocart-bar">
      <span class="addtocart-button">
        <input type="submit" name="addtocart" class="addtocart-button" value="Recommander" title="Recommander">
      </span>
    </div>
                 <input type="hidden" name="customPrice[0][]" value="92">
                    <input type="hidden" name="customPrice[0][]" value="91">
                    <input type="hidden" name="customPrice[0][]" value="90">
              <input type="hidden" name="quantity[]" value="1">
    <input type="hidden" name="virtuemart_product_id[]" value="36">
    <input type="hidden" name="reorderUrl" class="reorderUrl" value="...........?option=com_virtuemart&controller=cart&task=addJS&virtuemart_product_id[]=36&quantity[]=1&customPrice[0][]=92&customPrice[0][]=91&customPrice[0][]=90">
        <input type="hidden" name="option" value="com_virtuemart">
    <input type="hidden" name="view" value="cart">
    <input type="hidden" name="task" value="add">
    <input type="hidden" name="virtuemart_manufacturer_id" value="Array">

but there is custome field few lines bellow :

<div class="vm-customfield-cart"><span class="product-field-type-E"><span>Axe  160</span></span><br /><span class="product-field-type-E"><span>Cylindre -1.75</span></span><br /><span class="product-field-type-E"><span>Sphere  .25</span></span><br /></div>

Milbo

Are you logged in?

The module seems to be written for vm2 and it cannot work if you are not logged in, I think.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Studio 42

#3
Hi,
I don't have all the code to find why, but the javascript only get this value for adding to cart:
$addToCartUrl
in your case :
....?option=com_virtuemart&controller=cart&task=addJS&virtuemart_product_id[]=36&quantity[]=1&customPrice[0][]=92&customPrice[0][]=91&customPrice[0][]=90


If you updated virtuemart, as said by Milbo, this code cannot work because customfields keys are not same as before.

if you check the text input plugin in vm3 for eg. this is now set so :
$name = 'customProductData['.$product->virtuemart_product_id.']['.$params->virtuemart_custom_id.']['.$params->virtuemart_customfield_id .']

If you use virtuemart 2.x, your reorder module works else not.

If you find the file and function, the key is set (customPrice[0]....) and change it to use the new vm3 style, perhaps you can make it compatible for virtuemart 3


Greets,
Patrick

P.S: the line is $addToCartUrl .= '&customPrice['.$key.']['.$attribute->custom_id.']='.$attribute->id; in your file, i don't have seen it before.

alexanderflr

Hi,
I want to use the same module mod_virtuemart_reorder-1.0.7 but unfortunately doesn't work with latest VM.
Does anyone know how can I make this compatible? I can post the link to the module here if is allowed, but a simple search on google and will show up.

Thanks!
VM 3.2.8, Joomla 3.8.3, PHP 7.0.26

Studio 42

Quote from: alexanderflr on October 17, 2017, 15:22:51 PM
Hi,
I want to use the same module mod_virtuemart_reorder-1.0.7 but unfortunately doesn't work with latest VM.
Does anyone know how can I make this compatible? I can post the link to the module here if is allowed, but a simple search on google and will show up.

Thanks!
Have you try the support of this module ?
Perhaps they can do the changes for you, it's not a very big task.

alexanderflr

Hi,
It's a free module, they don't offer support.
The module is this one http://www.easyjoomla.org/virtuemart-extensions/virtuemart-reorder-module
My problem is it doesn't add anything in the shopping cart.
VM 3.2.8, Joomla 3.8.3, PHP 7.0.26