0

我的页面显示 4 个可更新项目。但是当单击更新时,它只更新最后一项。我将发布 3 个不同的文件。我已经为此工作了好几天。

我得到了一些关于该做什么的建议,但核心页面上的 foreach 似乎我错过了一些东西,但我不知道是什么。

显示页面

<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>

这是我的控制器页面:

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;
    }
}

这是核心页面:

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__;
4

1 回答 1

0

谢谢巴曼,这非常有效。

您正在为每个项目创建一个单独的表单。如果要一次更新所有项目,请将 foreach 循环放在外部。

将 return $product 从 foreach 循环中取出。它在更新第一项后退出该功能。

于 2022-01-25T18:30:26.743 回答