Login | Sign up | Toll-Free: 1-800-752-0455
 
 

Forum

 

3 posts by 2 authors in: Forums > CMS Builder
Last Post: October 26, 2017

I have a "Product Listings" multi section editor and a "Product Pricing Options" multi section editor. In the "Product Pricing Options" I have a list field (Get options from database "value = num", "label - title") created that you can select an item from "Product Listings" and create additional pricing options for the product. The "Product Pricing Options" are connected to the products by the product num. I have the code below and it works fine on the "Product's Details Page".

  // load record from 'product_listings'
  list($product_listingsRecords, $product_listingsMetaData) = getRecords(array(
    'tableName'   => 'product_listings',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $product_listingsRecord = @$product_listingsRecords[0]; // get first record
  if (!$product_listingsRecord) { dieWith404("Record not found!"); } // show error message if no record found
 
      // load record from 'additional pricing'
  list($additionalPricingRecords, $additionalPricingMetaData) = getRecords(array(
    'tableName'   => 'product_pricing_options',
    'where'         => "assign_to_product = '{$product_listingsRecord['num']}'",
    'loadUploads' => true,
    'allowSearch' => false,
  ));
  $additionalPricingRecord = @$additionalPricingRecords[0]; // get first record


    <h2><?php echo htmlspecialchars($product_listingsRecord['title']); ?></h2>
    <p><?php echo htmlspecialchars($product_listingsRecord['summary']); ?></p>
    <h4>
        Item Number: <?php echo htmlencode($product_listingsRecord['sku']) ?><br>
        Price: $<?php echo htmlencode($product_listingsRecord['price']) ?>
    </h4>
  
    <h4>Options:</h4>
    <?php foreach ($additionalPricingRecords as $record): ?>
            <?php echo htmlencode($record['title']) ?><br/>
            Description: <?php echo $record['description']; ?><br/>
            Price: $<?php echo htmlencode($record['price']) ?><br/>
    <?php endforeach ?>

What I need to do now is display the product's pricing options on a products multi-list page. I tried using the code below, but it throws an error on the "num". I guess because it's looking for a record number that doesn't exist because the page is a multi-list.

   // load records from 'product_listings'
  list($product_listingsRecords, $product_listingsMetaData) = getRecords(array(
    'tableName'   => 'product_listings',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

  list($additionalPricingRecords, $additionalPricingMetaData) = getRecords(array(
    'tableName'   => 'product_pricing_options',
    'where'         => "assign_to_product = '{$product_listingsRecords['num']}'",
    'loadUploads' => true,
    'allowSearch' => false,
  ));


<?php foreach ($product_listingsRecords as $record): ?>
<a href="<?php echo $record['_link'];?>"><?php echo $record['title'] ?></a>
<?php echo htmlencode($record['content']) ?>
Item Number: <?php echo htmlencode($record['item_number_sku']) ?><br>
Base Price: $<?php echo htmlencode($record['price']) ?>

    <h4>Options:</h4>
    <?php foreach ($additionalPricingRecords as $recordAddition): ?>
            <?php echo htmlencode($recordAddition['title']) ?><br/>
            Description: <?php echo $recordAddition['description']; ?><br/>
            Price: $<?php echo htmlencode($recordAddition['price']) ?><br/>
    <?php endforeach ?>

Anyone have any suggestions on how to get the price options from the "product_pricing_options" and display them with their perspective products?

Thanks Zicky

Hi Zicky, 

You're going to need to load all the "additionalPricingRecords" for each product displayed, and you're displaying all the products, so you might as well load all the product_pricing_options records.  Then you could loop over all of them for every product_listings record only showing those where assign_to_product matches the product num.

So you'd just remove the "where" from the block that loads from product_pricing_options and add a line (in red): 

<?php foreach ($additionalPricingRecords as $recordAddition): ?>
  <?php if ($recordAddition['assign_to_product'] != $record['num']) { continue; } // skip additions that don't match product ?>

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

Hey Dave,

Thanks for the guidance - that did the trick.