How to get Magento customer ID - php

Ugh how do I get the customer ID!!? These are all things I've tried! Can you see what I'm doing wrong?
//include_once "app/Mage.php";
require_once '/home/ab71714/public_html/app/Mage.php';
//Mage::app("default");
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
if($customer = Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getModel('customer/customer')->load($customer->getId())->getData();
print_r($customerData);
echo $customerData->getId();
}
//$customerData = Mage::getModel('customer/customer');
//$customerID = $customerData -> getId();
//$userinfo = $customerData->_origData; // fetch users info
$customerID=$customer -> getId();
//$customerID = $customerData->getEntityId();
//$customerID = $customerData[entity_id];

Try
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
echo $customerData->getId();
}
See Current user in Magento?

The fastest way is
Mage::getSingleton('customer/session')->getId()

The function isLoggedIn will only return a boolean as to if a customer is logged in and no other information.
The customer session does have to following functions:
getCustomerId: which will return the customer id
getCustomer: which will return the customer object.

Related

How can I get the subscription back from a Recurly Purchase?

I am building a custom checkout with the Recurly PHP Client. Since we use our own gateway logic, we are forced to use the Create Purchase method, as opposed to Create Subscription (as Create Subscription doesn't accept gateway_code as a parameter). Create Subscription returns the created subscription - easy!
But Create Purchase returns an Invoice Collection. It's possible to tease this apart to find the newly-created $subscription object but this hardly seems like the intended process. Is there (hopefully) a cleaner way of going about it?
My method for purchasing is as follows - see code comments.
protected static function create_subscription( $user_id, $args ) {
$result = false;
$purchase = new Recurly_Purchase();
$purchase->currency = $args['currency'];
$purchase->collection_method = 'automatic';
$purchase->gateway_code = $args['gateway_code'];
$account = new Recurly_Account( $user_id );
$account->email = $args['email'];
$account->first_name = $args['billing_first_name'];
$account->last_name = $args['billing_last_name'];
$account->vat_number = $args['vat_number'];
$billing_info = new Recurly_BillingInfo();
$billing_info->token_id = $args['recurly_token'];
$account->billing_info = $billing_info;
$purchase->account = $account;
$subscription = new Recurly_Subscription();
$subscription->plan_code = $args['plan_code'];
$purchase->subscriptions = array( $subscription );
try {
// "invoice" is the method to transact a Recurly_Purchase.
$purchase = Recurly_Purchase::invoice( $purchase );
if( $purchase instanceof Recurly_InvoiceCollection ) {
// this seems incredibly janky and error-prone
$result = reset( $purchase->charge_invoice->line_items )->subscription->get();
}
} catch ( Exception $e ) {
$result = $e;
}
// I need this to return the $subscription object generated by the purchase
return $result;
}
When you create a subscription, a successful response will include the UUID for that subscription. I am not a PHP developer but it might look something like this:
$subscription = new Recurly_Subscription();
$subscription->plan_code = $args['plan_code'];
$subscription->account = $account;
$subscription->currency = $args['currency'];
$subscription->create();
$uuid = isset($subscription->uuid);
$result = Recurly_Subscription::get($uuid);
return $result;
Also, please note that as of Recurly API version 2.17+ you can now pass gateway_code as a body param to create subscription AND create purchase as you initially hoped to do. Here is a link to the Recurly API Release notes, which indicates when the change was made.

Magento Order Creation Programatically only working on one shipping method

I am using the code below as part of a script to create Magento Orders programatically. The script works fine when I use
freeshipping_freeshipping
as the shipping method. However, when I choose any other shipping method such as a custom shipping method,flatrate_flatrate or usps_1 e.t.c, it gives me the error:
"Please Specify a shipping method"
I have searched everywhere but was unable to find a solution. Any help would be highly appreciated, thanks.
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod($shipping_method);
Edit:
I got it to work with flatrate for non US orders and an extension freeadminshipping on both US and non US orders. However, still unable to use any of the UPS or USPS shipping methods.
This method create Order and I have use this in my project it working. In your code please remove shipping method set code and try I hope that work fine for you. otherwise you can use below my code.
public function createOrderAction($paymentMethod) {
try {
$checkout_session = Mage::getSingleton('checkout/session');
$cq = $checkout_session->getQuote();
$cq->assignCustomer(Mage::getSingleton('customer/session')->getCustomer());
$quoteObj = Mage::getModel('sales/quote')->load($checkout_session->getQuoteId()); // Mage_Sales_Model_Quote
//Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()
$items = $quoteObj->getAllItems();
$quoteObj->reserveOrderId();
// set payment method
$quotePaymentObj = $quoteObj->getPayment(); // Mage_Sales_Model_Quote_Payment
$quotePaymentObj->setMethod($paymentMethod);
$quoteObj->setPayment($quotePaymentObj);
// convert quote to order
$convertQuoteObj = Mage::getSingleton('sales/convert_quote');
$quoteObj->setShippingAddress($cq->getShippingAddress());
$quoteObj->setBillingAddress($cq->getBillingAddress());
$orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());
$orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);
// convert quote addresses
$orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
$orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
// set payment options
$orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));
// convert quote items
foreach ($items as $item) {
// #var $item Mage_Sales_Model_Quote_Item
$orderItem = $convertQuoteObj->itemToOrderItem($item);
$orderItem->setWholesaler($item->getWholesaler());
$orderItem->setComment($item->getComment());
$options = array();
if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
$options = $productOptions;
}
if ($addOptions = $item->getOptionByCode('additional_options')) {
$options['additional_options'] = unserialize($addOptions->getValue());
}
if ($options) {
$orderItem->setProductOptions($options);
}
if ($item->getParentItem()) {
$orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
}
$orderObj->addItem($orderItem);
}
$orderObj->setCanShipPartiallyItem(false);
$orderObj->place();
$quoteObj->setIsActive(0)->save();
$orderObj->save();
$orderObj->sendNewOrderEmail();
return $orderObj->getId();
} catch (Exception $e){
Mage::log($e->getMessage());
Mage::log($e->getTraceAsString());
}
}

Set course teacher in moodle

what function could use to set course teacher
I used this to get course students or teachers
$context = get_context_instance(CONTEXT_COURSE, course_id);
$students = get_role_users(5, $context);
but what in need is to set course teacher by a function
Assign a user a teacher role on a course
$coursecontext = context_course::instance($courseid);
$teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
role_assign($teacherroleid, $userid, $coursecontext);
UPDATE: manually enrol a user on a course - this will also assign a role so you might not need the above code?
if (enrol_is_enabled('manual')) {
// Ensure the manual enrolment plugin is enabled.
$enrolplugin = enrol_get_plugin('manual');
// Lookup the manual enrolment instance for this course.
$instances = enrol_get_instances($this->course->id, true);
foreach ($instances as $instance) {
if ($instance->enrol === 'manual') {
break;
}
}
if ($instance->enrol !== 'manual') {
throw new coding_exception('No manual enrol plugin in course');
}
// Enrol the user with the required role
$enrolplugin->enrol_user($instance, $userid, $roleid);
}

Magento getData from guest on success.phtml

I am trying to get the subtotal amount on checkout success page. It works good for registred users:
$_order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$amount = $_order->getData('subtotal');
$total = number_format($amount, 2);
But when the order is processed by a guest, $total is empty.
What can be done?
P.S.: I am using Magento 1.6.1 CE
Taken from Magento's app/code/core/Mage/Checkout/Block/Onepage.php:
if (!$this->isCustomerLoggedIn()) {
return $this->getQuote()->getShippingAddress();
} else {
return Mage::getModel('sales/quote_address');
}
I am 99% sure you can do exactly the same with getOrder() and with Mage_Checkout_Block_Success =)
Note: the isCustomerLoggedIn() method is defined at Mage_Checkout_Block_Onepage_Abstract which is not inherited by Mage_Checkout_Block_Success. So, you could simply use its implementation:
public function isCustomerLoggedIn()
{
return Mage::getSingleton('customer/session')->isLoggedIn();
}
E.g. your code now shall look like this:
if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
$order = Mage::getSingleton('checkout/session')->getOrder();
} else {
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
}
Sorry for saying non-sense things before...

Get Customer Name for Coupon Codes in Magento

I have searched on the net and on this site for a solution but couldn't find any.
I would like to get the customer name associated to a coupon code in Magento. I found some code for the times the coupon code was used but I couldn't get the customer name.
I tried the following code:
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app( 'admin' );
$coupon = Mage::getModel('salesrule/coupon/usage');
$coupon->load('dd_38WX9N', 'code');
if($coupon->getId()) {
$timesUsed = $coupon->getTimesUsed();
$customer = $coupon->getCustomerId();
echo $timesUsed;
echo $customer;
}
?>
Could someone please help me with that and point me to the right direction.
Thanks a lot for your help.
Daniel
Here is the good solution. you need to use the resource and not the model to get access to the table salesrule_coupon_usage and get the customer_id list
/*#var $coupon Mage_SalesRule_Model_Coupon */
$coupon = Mage::getModel('salesrule/coupon');
$coupon->load('dd_38WX9N', 'code');
if($coupon->getId()){
/* #var $resourceCouponUsage Mage_SalesRule_Model_Mysql4_Coupon_Usage */
$resourceCouponUsage = Mage::getResourceModel('salesrule/coupon_usage');
$read = $resourceCouponUsage->getReadConnection();
$select = $read->select()
->from($resourceCouponUsage->getMainTable())
->where('coupon_id = ?', $coupon->getId());
$data = $read->fetchAll($select);
print_r($data);
foreach($data as $couponUsage){
$customer = Mage::getModel('customer/customer')->load($couponUsage['customer_id']);
echo $customer->getName();
}
}
When you are able to get the ID of the customer, you also can get the whole customer object:
$customer_data = Mage::getModel('customer/customer')->load($customer_id);
print_r($customer_data);
There you can get the name as well.

Categories