Magento 2 : 카트에 여러 구성 가능한 제품 변형 추가
-
29-09-2020 - |
문제
나는 한 번에 카트에 구성 가능한 제품의 여러 변형을 추가하려고하고 있으며, 코드를 함께 넣으려고하지만 현재는 제품의 올바른 수량을 추가하고 있지만 첫 번째 변형 만 사용합니다. 다른 말로하면, 2 개의 녹색 티셔츠와 4 개의 흰색 티셔츠를 추가하려고하면 장바구니에 6 개의 녹색 티셔츠가 추가됩니다.
이것은 내가 추가 컨트롤러 내에있는 코드입니다.
public function execute()
{
$paramsData = $this->getRequest()->getParams();
try {
$msg = array();
$errorMsg = array();
foreach($paramsData['qty'] as $pId=>$param){
if (isset($param)) {
$filter = new \Zend_Filter_LocalizedToNormalized(
['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
);
$params['qty'] = $filter->filter($param);
}
$params['product'] = $paramsData['product'][$pId];
$product = $this->initProduct($params['product']);
$params['super_attribute'] = $paramsData['super'][$pId];
/**
* Check product availability
*/
if (!$product) {
return $this->goBack();
}
$this->cart->addProduct($product, $params);
$msg[] = $product->getName();
}
$this->cart->save();
}
$resultRedirect->setPath('checkout/cart');
return $resultRedirect;
}
.
및 그 print_r에서 옵션이 올바른지 확인합니다.
Array ( [super_attribute] => Array ( [90] => 5 ) [qty] => 2 )
Array ( [super_attribute] => Array ( [90] => 7 ) [qty] => 4 )
.
그러나 장바구니에서 첫 번째 super_attribute 중 6 개 (녹색 4 개 대신 6 개의 녹색 티셔츠) 중 6 개를보고 있습니다.
각 항목이나 뭔가를 추가 한 후 카트를 '재설정'하기 위해해야 할 일이 있습니까?
감사합니다.
해결책
나는 대답을 얻었습니다. 문제는 다음 루프 오버라이드 (override)입니다. 컨트롤러에서
우리는 새로운 제품 개체와 이제는 아래의 변경 후에 작업을 수행해야합니다.
$product = $this->initProduct($params['product']);
we 대신 line line,
$storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($params['product']);
.
그것의 작품.
다른 팁
오래된 질문, 그냥 더 좋은 방법을보고하기 위해서. objectManager 를 사용하는 대신 ...
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($params['product']);
.
\ magento \ catalog \ model \ productfactory 를 사용하여 (SOU는 클래스 생성자에 대한 새로운 종속성을 추가해야합니다) 동일한 결과를 생성합니다 ...
$product = $this->_productFactory->create()->setStoreId($storeId)->load($params['product']);
. 제휴하지 않습니다 magento.stackexchange