0

My page is showing 4 updatable items. But when Update is clicked it is only updating the last item. I have 3 different files I'll post. I've been working on this for days.

I got some advice on what to do, but the foreach on the core page seems like I'm missing somethings, but I cannot figure out what.

Display Page

<div class="cccm-dashboard-content-inner">
    <div class="row">
    </div>
    <div class="ccm-dashboard-content-full">
        <div class="row" style="padding-bottom:6px">
            <div class="col-sm-6"><a href="<?= $productList->getSortURL('alpha') ?>">Product Name</a> -or- <a href="<?= $productList->getSortURL('sku') ?>">(SKU)</a></div>
            <div class="col-sm-6"><a>Pricing</a></div>
        </div>
        <?php if (count($products) > 0) {
            foreach ($products as $product) {
                $pID = $product->getID();
                $price = $product->getBasePrice() ?>
                <form role="form" class="form-inline" method="post">
                    <div class="row" style="border-top:1px solid #333;padding:12px 0">
                        <div class="col-sm-6">
                            <input type="text" id="pName<?= $pID ?>" name="pName[]" value="<?= $product->getName() ?>" required="required" class="form-control ccm-input-text" style="width:100%;margin:1px"/>
                            <input type="text" id="pSKU<?= $pID ?>" name="pSKU[]" value="<?= $product->getSKU() ?>" placeholder="No SKU Set" maxlength="100" class="form-control ccm-input-text" style="width:100%;margin:1px"/>
                            <strong><a href="<?= Url::to('/dashboard/store/products/edit/', $pID) ?>" target="_blank"><?= t('Edit Page Directly') ?></a></strong>
                        </div>
                        <div class="col-sm-6">
                            <div class="input-group">
                                <input type="number" id="changeValue<?= $pID ?>" name="pPrice[]" value="<?= $price ?>" step="0.01" class="form-control ccm-input-number" style="width:100%;margin:1px"/>
                            </div>
                        </div>
                            <?= $token->output('community_store') ?>
                            <input type="hidden" name="pID[]" value="<?= $pID ?>"/>
                        </div>
                    <div class="ccm-dashboard-form-actions-wrapper">
                        <div class="ccm-dashboard-form-actions">
                            <button class="float-end pull-right btn btn-primary" type="submit[]"><?= t('Update All Products') ?></button>
                        </div>
                    </div>
                </form>
            <?php }
        } ?>
    </div>
</div>

This is my controller page:

class Pricing extends DashboardSitePageController {
    public function view() {
        if ($this->request->getMethod() == 'POST') {
            $return = $this->save();
            if ($return) {
                return $return;
            }
        }
        $this->loadFormAssets();
        $this->set('product', $product);
        $this->set('page', $page);
        $productsList = new ProductList();
        $ListItemsPerPage = 3;
        $productsList->setItemsPerPage($ListItemsPerPage);
        $productsList->setGroupMatchAny(true);
        $this->set('productList', $productsList);
        $factory = new PaginationFactory($this->app->make(Request::class));
        $paginator = $factory->createPaginationObject($productsList);
        $pagination = $paginator->renderDefaultView();
        $this->set('products', $paginator->getCurrentPageResults());
        $this->set('pagination', $pagination);
        $this->set('paginator', $paginator);
        $site = $this->getSite();
    }
    public function save() {
        $data = $this->request->request->all();
        if ($this->request->request->all() && $this->token->validate('community_store')) {
            $product = ProductPricing::savePricing($data);
            $event = new ProductEvent($originalProduct, $product);
            Events::dispatch(ProductEvent::PRODUCT_UPDATE, $event);
            if ($data['pID']) {
                $this->flash('success', t('All Listed Products Updated'));
            }
        }
    }
    public function loadFormAssets() {
        $this->requireAsset('css', 'select2');
        $this->requireAsset('javascript', 'select2');
        $this->set('al', $this->app->make('helper/concrete/asset_library'));
        $this->requireAsset('css', 'communityStoreDashboard');
        $this->requireAsset('javascript', 'communityStoreFunctions');
        $pageType = PageType::getByHandle('store_product');
    }
    protected function getHeaderSearch($groupList, $gID) {
        if (!isset($this->headerSearch)) {
            $this->headerSearch = $this->app->make(ElementManager::class)->get('products/search', 'community_store', ['groupList'=>$groupList, 'gID'=>$gID]);
        }
        return $this->headerSearch;
    }
}

This is the core page:

class ProductPricing extends Product {
    /**
     * @return \Concrete\Package\CommunityStorePricing\Src\CommunityStore\Product\ProductPricing
     */
    public static function savePricing($data) {
        $counter = 0;
        foreach($data['pID'] as $aProductID) {
            echo $data['pPrice'][$counter];
            $product = self::getByID($data['pID'][$counter]);
            $product->setName($data['pName'][$counter]);
            $product->setSKU($data['pSKU'][$counter]);
            $product->setPrice($data['pPrice'][$counter]);
            $product->save($counter);
            return $product;
            $counter++;
        }
    }
}
return __NAMESPACE__;

1 Answers1

0

Thank you Barman, this worked perfectly.

You're creating a separate form for each item. If you want to update all items at once, put the outside the foreach loop.

Take return $product out of the foreach loop. It's exiting the function after updating the first item.