Extending Shopware customers API to include all customer's addresses - plugins

I am new with Shopware plugin development, please how to call all addresses related to a single customer?
Shopware v5.4.*

You can find all addresses of a customer using the AddressRepository:
// or you can inject the session and models services
$userId = $this->container->get('session')->get('sUserId');
$addressRepository = $this->container->get('models')->getRepository(Shopware\Models\Customer\Address::class);
$addresses = $addressRepository->findBy(['customer' => $userId]);
$adresses will be an array of Address objects.

After some research and inspiration from Paweł Napierała, I found what I was searching for, here is the final result:
public function getOne($id)
{
$result = parent::getOne($id);
$addresses = $this->getManager()
->getRepository(\Shopware\Models\Customer\Address::class)
->findBy(['customer'=>$id]);
$resultArray = $this->getManager()->toArray($addresses);
$result ['addresses'] = $resultArray;
return $result;
}

Related

Show All Product in Magento 2

I want to show all product if it is enable or disabled doesn't matter.
with this
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
return $collection;
I only get enabled product please help me with that to get disabled product also.
Found two solution on this, please try first one, if it does not work for you, then you can try 2nd one.
You can use disable stock check on your collection by this:
$productCollection = $this->_productFactory->create()->getCollection();
$productCollection->setFlag('has_stock_status_filter', false);
Or else you can use this:
$collection = $this->_productCollectionFactory->create()
->addAttributeToSelect('*')
->load();
// Patch to alter load and get disabled products too
$collection->clear();
$where = $collection->getSelect()->getPart('where');
foreach ($where as $key => $condition)
{
if(strpos($condition, 'stock_status_index.stock_status = 1') !== false){
$updatedWhere[] = 'AND (stock_status_index.stock_status IN (1,0))';
} else {
$updatedWhere[] = $condition;
}
}
$collection->getSelect()->setPart('where', $updatedWhere);
$collection->load();

Magento2 : How to add multiple product in cart using single form submission?

I am new in magento2 and using version 2.1.
I have to add multiple product in cart, the product can be any type and I want to add ajax validation as well with this functionality.
Can any one has idea to achieve this?
Thanks,
Chandan
Hi I got solution for adding multiple products in cart in for loop:
Please refer below code to implement this functionality in MAGENTO 2
if ($product_id) {
$storeId = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
try {
if(isset($params['super_attribute'][$product_id])){
$params_post = array();
$params_post['form_key'] = $this->formKey->getFormKey();
$params_post['qty'] = 1;
$params_post['super_attribute'] = $params['super_attribute'][$product_id];
$finalproduct = $this->productRepository->getById($product_id, false, $storeId);
$this->cart->addProduct($finalproduct, $params_post);
} else {
$params_post = array();
$params_post['form_key'] = $this->formKey->getFormKey();
$params_post['qty'] = 1;
$finalproduct = $this->productRepository->getById($product_id, false, $storeId);
$this->cart->addProduct($finalproduct, $params_post);
}
} catch (NoSuchEntityException $e) {
}
}

DoctrineODM Priming of Multi-Level References

Sorry for the awkward title but I have no better naming for the issue (comments on how to properly name the question are welcome).
Let's say I have 3 Documents:
Category
Product
Version
A Category has many Products. A Product has many Versions.
Now I want to Query for all Categories and list all Products and Versions of each Category.
I know about priming priming.
Is it possible to write a query like:
$qb = $dm->createQueryBuilder('Category')
->field('products')->prime(true)
->field('products.versions')->prime(true)
$query = $qb->getQuery();
Alright it seems that in the current state doctrine-odm does not support multi-level priming. This is a known issue on GitHub.
I found a solution in the GitHub Issue that passes a closure to the prime method to allow at least two level priming. Hope this helps someone.
$myPrimer = function(DocumentManager $dm, ClassMetadata $class, array $ids, array $hints) {
$qb = $dm->createQueryBuilder($class->name)
->field($class->identifier)->in($ids);
if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) {
$qb->slaveOkay(true);
}
if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) {
$qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
}
$results = $qb->getQuery()->toArray();
$nestedPrimers = array(
'address' => true, // List of fields to prime
);
$uow = $dm->getUnitOfWork();
$referencePrimer = new ReferencePrimer($dm, $uow);
foreach ($nestedPrimers as $fieldName => $primer) {
$primer = is_callable($primer) ? $primer : null;
$referencePrimer->primeReferences($class, $results, $fieldName, $hints, $primer);
}
};
The Closure can the be passed to the Primer:
->field('club')->prime($myPrime)

Zend Paginate - find a specific record within the result

I appreciate that this may not be possible, but is there a way to make Zend Paginate go to a specific item (record)?
The result I would like would allow me to seek a specific record in a tabled list of results, and display the appropriate page (within all available pages) combined with a name anchor tag to display the specific record.
To clarify: If I had the results as a Zend_Db_Table_Rowset_Abstract I would use the seek() method in a similar fashion to $rowset->seek(8); Although I don't believe the result returned by the DbSelect adapter is a SeekableIterator?
The code within my Mapper (using the Table Data Gateway pattern):
public function paginate($where = array(), $order = null)
{
$select = $this->getDbTable()->select()->from($this->getTableName(), $this->getTableFields());
foreach ($where as $key => $value) {
$select->where($key, $value);
}
$select->order($order);
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
return $paginator;
}
Within my controller:
$cache_id = sha1('list');
$mapper = new Application_Model_Galleries_Mapper();
if(!($data = Zend_Registry::get('cache')->load($cache_id))) {
$data = $mapper->paginate(array(), $sort);
Zend_Registry::get('cache')->save($data, $cache_id, array('list'), 7200);
}
$data->setCurrentPageNumber($this->_getParam('page'));
$data->setItemCountPerPage(30);
$this->view->paginator = $data;
To return a Zend_Paginator with a seekable iterator (Zend_Db_Table_Rowset) use the Zend_Paginator_Adapter_DbTableSelect() as it returns a rowset object, as opposed to Zend_Paginator_Adaoter_DbSelect() which returns an array().
Zend_Paginator

Symfony form with doctrine table other than getTable()->find() is not working

I get a really anoying error when I try to edit an entry from a table, in tutorial they always use getTable()->find(), but I need to verify that the person logged in is the owner of that entry here what I did:
In the action:
public function executeEdit(sfWebRequest $request)
{
$id = $request->getParameter('id');
$userid = $this->getUser()->getGuardUser()->getId();
$ad = Doctrine_Core::getTable('BambinbazarArticles')->getMyAd($id, $userid);
$this->forward404Unless($ad, sprintf('Object bambinbazar_articles does not exist (%s).', $request->getParameter('id')));
$this->form = new BambinbazarArticlesForm($ad);
}
In the model:
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid);
return $q->execute();
}
I tried it with and without the ->execute(), did doctrine clean, cleared cache, rebuilded model,
Always get the same error 'The "%s" form only accepts a "%s" object.
If I use the Doctrine_Core::getTable('BambinbazarArticles')->find() it work, but of course, i need more than that..
I am becoming crazy over this.
execute() can return multiple rows; effectively you're getting a recordset back, rather than the individual object that your form is expecting. Try fetching a single object, using, e.g.:
return $q->execute()->getFirst();
or
return $q->fetchOne();
Its probably because your query is returning a Doctrine_Collection, not the actual Doctrine_Record youre expecting. Instead of execute use fetchOne.
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid)
->limit(1);
return $q->fetchOne();
}