Friday 5 December 2014

magento admin side cms page only 404 error page not found 1.9


You are getting a 404 on CMS pages because of the remains of old store views still hanging around in the database after deleting store views from magneto admin, to clean up ( delete ) old unused CMS pages for store views you have deleted run this MySQL cmd.

DELETE FROM cms_page_store WHERE store_id NOT IN (SELECT store_id FROM core_store)

You could also use an app like phpmyadmin to manually look for CMS entries that have store IDs that you know not to be in use, but this could get messy.

Wednesday 15 October 2014

Add Custome Meta Data Field in WordPress

Default Usage

$post_id='12';
$mata_value ='field value'
<?php add_post_meta( $post_id, 'my_key', $mata_value); ?>

Adding or Updating a Unique Custom Field

Adds a new custom field if the key does not already exist, or updates the value of the custom field with that key otherwise.
 <?php add_post_meta( $post_id, 'fruit', 'banana', true ) || update_post_meta( $post_id, 'fruit', 'banana' ); ?>
The following will have the same effect:
<?php
// Add or Update the meta field in the database.
if ( ! update_post_meta ($post_id, 'fruit', 'banana') ) { 
 add_post_meta($post_id, 'fruit', 'banana', true ); 
}; 
?> 

Other Examples

Adds a new custom field only if a custom field with the given key does not already exists:
<?php add_post_meta( $post_id, 'my_key', '47', true ); ?>
Adds several custom fields with different values but with the same key 'my_key':
<?php add_post_meta( $post_id, 'my_key', '47' ); ?>
<?php add_post_meta( $post_id, 'my_key', '682' ); ?>
<?php add_post_meta( $post_id, 'my_key', 'The quick, brown fox jumped over the lazy dog.' ); ?>
...

Submit WordPress Posts From The Frontend

1. The Form

First we will set up the basic form which you can use to enter in the information to submit new posts.
<!-- New Post Form -->

<div id="postbox">

<form id="new_post" name="new_post" method="post" action="">

<p><label for="title">Title</label><br />

<input type="text" id="title" value="" tabindex="1" size="20" name="title" />

</p>

<p><label for="description">Description</label><br />

<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>

</p>

<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>

<p><label for="post_tags">Tags</label>

<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>

<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

<input type="hidden" name="post_type" id="post_type" value="post" />

<input type="hidden" name="action" value="post" />

<?php wp_nonce_field( 'new-post' ); ?>

</form>

</div>

<!--// New Post Form -->
There’s nothing special with this form really. The only thing is the wp_dropdown_categories() function, which creates the list of categories you can select from. You can learn more about this function here. Everything else you enter into the form will automatically create a new entry, including tags. Make sure no matter what you do, always include the wp_nonce_field(), for security reasons.

2. The PHP for Processing the Form

Here is where the fun part begins. This is the PHP which you need to process and submit the form information.
<?
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

 // Do some minor form validation to make sure there is content
 if (isset ($_POST['title'])) {
  $title =  $_POST['title'];
 } else {
  echo 'Please enter a title';
 }
 if (isset ($_POST['description'])) {
  $description = $_POST['description'];
 } else {
  echo 'Please enter the content';
 }
 $tags = $_POST['post_tags'];

 // Add the content of the form to $post as an array
 $post = array(
  'post_title' => $title,
  'post_content' => $description,
  'post_category' => $_POST['cat'],  // Usable for custom taxonomies too
  'tags_input' => $tags,
  'post_status' => 'publish',   // Choose: publish, preview, future, etc.
  'post_type' => $_POST['post_type']  // Use a custom post type if you want to
 );
 wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
       // http://codex.wordpress.org/Function_Reference/wp_insert_post
 wp_redirect( home_url() );
} // end IF

// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post'); 

?>
Let’s go over this.
The first line checks if a post has been submitted, and if the action is not empty. You could do this check a number of ways, this is just how I’ve done it:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
The second part is a basic form validation that checks to see if the title and description fields are set, and if so set the values to the variables $title and $description. You may want to check that each of the fields are set this way but I am just showing as an example.
Then, I set up a variable called $post, which is an array of values from the form fields.
  • post_title = $title like we set above using the validation
  • post_content = $description as we set
  • post_category = the category that was selected
  • tags_input = the tags
  • post_status = publish, future or draft. Whatever you want to set it to
  • post_type = post, page, or if you have set up a custom post type you can use that instead
To insert the new post information you can use the function wp_insert_post() and passing the $post variable to it. This is all of course, only if the form has been submitted which is why we put all this within the ‘if’ statement.
Update: I’ve added the function wp_redirect() after the wp_insert_post() function, to fix the redirect to a 404 page. You can change the location by passing the function an argument. For example: wp_redirect( ‘/some-page/’) or wp_redirect( home_url() ).
Close the IF statement and then add the action. The line do_action(‘wp_insert_post’, ‘wp_insert_post’); will add the wp_insert_post hook, with the wp_insert_post from the if statement as the callback, thereby adding the $post variable with all it’s information to the function. Without this line the post will not be submitted and your form will be useless.
I will leave the CSS up to you since I am not here to make your form look pretty, just work. Besides, everyone styles things differently anyways.
You can embed this code in a custom page template, say for example: page-submit.php. Copy the code from page.php and paste it in your custom template. You can create custom pate templates using page-slug.php or page-id.php. I find making custom templates this way 100 times better than using the old way, which involves assigning the page a specific template in the page attributes.
Make sure that you create a new page in the WordPress dashboard, and that your page slug or page id match the name of your template file (ie: page-submit.php – page slug = submit). This new page you create will automatically use the custom template you make, so there is no need to do anything else. You can enter content into the page if you choose to of course, but as long as the custom template includes the new post form, either above or below the_content() function you are good to go.

Monday 13 October 2014

Modifying Administration Tables

Define Your Custom Column


To add a custom column, you must first add its name to the array of column header names. This is done by hooking into the 'manage_{$screen->id}_columns' filter. Your callback is passed the current name array. Simply add another key/value pair to the array and return the entire array. The key can be any arbitrary unique string and the value is the actual column name that will appear in the table header. The following example defines a 'Metabox' column to be added to a 'portfolio' custom post type table.
  1. function bcw_cpt_columns($columns) {
  2. $columns["metabox"] = "Metabox";
  3. return $columns;
  4. }
  5. add_filter('manage_edit-portfolio_columns', 'bcw_cpt_columns');


Sortable Columns

You can only sort columns that contain data from the database related to the table objects, as the sort is achieved by re-querying the table contents. If your data cannot be related to the table objects in a mySQL query, then it cannot be used for sorting of your column.

After defining your column you can make it sortable by hooking the sortable filter variant 'manage_{$screen->id}_sortable_columns' and adding your column name to that array as well. This can often be the same callback as the one for adding the initial non-sortable column.
  1. function bcw_cpt_columns($columns) {
  2. $columns["metabox"] = "Metabox";
  3. return $columns;
  4. }
  5. add_filter('manage_edit-portfolio_columns', 'bcw_cpt_columns');
  6. add_filter('manage_edit-portfolio_sortable_columns', 'bcw_cpt_columns');
You then hook the 'request' filter and check the passed query variables array for your column name under the 'orderby' key. If found, set query arguments that can be used to query the table contents in the correct order (typically the orderby column) and merge into the passed variables. Clicking the column head will toggle the query order between ASC and DESC.

The query arguments you set are the same arguments you define in a custom new WP_Query object. You essentially define an arguments array as if for a new query object and merge it with the array passed by the 'request' filter. The 'request' filter is initiated from the parse_request() method of the WP class. It is defined in wp-includes/class-wp.php. Your callback is passed $this->query_vars.
  1. function bcw_sort_metabox($vars) {
  2. if(array_key_exists('orderby', $vars)) {
  3. if('Metabox' == $vars['orderby']) {
  4. $vars['orderby'] = 'meta_value';
  5. $vars['meta_key'] = '_my_meta_value_key';
  6. }
  7. }
  8. return $vars;
  9. }
  10. add_filter('request', 'bcw_sort_metabox');

Horizontally Reordering Columns

By default, the column order is predefined. So when you add your custom column, it appears at the end of the list at the far right edge of the table. If you want your column to appear elsewhere, or simply wish to rearrange the order of default columns, while you are hooked into the 'manage_{$screen->id}_columns' filter, instead of working with the array passed, copy the elements in the desired order into a new array. Return this new array and it's order will be used instead of the default. The following example moves our Metabox column to appear before the Date column. As it only involves the last column, instead of redefining the entire array, we leave the first part alone and only unset then reset the date column we want to move to the end. You can var_dump($columns) to see the key names used for each column.
  1. function bcw_cpt_columns($columns) {
  2. $date = $columns['date'];
  3. unset $columns['date'];
  4. $columns['metabox'] = 'Metabox';
  5. $columns['date'] = $date;
  6. return $columns;
  7. }
  8. add_filter('manage_edit-portfolio_columns', 'bcw_cpt_columns');

Saturday 11 October 2014

E-Mail Attachments With PHP's mail() Function

Below is a full working example of how to include one or more attachments to an outbound e-mail utilizing PHP's mail() function. Usage Example

<?php
$to = 'destination-address@somewhere.com';
$from = 'source@somewhere.com';
$subject = 'See Attachments';
$message = 'Please review the following attachments.';

// Define a list of FILES to send along with the e-mail. Key = File to be sent. Value = Name of file as seen in the e-mail.
$attachments = array(
 '/tmp/WEDFRTS' => 'first-attachment.png',
 '/tmp/some-other-file' => 'second-attachment.png'
);

// Define any additional headers you may want to include
$headers = array(
 'Reply-to' => 'source@somewhere.com',
 'Some-Other-Header-Name' => 'Header Value'
);

$status = mailAttachments($to, $from, $subject, $message, $attachments, $headers);
if($status === True) {
 print 'Successfully mailed!';
} else {
 print 'Unable to send e-mail.';
}

Where the Magic Happens

<?php
function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') {
 $headers['From'] = $from;

 // Define the boundray we're going to use to separate our data with.
 $mime_boundary = '==MIME_BOUNDARY_' . md5(time());

 // Define attachment-specific headers
 $headers['MIME-Version'] = '1.0';
 $headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"';

 // Convert the array of header data into a single string.
 $headers_string = '';
 foreach($headers as $header_name => $header_value) {
  if(!empty($headers_string)) {
   $headers_string .= "\r\n";
  }
  $headers_string .= $header_name . ': ' . $header_value;
 }

 // Message Body
 $message_string  = '--' . $mime_boundary;
 $message_string .= "\r\n";
 $message_string .= 'Content-Type: text/plain; charset="iso-8859-1"';
 $message_string .= "\r\n";
 $message_string .= 'Content-Transfer-Encoding: 7bit';
 $message_string .= "\r\n";
 $message_string .= "\r\n";
 $message_string .= $message;
 $message_string .= "\r\n";
 $message_string .= "\r\n";

 // Add attachments to message body
 foreach($attachments as $local_filename => $attachment_filename) {
  if(is_file($local_filename)) {
   $message_string .= '--' . $mime_boundary;
   $message_string .= "\r\n";
   $message_string .= 'Content-Type: application/octet-stream; name="' . $attachment_filename . '"';
   $message_string .= "\r\n";
   $message_string .= 'Content-Description: ' . $attachment_filename;
   $message_string .= "\r\n";

   $fp = @fopen($local_filename, 'rb'); // Create pointer to file
   $file_size = filesize($local_filename); // Read size of file
   $data = @fread($fp, $file_size); // Read file contents
   $data = chunk_split(base64_encode($data)); // Encode file contents for plain text sending

   $message_string .= 'Content-Disposition: attachment; filename="' . $attachment_filename . '"; size=' . $file_size.  ';';
   $message_string .= "\r\n";
   $message_string .= 'Content-Transfer-Encoding: base64';
   $message_string .= "\r\n\r\n";
   $message_string .= $data;
   $message_string .= "\r\n\r\n";
  }
 }

 // Signal end of message
 $message_string .= '--' . $mime_boundary . '--';

 // Send the e-mail.
 return mail($to, $subject, $message_string, $headers_string, $additional_parameters);
}

Wednesday 20 August 2014

How to create coupon codes in Magento

Want to set up a promotion on your Magento eCommerce store? Would you like to give your users free shipping? Maybe offer free shipping for just a limited time only? Or perhaps buy 1, get one 50% off? This is why Magento is great! It allows you, the admin, to create very basic coupon codes and/or very specific codes all based on the conditions and actions you set.
In this tutorial, I will show you three examples of different shopping cart rules that will help you set up rules for your own eCommerce store and let your customers take advantage of these great offers!

1. Free Shipping

  1. Log in to Magento Admin.
  2. Click on Promotions –> Shopping Cart Price Rule.
  3. Fill in the fields; be sure to give the rule a Name and Description and decide whether or not you are choosing ALL Customer groups or not.
  4. In the General Information page in the coupon menu, select the Specific Coupon option.
  5. Enter a code in the coupon code field (can be letters or numbers).
  6. In the Uses Per Coupon field, specify the number of times a customer can use this coupon code if you would like to provide a limit. If not, leave blank.
  7. In the Uses Per Customer field, specify the number of times a customer can use this promotion if you would like to provide a limit. If not, leave blank.
  8. In the From/To Date menu, select a time frame for your coupon if you would like to provide one. If not, leave blank.
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Make sure these conditions are met [none for this case]:
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Make sure these actions are met:
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
And if you want something more specific, such as: Free shipping on only a certain item, follow these actions:
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Remember to click Save Rule to save all changes.

2.      Buy 1, Get One [Free] Coupon:

  • Follow steps a-h from previous example first.
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Then ensure these Actions are met:
  • Set the Discount amount to 1. This is the quantity that is discounted (the Y value, the quantity received for free).
  • [Optional] If you want to set the number needed higher than 1, e.g., to 5, set the discount Qty Step (Buy X) to 5. This is the quantity the customer must purchase in order to qualify for the free item.
  • [Optional] If you want to set it to a specific product SKU, you can enter these in the Conditions on the Actions tab.
  • Click Save Rule to save all changes.

3.       Creating a Coupon Code for a Specific Product

  • Follow steps a-h from #1.
Set Conditions
  • On left sidebar, click conditions tab.
  • Click the + button.
  • Select Product Attribute Combination.
  • Click + button.
  • Select SKU.
  • Now you will see the SKU.
  • Place your product SKU here.
Note: If you are not seeing SKU in the dropdown, go to: Catalog –> Attributes –> Manage Attributes. Search for SKU attribute and set the drop down “Use for Promo Rule Conditions” to “YES.” 
 Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Set Actions
  • Under Actions tab, choose how much you’d like to discount.
  • Click Save Rule to save all changes.
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
And… voila! You are now on your way to successful coupon creation in Magento!