Magento programmatically creating a sales/quote_address object - php

I am having an issue with creating and adding sales/quote_address objects to the multishipping checkout process. Currently, whenever I create a new address object, it is just reusing the exact same object over and over; Thus, when I add items to one address, it will add them to all addresses. As a check, I put a for loop after my main loop to echo out all of the ID's of the created addresses - it always echos out the number 3. When i try to dynamically change the ID of the newly created addresses(commented out section) they won't even show up at all in the final for loop. My code is as follows:
//dynamically create the addresses and add them to the shipping information screen
$idCounter = 1;
foreach($dropshippersCombineWProducts as $dropshippersWCProducts) {
$newAddress = null;
$newAddress = Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress);
//$idCounter++;
//$newAddress->setId($idCounter);
foreach ($newAddress->getItemsCollection() as $item) {
$item->isDeleted(true);
}
foreach ($dropshippersWCProducts[1] as $_item) {
$newAddress->addItem($_item);
}
$quote->setShippingAddress($newAddress);
$newAddress->collectShippingRates();
}
$addresses = $quote->getAllShippingAddresses();
foreach ($addresses as $address) {
echo $address->getId();
}
Any help would be much appreciated.

The getSingleton method always returns the same object instance. Try changing that call to getModel and see if it doesn't fix the problem for you.

Related

Is there a way to add a second custom field in google people api when creating a person?

I am importing google contacts from a csv file in a specific manner, in the file I have 2 custom fields that need to be added to every contact when they are imported. I have added the first custom field but have no idea how to add a second one programatically. It seems possible as you can add a second custom field from the contact page. Here is the code to add one custom field to the contact:
$opened_file=fopen("export_test.csv", "r");
while(($data = fgetcsv($opened_file, 1000,",")) !== FALSE)
{
$contacts[] = $data;
}
$person = new Google_Service_PeopleService_Person();
$custom_field = new Google_Service_PeopleService_UserDefined();
for($i = 1; $i < count($contacts); $i++)
{
$custom_field->setKey($contacts[$i][50]);
$custom_field->setValue($contacts[$i][51]);
$person->setUserDefined($custom_field);
}
I have tried making a new user defined object, setting the key and value and attaching it to the person but this just overwrites the first custom field. I have also looked this issue up but turned up with nothing. Is it possible to add a second custom field programatically?
UserDefined is a list in the documentation https://developers.google.com/people/api/rest/v1/people#resource:-person.
I'm unsure about the PHP syntax, but based on examples in https://github.com/googleapis/google-api-php-client, try doing
$custom_field_array = array();
...
$person->setUserDefined($custom_field_array);

OctoberCMS get list of content files

Trying to get content files started with cont*
using :
Content::loadCached('theme', 'listOfContentFiles');
And getting an error.
I can get one but not the list.
Seems there is no direct way of doing it, you can use this code to get list manually and filter it by your self
use Cms\Classes\Content;
use Cms\Classes\Theme;
$activeTheme = Theme::getActiveTheme();
$instance = Content::inTheme($activeTheme);
$items = $instance->newQuery()->lists('fileName');
$loadedItems = [];
foreach ($items as $item) {
// we need to manually filter data you can
// add more logic here for sub directory parsing etc
if(starts_with($item, 'cont_')) {
$loadedItems[] = Content::loadCached($activeTheme, $item);
}
}
dd($loadedItems);
// if you want to make it collection
$result = $instance->newCollection($loadedItems);
it will return you list of content files in active theme by our filter logic.

Magento duplicate attriubte set new attributes not showing in front end

Duplicated attriubte set for new product line.
Attributes set show in backend and front end.
Created new attribute and added to attribute set.
New attribute show's in back end but not front end.
If I add the new attribute to the origonal set that was duplicated the new attribute will now show on the front end for the product assoiated with the duplicated attribute group.
So basicly A new attribute added to a duplicate set won't show on the front end until its added to the attribute set that was duplicated.
I've checked to make sure the attribute is visable on front end etc and tried it several times checking the settings.
The goal is to be able to duplicate a attribute set and add new attributes for different product types. Then call the folder by id and display the assoiated attributes.
I've called the attribute group by ID (spec's). This code is working.
<?php
require_once 'app/Mage.php';
Mage::app();
$attributeGroupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection');
$product = $this->getProduct();
foreach ($attributeGroupCollection as $attributeGroup) {
$attributeGroup->getAttributeGroupId();
$attributeSpecs = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter(41);
}
?>
Help is appreciated, thanks
Duplicating an attribute set is duplicating at a time (the moment when you click the button). If you want to add an attribute in both attribute sets, you need to create it in both, for that, you just need to create your attribute and drop it in the section you want to have it linked to (what you call "folder"). If you want to do it programmatically. You need to create an observer that will be fired after the attribute set is saved and that will add the attribute to the duplicated attribute set. I would not advise to do so because it means that you need to enter the ID of the attribute set duplicated and it can be tricky thing when it comes to pushing the development to the production. I am sure there is a workaround for what you want to achieve.
The problem was is magento creates a new id for each new group folder in the new duplicated attribute set (makes sense). I was calling by ID for the group and as a result will only show attributes in the origional folder (weird) even if the product was assoicated with the new attribute set. What I did was get the current attribute set id and then sort out the attribute groups by name so even if the attribute sets are coppied as long as the folde has the name it will display in the custom loop displaying the attributes. Here is my working code:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
$attributeSetID = $attributeSetModel->getAttributeSetID();
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetID)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
if (strpos($groupName , 'Specifications') !== false) {
echo 'true';
$specificationsNum = $groupId;
};
if (strpos($groupName , 'eatures') !== false) {
echo 'true';
$prodfeaturesNum = $groupId;
};
}
//echo $specifications;
$specifications = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($specificationsNum);
$prodfeatures = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($prodfeatures);

magento - retrieve values from catalog/eav_attribute

Ive added a field to catalog/eav_attribute and managed to save data into it. I just cant figure out how to get the data out in the front end.
im using the event observer "catalog_product_load_after" and im trying to extract "is_featured_attribute" from the "catalog/eav_attribute" resouce, im fairly new to magento and just cant figure out how to get the data.
Any help would be greatly appreciated
Dave
It would help if you could show what your observer function looks like, but I'll hypothesize. Essentially you need to load the product from the observer to retrieve your attribute value;
public function yourClass($observer) {
// Get the observed product
$item = $observer->getProduct();
// Get value of your attribute (guessing Yes or No)
$myAttr = $item->getIsFeaturedAttribute();
if($myAttr == 'Yes') {
// Do something
$isItYes = true;
} else {
// Do something
$isItYes = false;
}
return $isItYes;
}

Adding custom option to order line

I'm currently trying to add a custom option to a specific orderline on add to cart via the following:
public function addToPackageQuote()
{
$cart = Mage::getSingleton("checkout/cart");
$quote = Mage::getSingleton("checkout/session")->getQuote();
$packageId = Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId();
$products = $this->sortArray();
foreach ($products as $productInfo) {
try {
$split = explode(",", $productInfo);
$_product = Mage::getModel('catalog/product')->load($split[0]);
if($_product->isConfigurable()) {
$simpleId = $this->getConfigurableSimple($split[1],$split[3],$split[0]);
} else {
$simpleId = $split[0];
}
$product = Mage::getModel('catalog/product')->load($simpleId);
$options = new Varien_Object(array(
"qty" => 1,
"custom_options" => array(
"package" => $packageId,
"packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
)
));
$quote->addProduct($product, $options);
$this->_getSession()->setCartWasUpdated(true);
$quote->save();
} catch (Exception $e) {
echo $e->getMessage();
}
$this->addFreeItems();
}
$cart->save();
unset($_SESSION['products']);
unset($_SESSION['productId']);
$cart->save();
// Let's unset all the package sessions (apart from a few that are needed!).
$this->kill();
}
This method is completely seperate from the generic add to cart handler, and is used purely in a packages system so that it adds simple products exclusively (also breaks down configurables super attribute to find the simple product too).
These simple products have no custom options attached to them in the Magento backend, nor is it a goal to add custom options to the product itself. What I would like to do is attach custom options to the order-line that is then transferred over to the order if a purchase is made. So effectively data that is added at the add to cart method and no where else!
The add to cart method works as expected it's just not including the custom options I am trying to attach. I have also tried defining the options object as simply:
$options = new Varien_Object(array(
"qty" => 1,
"package" => $packageId,
"packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
)
The above info, not including qty is not in the orderline object at all, and I can't seem to work out where to move on from here.
Endlessly googling at the moment so some help would be most appreciated!!
I do appreciate I’m instantiating the product model object twice in this, however the plan is to just get it working then optimise! :)
You have to set the custom options for the product before adding it to cart.
$product->setCustomOptions($options);
The in Mage_Sales_Model_Quote::_addCatalogProduct() the custom options will be added to the cart item.
See also here: http://www.magentocommerce.com/boards/viewthread/49659/
By the way: Your code may be pretty slow because you are loading products twice in a foreach loop. You should consider some refactoring by using the product collection instead. Also it looks kind of hackish to directly access the $_SESSION variable here. You could rather use the Checkout Session for that (Mage::getSingleton('checkout/session')).
I have now resolved this, after much headache. You can add a custom option to the cart and not have to instantiate the product object and save a custom option to do this, it can be done via tacking onto an observer, and pulling the quote items.
After tacking onto: sales_quote_add_item
I then used:
public function addCustomData($observer) {
$event = $observer->getEvent();
$quote_item = $event->getQuoteItem();
$quote = $session->getQuote();
$quote_item->addOption(array("product_id" => $quote_item->getProduct()->getId(),
"product" => $quote_item->getProduct(),
"code" => 'PackageId',
"value" => Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId()
));
$quote->save();
}
It is most important to include the product object and id, as the function doesn't use the loaded object for some reason.
You can then get at the object via:
$_item->getOptionByCode('PackageId')->getValue();
Quick piece of handy info, if it dumps a stack trace in front of you it can't find the defined option, lose the getValue() (if using var_dump) function to see if you are getting a null value, otherwise xdebug will give you a ton of hints to get around it.

Categories