In my Magento store I have added downloadable products and if user want to download this product user need to subscribe our newsletter. for that I have added newsletter subscriber block in view.phtml file.code is below.
<?php $download = Mage::registry('current_product')->getTypeId();?>
<?php
if($download == 'downloadable')
{ ?>
<div class="pop-upss">
<input type="button" onclick="showMrnlePopup();" value="To Download This Product You Need To Subscribe Our Newsletter" name="submit"/>
</div>
<?php } ?>
so I have set onclick function and when user will click on this button newsletter pop-up will open.
in newsletter PHTML file I get current product's id using below code
<input type="hidden" name="pro_id" value="<?php echo Mage::registry('current_product')->getId();?>">
ok now we go to SubscriberController.php file and it's newAction(). I get current product id in newAction() using below code.
$product_id = $this->getRequest()->getPost('pro_id');
Now what I want using this product Id is :
1). I want all data about current product. -> I got this is using below code :
if($product_id) {
$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product_id);
echo '<pre>';
print_r($_product->getData());
die;
}
2). if this product is downloadable then I want it's sample data and it's link to download product but I can not get current product's download link.my code for get download link is below.
if($_product->hasSamples()) {
$_samples = $this->getSamples();
foreach ($_samples as $_sample){
echo $samplePath = $_sample->getBasePath();
echo $sampleFile = $_sample->getSampleFile();
}
} else {
echo 'not getting anything here...';
}
when I run above code it's go into else part and echo 'not getting anything here...'.
so please anyone who know that how can I get downloadable product link please help me.
Thanks.
Getting these links is a bit complicated. I prepared a little snippet how to get the sample files and the normal links.
public function indexAction()
{
$product_id = $this->getRequest()->getPost('pro_id');
if($product_id) {
/** #var Mage_Catalog_Model_Product $product */
$product = Mage::getModel('catalog/product')->load($product_id);
if($product->getTypeId() == 'downloadable') {
/** #var Mage_Downloadable_Model_Resource_Sample_Collection $samples */
$samples = Mage::getModel('downloadable/sample')->getCollection()->addProductToFilter($product_id);
if($samples->count() > 0) {
foreach($samples as $sample) {
/** #var Mage_Downloadable_Model_Sample $sample */
$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'downloadable/files/samples' . $sample->getSampleFile();
echo 'Sample URL: ' . $url . "<br>";
}
}
/** #var Mage_Downloadable_Model_Resource_Link_Collection $links */
$links = Mage::getModel('downloadable/link')->getCollection()->addProductToFilter($product);
if($links->count() > 0) {
foreach($links as $link) {
/** #var Mage_Downloadable_Model_Link $link */
$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'downloadable/files/links' . $link->getLinkFile();
echo 'Link URL: ' . $url;
}
}
}
}
}
You need to load collections of downloadable/sample and downloadable/link and pass a product model or a product id to it. That's the Magento way ;-)
Related
<?php
/**
* Plugin Name: Display UserID
* Plugin URI: http://www.sitecrafters.pro/plugins/measurement-tracker.zip
* Description: The very seventh plugin that I have ever created.
* Version: 1.10
* Author: Cody King
* Author URI: http://sitecrafters.pro
*/
/**
* #snippet Display UserID
* #author Cody King
* #compatible Wordpress 6.0
*/
function show_form(){
$user_id = get_current_user_id();
echo '<form method="GET">'; // printing form tag
echo '<input type="text" name="inches">';
echo '<input type="submit" name="send_btn" value="Submit">';
echo '</form>';
if (isset($_GET['send_btn'])) { // checking is form was submitted then accessing to value
$bicep = $_GET['inches'];
if ($user_id == 0) {
// The user ID is 0, therefore the current user is not logged in
return; // escape this function, without making any changes to database
}
update_user_meta($user_id,'_bicep_measurement',$bicep);
return "Today your bicep is: $bicep inches!";
}
}
add_shortcode('show_form','show_form');
function checkBiceps(){
$user_id = get_current_user_id();
$bicep = get_user_meta($user_id,'_bicep_measurement',true);
return "Your bicep was: $bicep inches last time";
}
add_shortcode('check_biceps','checkBiceps');
?>
This is a plugin I'm making to track body part measurements for WordPress users. I have gotten the shortcode to work, and it makes a functional input box... Up in the top corner.
For some reason, this form is being displayed in the upper left corner instead of inline, where I put the shortcode. What am I doing wrong here?
I admit, I'm new to this programming language and making Wordpress plugins but I want to learn.
Like this:
<?php
function show_form()
{
$user_id = get_current_user_id();
$str = "";
$str .= '<form method="GET">'; // printing form tag
$str .= '<input type="text" name="inches">';
$str .= '<input type="submit" name="send_btn" value="Submit">';
$str .= '</form>';
if (isset($_GET['send_btn'])) { // checking is form was submitted then accessing to value
$bicep = $_GET['inches'];
if ($user_id == 0) {
// The user ID is 0, therefore the current user is not logged in
return; // escape this function, without making any changes to database
}
update_user_meta($user_id, '_bicep_measurement', $bicep);
return "Today your bicep is: $bicep inches!";
}
return $str;
}
add_shortcode('show_form', 'show_form');
Good day. Please help me to display units of measurement for goods. OcStore 2.3 engine.
By default, units of measurement are not displayed next to the product, they helped me to display them on the product page in this way:
In
catalog/model/catalog/product.php
Find line:
public function getProduct($product_id) {
Before the line mentioned above add the following code:
public function getProductWeightWithUnit($product_id) {
$product_info = $this->getProduct($product_id);
$query = $this->db->query("SELECT unit FROM `" . DB_PREFIX . "weight_class_description` WHERE
weight_class_id='".$product_info['weight_class_id']."'");
if ($query->num_rows) {
return number_format($product_info['weight'],2) . " " . $query->row['unit'];
} else {
return false;
}
}
Save changes and close the file.
Now, open file
catalog/controller/product/product.php
Find line:
if ($product_info['minimum']) {
Before the line mentioned above add the following code:
if ($product_info['weight']) {
$data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
} else {
$data['weight'] = false;
}
Now, the backend code is ready. Based on the theme that you use, you need to edit the correct product.tpl file from your theme. For example if you use the default theme, then the file to edit is the following:
catalog/view/theme/default/template/product/product.tpl
Find the line:
<li><?php echo $text_stock; ?> <?php echo $stock; ?></li>
and add the following code after:
<li><?php echo $weight; ?></li>
Fine! Everything works on the product page. But you need it to work the same in modules (for example Featured Products).
I do all the same steps for the module
Backend:
catalog/controller/extension/module/featured.php
and front end:
catalog/view/theme/default/template/extension/module/featured.tpl
But I get the error:
Notice: Undefined index: product_id in C:\OSPanel\domains\mywebsite.com\catalog\controller\extension\module\featured.php on line 43
That's what it says:
if ($product_info['weight']) {
LINE 43 $data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
} else {
$data['weight'] = false;
}
Why not seen product_id in ??
Open catalog/controller/extension/module/featured.php.
Find
if ($this->config->get('config_tax')) {
Add before
if ($product_info['weight']) {
$weight = $this->model_catalog_product->getProductWeightWithUnit($product_info['product_id']);
} else {
$weight = false;
}
Basically, here I've replaced $this->request->get['product_id'] with $product_info['product_id'], like #K.B. said in his answer, but made more accurate example.
Then, in the same file find
'tax' => $tax,
Add after
'weight' => $weight,
Now go to catalog/view/theme/default/template/extension/module/featured.tpl
Find
<?php if ($product['tax']) { ?>
<span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
<?php } ?>
Add after
<?php if ($product['weight']) { ?>
<span><?php echo $product['weight']; ?></span>
<?php } ?>
You must use $product_info['product_id'] or just $product_id instead $this->request->get['product_id'] Because you can't get product_id's for several products using this expression, which are in this loop foreach ($products as $product_id) {
I just upgraded from woocommerce 1.6 to 2.1. Now product options aren't posting to my hidden form.
I have this function built and have it in the cart.php template file of woocommerce. It's objective is to grab the product options/variances so that I can use it later in the hidden form.
$customs = array();
if (isset($values['variation'])) {
if (count($values['variation']) > 0) {
foreach ($values['variation'] as $property => $propertyValue) {
$customs[] = $property . ': ' . $propertyValue;
}
}
}
I then take that value and append into my hidden form, also found on cart.php
<input type="hidden" id="PRODUCT_CUSTOM_1_<?php echo $counter; ?>" name="PRODUCT_CUSTOM_1_<?php echo $counter; ?>" value="<?php echo implode(', ', $customs) ?>"/>
However, when I POST that form to a URL it doesn't have a value for the custom variable PRODUCT_CUSTOM. http://screencast.com/t/UiRJlCDciLYc All other variables contain a value.
Please help get this code to work in 2.1 like it did in 1.6 woocommerce.
I have this site and I want to put some in header for SEO purposes. The H1 must contain manufacturer name (brand) and product code (model).
So the problem is that I'm not sure how to put this variables in controller's file so I could call them in template file next.
Any ideas? :)
The best way around this would be to edit the product controller
catalog/controller/product/product.php
and change the heading there. It's set with
$this->document->setTitle($product_info['name']);
so you just need to prepend/append the manufacturer name there, such as
$this->document->setTitle($product_info['name'] . ' ' . $product_info['manufacturer']);
Did it! Added this code to controller/common/header.php
if (isset($this->request->get['product_id'])) {
$product_id = $this->request->get['product_id'];
} else {
$product_id = 0;
}
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($product_id);
$this->data['product_info'] = $product_info;
if ($product_info) {
$this->data['manufacturer'] = $product_info['manufacturer'];
$this->data['model'] = $product_info['model'];
}
And this to theme/default/template/common/header.tpl
<?php echo $product_info['manufacturer']; ?> <?php echo $product_info['model']; ?>
I have an online store. A products page that allows the user to view a product and add it to the basket. It is added to the basket by clicking "Add to basket" button.
When a user clicks "add to basket", the script redirects them to the basket page and adds the product to the basket.
My question is, how do I print the basket output on the "basket.php" page? How do I pass the session content into variables to be printed?
Thank you.
"products" table in the database:
id int(11), name varchar(255), price int(11)
product.php
...
<form id="basket" name="basket" method="post" action="basket.php">
<input type="hidden" name="p_id" value="<?php echo $id; ?>"/>
<input type="submit" name="submit" value="Add to basket"/>
</form>
...
basket.php
<?php
//add product to cart with product ID passed from previous script
if (isset($_POST["p_id"]))
{
$p_id = $_POST["p_id"];
$q = mysql_query("SELECT * FROM products WHERE id='$p_id'");
$is = mysql_fetch_row($q); $is = $is[0];
$result = "";
while($row = mysql_fetch_array($q)) {
$name = $row["name"];
$price = $row["price"];
$info = $row["info"];
}
$result .= $name .= $price .= $info;
//$_SESSION['p_id'] contains product IDs
//$_SESSION['counts'] contains item quantities
// ($_SESSION['counts'][$i] corresponds to $_SESSION['p_id'][$i])
//$_SESSION['p_id'][$i] == 0 means $i-element is 'empty' (does not refer to any product)
if (!isset($_SESSION["p_id"]))
{
$_SESSION["p_id"] = array();
$_SESSION["counts"] = array();
}
//check for current product in visitor's shopping cart content
$i=0;
while ($i<count($_SESSION["p_id"]) && $_SESSION["p_id"][$i] != $_POST["p_id"]) $i++;
if ($i < count($_SESSION["p_id"])) //increase current product's item quantity
{
$_SESSION["counts"][$i]++;
}
else //no such product in the cart - add it
{
$_SESSION["p_id"][] = $_POST["p_id"];
$_SESSION["counts"][] = 1;
}
}
?>
<div>
<?php echo $result ?>
</div>
create ajax request on button click, to basket.php with GET veriable todo='add_to_basket', witch you will handle in basket.php
HTML of you add product button or link
Your count of product, you will get by Jquery, just type your count html selectors with IDs
<select id="count_<?=$YOUR_PRODUCT_ID?>"></select>
function add_to_basket(product_id){
var product = {};
product['prod_id'] = product_id;
//here you get count of current product
product['count'] = $("count_"+product_id).val();
$.ajax({
type: "GET",
url: "your_domain/basket.php?todo=add_to_basket",
data: "product",
success:function () {
}
});
}
on your basket.php you handle this request like that
if ($GET['todo'] == "add_to_basket"){
// here you add your data(witch comes from ajax) to SESSION
$_SESSION['basket'] [$GET['prod_id']] = $GET['count'];
return true;
}
then when user click on basket image you redirect him to basket page, in wich you display all product in session
<?foreach ($_SESSION['basket'] as $item){?>
// here you get product info by product id from your database and product count from $_SESSION print it to view , price wille be count*price
<?}?>