Get access to $item->getNumberOfDownloadsUsed() function on Download.php - php

I need to get the number of downloads bought and used, that's two methods that are available on $item object. But I can't instance $item on the file that I'm working:
app/code/core/Mage/Downloadable/Helper/Download.php
On this file, I need to retrieve the number of boughts and used of the download purchased.
I'm also trying to get the order ID or at least the link hash to identify that number of boughts/used downloads with the unique id of the purchase.
For example, I try this:
Mage::getModel('downloadable/link_purchased_item')
->load($this->getOrderItem()->getOrder()->getId(), 'order_id');
But $this->getOrderItem() is not available on Download.php file. I was trying this:
Mage::getModel('downloadable/link_purchased_item')->getCollection()
->addFieldToFilter('order_item_id', $this->getOrderItem()->getId());
But obviously getOrderItem() is unavailable.
Fatal error: Call to undefined method Mage_Downloadable_Helper_Download::getOrderItem() in /[...]/app/code/core/Mage/Downloadable/Helper/Download.php on line 135
But I'm able to use the customer singleton to retrieve client data like this:
$cliente = Mage::getSingleton('customer/session')->getCustomer();
So on this file I'm able to access others methods, but I'm unable to get the following details:
Number of downloads bought
Number of downloads used
Order ID or Link hash ID.
So please, I'm requesting how to get the current order instance and / or the current link instance, on the Download.php related with the file downloading.
Thank you!

You are doing it wrong, you can not get any object like this $this->getOrderItem() without set/declaring them. As you are trying to get order_id and item_id in the helper, which you are using on a custom page redirected from customer downloadable products. Here is what you have to do
Step - 1
From customer My Downloadable products, in the redirect url you have to pass that products order_id and item_id as parameter. You can get them from below code
$orderId = $_item->getPurchased()->getOrderId();
$itemId = $_item->getId();
Step -2
Now in your template file while using your helper function pass this order_id and item_id to method parameter. Like below code
$orderId = $this->getRequest()->getParam('order_param');
$itemId = $this->getRequest()->getParam('item_param');
//Your helper function
Mage::helper('your_helper')->yourMethod($orderId, $itemId);
Step - 3
In your helper file you can use below code
public function yourMethod($orderId, $itemId)
{
$linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')
->load($orderItem, 'order_id');
$LinkPurchaseOrderItemId = Mage::getModel('downloadable/link_purchased_item')->getCollection()
->addFieldToFilter('order_item_id', $itemId);
}
Note: Do not make any changes in core files, instead override them

Related

Displaying order ID as a UNIQUE string type in woocommerce

Ok I want to display the latest Order id as a unique string without the #
I have used this code to attempt to display on checkout page but it displays nothing.
<?php print $order->get_id();?>
Also i would like a function that allows the place order button on the checkout page to run my custom gateway when its pressed currently I am doing this using html.
If i'm understanding correctly your function get_id() is returning something like "#1234";
In this case you could just do a str_replace(). For example:
$id = "#1234";
$id = (int) str_replace("#", "", $id);
If not, please edit your question.

OpenCart - PHP - Check if a product (download) is already purchased

Regarding OpenCart store selling digital downloads.
I am trying to add a script to the product page to warn the customer if that specific product (download) is already purchased and exists in Account>Downloads.
The reason is to avoid customers purchasing the same product twice.
Appreciate your help.
Edit:
I have tried getting the product IDs of all orders by a customer using a SQL query and that works fine externally but inside OpenCart I am facing issues.
A query such as:
SELECT product_id
FROM ocuk_order_product
WHERE order_id IN (
SELECT order_id
FROM ocuk_order
WHERE customer_id = 'xxxx'
)
My main problem is not being sure how to get similar results in OpenCart Product page. (Which pages and path exactly and where inside the files)
Tried this post as well: Opencart get product downloads
But it is not exactly working on product page (product.php)
Opencart Version 3.0.2.0
Ok I'll take a stab at this but let's clarify a few things:
1) DB_PREFIX is simply a php constant that's declared in your config.php file. Based on the query you provided it's probably equal to ocuk. The point is, you write queries using this constant and they become portable across installations regardless of the users' chosen database prefix.
2) Opencart is based on the MCVL (an extension of MVC which includes language files) structure. The place to execute queries is in the model. The place to call model methods is in the controller. The controller passes output to the view and often uses language variables from language files.
So what I would do is write function and put it in your product model – in this case that's catalog/model/catalog/product.php. This function is called a method since it's now part of your model class. The function will output the rows in your table that a logged in customer has purchased the given product. It's also important to check (a) that the customer is logged in and (b) that the orders you are querying against are real orders - with an order_status_id > 0. We can go into that but suffice to say that you always need to check the order_status_id if you want to know about actual completed orders. Such a method could look something like this:
public function getOrderProductHistory($product_id) {
$result = [];
if ($customer_id = $this->customer->getId()) {
$sql = "
SELECT *
FROM " . DB_PREFIX . "order o
JOIN " . DB_PREFIX . "order_product op USING (order_id)
WHERE o.order_status_id > 0
AND o.customer_id = '" . (int)$customer_id . "'
";
$query = $this->db->query($sql);
$result = $query->rows;
}
return $result;
}
Now in your controller (on a product page that's catalog/controller/product/product.php) you can call this method to get results like this:
$order_product_history = $this->model_catalog_product->getOrderProductHistory($product_id);
And then do something based on the output:
if ($order_product_history) {
$data['has_been_ordered'] = true;
} else {
$data['has_been_ordered'] = false;
}
Now $has_been_ordered is a boolean that you can access in your view to display a message to the customer. There's still a bit more code for you to write but hopefully this helps get you off to a good start.

Php - Multiple Pages Same Url Different Ids?

I've been working on a project for which i want to create a page leading to different pages but i want to use same URL i have created all the pages but now i am stuck...
the Thing i want is.
i have pages **
index324.php , index34.php , index378.php , index345.php ,
index365.php , index334.php
What i want is to use a url -
index.php?product_id=21
index.php?product_id=23
Something Like Shown Above.
On your index.php, check if $_GET['product_id'] is set. If it is, use it to retrieve the desired ID which in turn you will use for checking against a database/file/whatever.
if (isset($_GET['product_id']))
$product_id = $_GET['product_id'];
Of course, you'll want to do some validation on the fetched ID to be sure it won't harm anything in your system since it can be altered by anyone issuing a GET request to your page.
Suppose that you have product detail pages for each product like: product1.php, product2.php etc ... You can include your desired file in index.php by passing GET parameter. e.g. (index.php?product_id=21) :
if(isset($_GET['product_id'])){
$product_id = $_GET['product_id'];
include('product' .$product_id. '.php');
}
else{
// product_id is not set ...
// do more stuff...
}

How to get random products if I don't have related products in magento

I have an issue. I want to show 4 related products in the product page. That is simple and I have done that. But if the product doesn't have any or has less than 4 products I want the remaining product to randomly appear in the page.
To select 4 random products first you need rewrite class that responsible for related block (or just move this file to local folder) and change logic for function that returns collection to something like next code:
$productsCollection = Mage::getResourceModel('catalog/product_collection');
$productsCollection->getSelect()->order('rand()');
$productsCollection->getSelect()->limit(4);
Hope, it will be helpful
If you're only interested in creating this functionality on the product page you can find most of the magic in Mage_Catalog_Block_Product_List_Related::_prepareData().
To fill out your related products with random products, we first need to know exactly how many random products we'll need. In this case, it's (4 - related products found):
// Existing Magento code near end of method
$this->_itemCollection->load();
// Our code
$numRandomsToGet = 4 - count($this->_itemCollection);
Then we can fetch the appropriate number of random products and add them to the collection:
// Our code
$randCollection = Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/layer')->prepareProductCollection($randCollection);
$randCollection->getSelect()->order('rand()');
$randCollection->addStoreFilter();
$randCollection->setPage(1, $numRandomsToGet);
$randCollection->addIdFilter($this->_itemCollection->getAllIds(), true);
foreach($randCollection as $randProduct)
{
$this->_itemCollection->addItem($randProduct);
}
// Existing Magento code
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
Caveat/Plug: I pulled this code from our Related Products Manager extension for Magento so there might be some fiddling external to this method that needs to get done but I don't think so. If you get stuck you can try downloading the extension and examining the code in its entirety. Or, of course, you can just use the extension as-is.

Set subscriber status in Magento programmatically

I am trying to write a module that syncs my newsletter subscribers in Magento with a external database. I need to be able to update the subscription status in Magento programmatically but I am having diffuculty getting the "setStatus" method in Magento to work. It does not throw any errors but the code does not seem to have any effect. Below is the code where I call the method:
$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();
foreach ($collection as $cust) {
$cust->setStatus(1);
}
In theory, this should set the status of all of my subscribers to "subscribed". I could optionally change the argument sent to "setStatus" to any of the below ints for a different status.
1: Subscribed
2: Status Not Active
3: Unsubscribed
How to best change the subscriber status or get this code working?
Here an import script:
<?php
require_once("./app/Mage.php");
Mage::app();
$subscribers = array('email1#server1.com', 'email2#server2.com');
foreach ($subscribers as $email) {
# create new subscriber without send an confirmation email
Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($email);
# get just generated subscriber
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
# change status to "subscribed" and save
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->save();
}
?>
It seems that newsletter subscribers are also stored elsewhere. What you are setting is just a check in the customer base for some other use.
You need to do the following for each customer as well.
Mage::getModel('newsletter/subscriber')->subscribe($email);
See this link for a complete reference.
Thanks to the link #Ozair shared I was able to figure out what I needed to do.
I was successfully setting the status of the subscriber in the Magento subscriber object but I was not saving the object. I needed to call Magento's save method so it would call the ORM and write it to the database. All I need to do was add
$cust->save();
in the for loop. Below is the whole code snippet.
$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();
foreach ($collection as $cust) {
$cust->setStatus(1);
$cust->save();
}
I Hope this helps someone in the future. I needed it for a Constant Contact - Magento Synchronization extension I was making: http://www.freelunchlabs.com/store/constant-contact-and-magento-sync.html

Categories