I'm trying to get $cert_url and use it in my custom page to create a sharable link to Facebook. However, I have no idea to get the variable functions.php and pass it to custom page.
Functions.php
add_action( 'woocommerce_thankyou', 'get_cert_url' );
function get_cert_url( $order_id ) {
global $wpdb;
$msf_campaign_status = array('wc-completed');
$msf_campaign_detail = get_post_meta($order_id);
$msf_campaign_fund = $msf_campaign_detail['msf_charity_fund'][0];
$order = wc_get_order( $order_id );
if( $order->has_status('processing') ){
$name_on_e_cert = str_replace(" ", "+", $msf_campaign_detail['name_on_e_cert'][0]);
$msf_charity_name = get_option('wc_fields_billing');
if($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_1'){
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_1'];
}
elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_2') {
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_2'];
}
elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_3') {
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_3'];
}
elseif ($msf_campaign_detail['charity_organizations'][0] == 'msf_charity_4') {
$charity_name = $msf_charity_name['charity_organizations']['options']['msf_charity_4'];
}
$donateto = str_replace(" ", "+", $charity_name);
$subtotal = $order->get_subtotal();
$amount = (($subtotal*25)/100);
$request = wp_remote_retrieve_body(wp_remote_get('https://www.api_website.com.my/api/c/product.ashx?key=api_key&orderid='.$order_id.'&name='.$name_on_e_cert.'&donateto='.$donateto.'&amount='.$amount.''));
$data = json_decode($request);
$result[]= $data;
foreach($result[0] as $line) {
$cert_url = $line;
}
return $cert_url;
}
}
The code below is to create a share button to share the link to Facebook.
Custom page
Share this on Facebook
May I know how can I get the $cert_url and use it on custom page? Thanks in advance !
Thank you for your answer. I solved the problem by using add_query_arg().
I'm making a script that should make a copy of an existing order.
I can create the overall order, with this code:
$order = new Order($_GET["id_order"]);
$order->add();
But there's no products in the order - I tried with this:
$order_detail = new OrderDetail($_GET["id_order"]);
$order_detail->add();
What am I doing wrong, how can I copy an existing order?
You can duplicate an order using the duplicateObject() method from the ObjectModel class.
Here is a function that should do the trick :
function duplicateOrder($id_order)
{
$order = new Order($id_order);
$duplicatedOrder = $order->duplicateObject();
$orderDetailList = $order->getOrderDetailList();
foreach ($orderDetailList as $detail) {
$orderDetail = new orderDetail($detail['id_order_detail']);
$duplicatedOrderDetail = $orderDetail->duplicateObject();
$duplicatedOrderDetail->id_order = $duplicatedOrder->id;
$duplicatedOrderDetail->save();
}
$orderHistoryList = $order->getHistory(Configuration::get('PS_LANG_DEFAULT'));
foreach ($orderHistoryList as $history) {
$orderHistory = new OrderHistory($history['id_order']);
$duplicatedOrderHistory = $orderHistory->duplicateObject();
$duplicatedOrderHistory->id_order = $duplicatedOrder->id;
$duplicatedOrderHistory->save();
}
}
I need to let customer edit their pending payment order. By default, woocommerce only allow change the payment method. So, I have created a custom template for this feature.
The problem now I encountered is I can't get the shipping packages in the template.
Here is the code that I adapted from the wc_cart_totals_shipping_html() :
$packages = WC()->shipping->get_packages();
print_r($packages);
foreach ( $packages as $i => $package ) {
//blah blah blah
}
The print_r($packages) give me the null array. But on the checkout page, it's working fine.
Any idea why? Or, get the shipping packages by other method?
Please try this -
global $woocommerce;
$customerZipCode = 75098;
$zipResultArr = csd_check_zip_and_state($customerZipCode);
$bh_packages = $woocommerce->cart->get_shipping_packages();
$bh_packages[0]['destination']['state'] = $zipResultArr['state'];
$bh_packages[0]['destination']['postcode'] = $customerZipCode ;
$bh_packages[0]['destination']['city'] = $zipResultArr['city'];
$bh_packages[0]['destination']['address'] = '';
$bh_packages[0]['destination']['address_2'] = '';
//Calculate costs for passed packages
$bh_shipping_methods = array();
foreach( $bh_packages as $bh_package_key => $bh_package ) {
$bh_shipping_methods[$bh_package_key] = $woocommerce->shipping->calculate_shipping_for_package($bh_package, $bh_package_key);
}
$shippingArr = $bh_shipping_methods[0]['rates'];
if(!empty($shippingArr)) {
$response = array();
foreach ($shippingArr as $value) {
$shipping['label'] = $value->label;
$shipping['cost'] = $value->cost;
$response['shipping'][] = $shipping;
}
}
// This is your shipping
print_r($response);
I am unable to sort product by price in product list page.
Sort by name works fine there.
Any idea guys?
I googled and tried some solution but they didn't solve my problem.
I tried: System->Cache Management->Layered Navigation Indices->Refresh now
Also tried this link: http://www.miromedia.co.uk/blog/300/fixing-the-magento-price-sort-issue.htm
Magento version: 1.3.2.4
There seems to be some kind of bug in this version of Magento, the product catalog index price not updating on product save. So what you can do is by pass the price condition like described below.
Update addAttributeToSort function of Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection class.
Comment out the following code:-
// if ($attribute == 'price' && $storeId != 0) {
// $websiteId = Mage::app()->getStore()->getWebsiteId();
// $customerGroup = Mage::getSingleton('customer/session')->getCustomerGroupId();
//
// if ($this->isEnabledFlat()) {
// $priceColumn = 'e.display_price_group_' . $customerGroup;
// $this->getSelect()->order("{$priceColumn} {$dir}");
// }
// else {
// $priceAttributeId = $this->getAttribute('price')->getId();
//
// $entityCondition = '_price_order_table.entity_id = e.entity_id';
// $storeCondition = $this->getConnection()->quoteInto(
// '_price_order_table.website_id = ?',
// $websiteId
// );
// $groupCondition = $this->getConnection()->quoteInto(
// '_price_order_table.customer_group_id = ?',
// $customerGroup
// );
// $attributeCondition = $this->getConnection()->quoteInto(
// '_price_order_table.attribute_id = ?',
// $priceAttributeId
// );
//
// $this->getSelect()->joinLeft(
// array('_price_order_table'=>$this->getTable('catalogindex/price')),
// "{$entityCondition} AND {$storeCondition} AND {$groupCondition} AND {$attributeCondition}",
// array()
// );
// $this->getSelect()->order('_price_order_table.value ' . $dir);
//
// /**
// * Distinct we are using for remove duplicates of products which have
// * several rows in price index (like grouped products)
// */
// $this->getSelect()->distinct(true);
// }
//
// return $this;
// }
You can do this in Magento standard way other than commenting out the core code. Hope this helps.
You need to configure the price attribute correctly in Magento admin.
Go to attribute management, find the price attribute and ensure that you tick the box on "sortable in frontend"
that should resolve this for you.
I am trying to add new values to an attribute option in Magento using a script to speed up the process since I have over 2,000 manufacturers.
Here is a piece of code that I used to perform exactly this task. Create a custom module (using ModuleCreator as a tool) and then create a mysql4-install-0.1.0.php under the sql/modulename_setup folder. It should contain the following (adapted to your own data, of course!).
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$aManufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer');
for($iCount=0;$iCount<sizeof($aManufacturers);$iCount++){
$aOption['value']['option'.$iCount][0] = $aManufacturers[$iCount];
}
$installer->addAttributeOption($aOption);
$installer->endSetup();
More documentation on the Magento wiki if you want.
If you don't want to do it in a custom module, you could just create a php file that starts with:
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
Answer of Jonathan is correct. But if you want to perform it without installer i.e in any other code, then you might find this helpful:
$arg_attribute = 'manufacturer';
$manufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();
$option['attribute_id'] = $attr_id;
foreach ($manufacturers as $key=>$manufacturer) {
$option['value'][$key.'_'.$manufacturer][0] = $manufacturer;
}
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
More information can be found here.
I have created a function to dynamically add option to attribute
public function addAttributeOptions($attributeCode, $argValue)
{
$attribute = Mage::getModel('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
if ($attribute->usesSource()) {
$id = $attribute->getSource()->getOptionId($argValue);
if ($id)
return $id;
}
$value = array('value' => array(
'option' => array(
ucfirst($argValue),
ucfirst($argValue)
)
)
);
$attribute->setData('option', $value);
$attribute->save();
//return newly created option id
$attribute = Mage::getModel('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
if ($attribute->usesSource()) {
return $attribute->getSource()->getOptionId($argValue);
}
}
You can add an option to your attribute by providing code and option value
$this->addAttributeOptions('unfiorm_type', 'leotartd')
Important! (Hopefully this helps somebody, cause I was stuck like 2h with this issue)
If you are using special characters (such as ä, ö, ü, ß, ×, ...) make sure to encode them properly!
array_walk($manufacturers , create_function('&$val', '$val = utf8_encode($val);'));
Here a simple and very fast way....
Delete an option
/* My option value */
$value = 'A value';
/* Delete an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
$option_id = $attribute->getSource()->getOptionId($value); // Check Option ID from value...
if ($option_id) {
$options['delete'][$option_id] = true;
$attribute->setOption($options)->save();
}
}
/* END ! */
Add or update an option
/* Add/Update an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
$option_id = $attribute->getSource()->getOptionId($value); // Check Option ID...
$options['order'][$option_id] = 10; // Can be removed... Set option order...
$options['value'][$option_id] = array(
0 => $value, // Admin Store - Required !
1 => $value, // Store id 1 - If U want ! Can be removed
);
$attribute->setDefault(array($option_id)); /* If you want set option as default value */
$attribute->setOption($options)->save(); /* That's all */
}
/* END ! */
In my tutorial I am explaining how to read the options from the CSV and create the options pro grammatically.
http://www.pearlbells.co.uk/add-attribute-options-magento-scripts/
please click the tutorial for further explanation
function createAttribute($options) {
$option = array('attribute_id' =>
Mage::getModel('eav/entity_attribute')->getIdByCode(
Mage_Catalog_Model_Product::ENTITY,
'color'
)
);
for ($i = 0; $i < count($options); $i++) {
$option['value']['option'.$i][0] = $options[ $i ]; // Store View
$option['value']['option'.$i][1] = $options[ $i ]; // Default store view
$option['order']['option'.$i] = $i; // Sort Order
}
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
}
Answer of Arvind Bhardwaj enter code here is correct. But if you want to perform it without installer i.e in any other code, then you might find this helpful:
Agree with Arvind but it's only works for insert the single option value and if you want to perform insert multiple option value then you just needs to replace the code from "$option['value'][$key.''.$manufacturer] = $manufacturer;" to "$option['values'][$key.''.$manufacturer] = $manufacturer;" to this.
below is the final code
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
$arg_attribute = 'manufacturer';
$manufacturers = array('Sony', 'Philips', 'Samsung', 'LG', 'Panasonic', 'Fujitsu', 'Daewoo', 'Grundig', 'Hitachi', 'JVC', 'Pioneer', 'Teac', 'Bose', 'Toshiba', 'Denon', 'Onkyo', 'Sharp', 'Yamaha', 'Jamo');
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();
$option['attribute_id'] = $attr_id;
foreach ($manufacturers as $key => $manufacturer) {
$option['values'][$key . '_' . $manufacturer] = $manufacturer;
}
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
I hope its works for insertion multiple option.