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.
Related
I am creating one feature for adding user roles in WordPress default registration, it's working but I want to beautify it by adding a blank in user role selector, if it's showing a dropdown icon that will be awesome. I tried from my end but it's not showing.
My codes are: (enable 'anyone can register' here /wp-admin/options-general.php, to see the changes, now go to register /wp-login.php?action=register)
/*
show user profile in registeration
*/
//1. Add a new form element...
add_action('register_form', 'myplugin_register_form');
function myplugin_register_form()
{
global $wp_roles;
pll_e('Select Role');
echo '<select name="role" class="input">';
foreach ($wp_roles->roles as $key => $value) {
// Exclude default roles such as administrator etc. Add your own
if (!in_array($value['name'], ['Administrator', 'Editor'])) {
echo '<option value="' . $key . '">' . $value['name'] . '</option>';
}
}
echo '</select>';
}
//2. Add validation.
add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
function myplugin_registration_errors($errors, $sanitized_user_login, $user_email)
{
if (empty($_POST['role']) || !empty($_POST['role']) && trim($_POST['role']) == '') {
$errors->add('role_error', __('<strong>ERROR</strong>: You must include a role.', 'mydomain'));
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action('user_register', 'myplugin_user_register');
function myplugin_user_register($user_id)
{
$user_id = wp_update_user(array('ID' => $user_id, 'role' => $_POST['role']));
}
Code is correct, change these lines from
echo '<select name="role" class="input">';
to
echo '<select name="role">';
this will give you a dropdown, without any conflict.
Now, these lines will give you a label
echo ('<label>Select Role</label>: ');
Paste this in your code, as per your need.
Learn inputs, form fields here https://www.w3schools.com/tags/tag_input.asp
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 ;-)
I have a list which I am populating from my DB into multiple checkboxes using a foreach loop:
<?php
$sections_arr = listAllForumBoards(0, 1, 100);
$count_board = count($sections_arr);
$ticker = 0;
foreach($sections_arr as $key => $printAllSections){
$ticker = $ticker + 1;
$sectionId = getBoardPart($printAllSections, 'id');
$sectionName = getBoardPart($printAllSections, 'title');
$sectionSlug = getBoardPart($printAllSections, 'slug');
?>
<dd><label for="<?php echo $sectionSlug; ?>">
<input type="checkbox" name="section[]" id="<?php echo $sectionSlug; ?>" value="<?php echo $sectionId; ?>" /> <?php echo $sectionName; ?></label></dd>
<?php } ?>
The list is populating as expected. But I want to be able to check to make sure that a user selects at least one of the checkboxes. I've searched here in SO and I only got the one that was done using JQuery, but I want to be able to do this verification using PHP
In the file, where your form is submitting (action file) add this condition:
if (empty($_POST['section'])) {
// it will go here, if no checkboxes were checked
}
In your action file, you should have the following
if(empty($_POST['section'])) {
//this means that the user hasn't selected any checkbox, redirect to the previous page with error
}
I have created two php codes one is for add product to cart and the other php code is to remove product from cart.However I was just wondering will I need to create a button using html near the product table so that users are able to add or remove the product from their basket.
<?php
session_start();
// get the product id
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "";
/*
* check if the 'cart' session array was created
* if it is NOT, create the 'cart' session array
*/
if(!isset($_SESSION['cart_items'])){
$_SESSION['cart_items'] = array();
}
// check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
// redirect to product list and tell the user it was added to cart
header('Location: products.php?action=exists&id' . $id . '&name=' . $name);
}
// else, add the item to the array
else{
$_SESSION['cart_items'][$id]=$name;
// redirect to product list and tell the user it was added to cart
header('Location: products.php?action=added&id' . $id . '&name=' . $name);
}
?>
You provide just php code. If you want use it on frontend - you need to create link, form, ajax.
link
<a href="yourcode.php?id=ID&name=NAME&quantity=QUANTITY">
form
<form action="yourcode.php">
<input type="hidden" name="id" value="ID">
<input type="hidden" name="name" value="NAME">
<input type="text" name="quantity" value="1">
<input type="submit" value="Add or Delete">
</form>
ajax (jquery). For ajax you need to return json response as {success: true, message: "Product has been added to the cart"} also you can use $.ajax instead $.get.
$.get('yourcode.php', {id: 'ID', name: 'NAME', quantity: 'QUANTITY'},
function (response) {
if (response.success && response.message) {
alert(response.message);
} else {
alert('Something went wrong');
}
}
);
In the phtml file shows all my database in a table including price, id etc , and then in the php file Details when the user try to press rent it takes the Id of the item and store it in the shop using the method add I keep getting this error
Fatal error: Function name must be a string.
The reason why I store the id of the item from the database so when the user check out I ll retrieve all the information using the id of the item been selected by the user. and also I am using array list to store more then one item, another thing to mention they way of getting the id of the item is by the local variable $tem = $_POST(['$dvdDetails->getDvdId()']); in the adding cart file but i am not really sure if its store the right value as there is a foreach loop and The duration of the variable will be distorted by the end of the method so how can i get the the id of the item been selected.
<?php
require_once('Models/Product.php');
class Shop {
private $_products = array();
public function getProducts()
{ return $this->_products;}
public function addProduct(Product $product)
{ $this->_products[] = $product;
return $this;
}
}
?>
<?php
class Option {
private $_optionKey;
private $_optionValue;
public function getKey()
{ return $this->_optionKey; }
public function getVlue()
{
return $this->_optionValue;
}
public function setOption($key, $value)
{
$this->_optionKey = $key;
$this->_optionValue = $value;
return $this;
}
}
?>
<?php
require_once('Models/Option.php');
class Product {
private $_options = array();
public function getOptions()
{ return $this->_options; }
public function addOption(Option $option)
{ $this->_options[] = $option;
return $this;
}
}
?>
//SHow all
<?php require('template/header.phtml') ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table class="datatable">
<tbody>
<?php foreach ($view->dd as $dvdDetails) {
echo '<tr> <td>'
.'<img src="images/'. $dvdDetails->getPhotoDetails() .'"alt="no picture" height="240" width="820" />' .'<br><br>'.'<font size="3" color="#2E2E2E"><center>'. $dvdDetails->getDvdPhoto(). '</center></font>'.
'<br><br><br>'. '<font size="2" color="blue"><strong>Genre: </strong></font>'. $dvdDetails->getDvdGenre() .
'<br><br>'. '<font size="2" color="blue"><strong>Director: </strong></font>'. $dvdDetails->getDvdDirector() . '<br><br>' .'<font size="2" color="blue"><strong>Ritels: </strong></font>'. $dvdDetails->getDvdRitels() .
'<br><br>' . '<font size="2" color="blue"><strong>Price for rent: </strong></font>'. $dvdDetails->getDvdId()) .
'<br><br>' .
'<div class="ghassar">' .
'<div id="op"> <label>Number of days </label> <select name="days" > <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option>
</select>
<br><br><br><br>
</div>
<div>
<input type="submit" value="Add to the basket" name="rent" id="buttom1" />
</div> ' . '<br><br>' .
'<br><br>' .'<div>'.
'</td> </td> </tr>';
} ?>
</tbody>
</table>
<?php require('template/footer.phtml') ?>
// adding to the cart
<?php
require_once('Models/Dvd_sql.php');
require_once('Models/Shop.php');
$view = new stdClass();
$view->dd = 'SQL';
$dvd_sql = new Dvd_sql();
$view->dd = $dvd_sql->fetchAllStudents(); //->fetchAllStudents();
if (isset($_POST['rent']))
{
$tem = $_POST(['$dvdDetails->getDvdId()']);
$shop = new Shop(); $shop->addProduct($tem);
}
require_once('Views/dvdDetails.phtml');
I think what you're trying to do is get the ID of the DVD from the form page into the form processing page. Assuming that's it, there are a couple of problems with the way you're going about that.
First, $_POST['$dvdDetails->getDvdId()'] is looking in the post data for a string key literally matching the string '$dvdDetails->getDvdId()'. Strings in single quotes aren't interpolated by PHP, and even with double-quotes you really want to stick with simple variables and/or use {$brackets}.
Second, it seems to me that the object $dvdDetails does not even exist in the form processing script. I only see it in the form display code, and that does not automatically transfer it over to the processing script. You'll need to explicitly pass the data you need in the form. For that, you can do something like this:
In the form (following your string concatenation style)
'<input type="hidden" name="item_id" value="' . $dvdDetails->getDvdId() . '">' .
in the processing script
$item_id = $_POST['item_id'];
Edit
The first time around, I missed the fact that you're looping over many DVD items. In that case, my advice would lead to many item_id inputs and only one (the last, I think) will be seen by the processing script. Your "days" selector will suffer from the same problem. In this case, I think simplest solution would be to create a separate form for each DVD and use my advice above. That way only one copy of "days" and "item_id" get sent when you submit the form for a particular DVD.