I have two custom blocks in my homepage of Magento. One of the blocks displays all the products. And the other block should display the product which was recently clicked on/viewed.
I have created the observer for the event to get the clicked product data.
How can I use this data from observer to show that product's data in my second block in the homepage? I have to pass the data from the observer somehow.
Observer.php
<?php
include 'C:\wamp64\www\magento1924\app\code\local\Company\Namespace\Block\Recommended.php';
class Company_Namespace_Model_Observer {
public function getProductData($observer) {
//Uncomment the line below to log what is contained here:
//Mage::log($observer);
$data = new Company_Namespace_Block_Recommended();
$product = $observer->getProduct();
$pId = $product->getId();
$pName = $product->getName();
//Mage::log($pId);
Mage::log($pName);
$data->recommended($pId, $pName);
}
}
?>
Block file Recommended.php
<?php
class Company_Namespace_Block_Recommended extends Mage_Core_Block_Template
{
public function recommended($pId, $pName){
echo $pId;
echo $pName;
echo "Test!";
}
}
?>
Recommended.phtml
<h1>Recently viewed</h1>
<?php
$this->recommended();
?>
So I'm using this function recommended() to print out the clicked product's data. When I'm in the homepage and I click something, it takes me to the product view page in which I can also see (in the upper part) the product's name and id and also "Test!"(which was also echoed out in the function).
What I want to have is that since I'm also callin the same function recommended() out in the homepage (in the phtml file) I should see the products id, name and also "Test!". The problem is it is only showing the "Test!".
Am I doing something totally wrong?
I will suggested you to use cookies. Once a person clicks on a Product, save their IDs in cookie. And in the block class read the cookie values and grab the product collection like this:
$productIds = array('384','385','385'); // Dummy ids, fetch them from cookies
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->addIdFilter($productIds);
;
I hope this will give you an idea.
Please follow below step to get recently clicked/viewed on home page:
1- Click on CMS->Widgets
2- Click on the 'Add new widget instance'
3- Select Type->Recently Viewed Products
4- Select Design Package Theme->Your Theme
5- Click on Continue Button
6- In Frontend Properties
a. Give the Widget Instance Title
b. Assign to store views->All Store Views
c. Scroll a little down and you will notice a 'Layout updates' section
i. Click on Add Layout Updates
ii. Display On -> Specified Page (you can select any other option too)
Page->(Select your desired page)
Block Reference->(Select your reference to show it)
7- In Widget Options
a. Set 'Number of Products to display'
8- Click on 'Save' button and check your specified page. Don't forget to clear the cache if you don't see anything.
Related
friends.
I'm a newb dev building a website using Wordpress, WooCommerce, Gravity Forms, JetEngine and JetWooBuilder. The service is online photo printing, with the ability to upload user photos and choose between black or white paper.
I have the following situation:
Users upload files thru Gravity Forms.
Function hooked to gform_confirmation gets those files and generates a new product for each file. Function returns array of new products ids. (Function runs on Snippets plugin)
Here comes the trouble: Users must be able to select a color variation before adding the generated products to the cart. But how?
Possible scenarios:
Auto-add new products to cart and allow for variation selection directly on cart.
Could not make that work using my image swatches on cart.
Show custom boolean/switcher field on form page (either before submitting files, on the same page of the file upload form; or after submitting the files, on a second page of the form) and then somehow map that field to the correct variation and add to cart. I guess this could work but seems too much for my capabilities.
Use the product IDs that the function is returning and populate a temporary Shop page with these products, so the user can set the desired variation and add products to the cart. (hence the title of this post) This would work since the Shop page already has everything set up as far as styling, variation swatches and so on.
But how do I get those array values to do that? Could I pass thru an URL redirect after the function runs? And how to tell the Shop page to behave correctly?
I am using JetWooBuilder for a custom Shop page, so any solution that could include it would be the perfect match. But of course I am open to any help that comes on my way.
Thank you everyone in advance!
Below I leave that function for reference:
add_action( 'gform_confirmation', 'create_new_products_for_every_upload', 10, 4 );
function create_new_products_for_every_upload( $confirmation, $form, $entry, $ajax ) {
// Get uploaded-file ids from Media Library and loop thru
$file_ids = gp_media_library()->get_file_ids_by_entry( $entry, $form );
$fresh_prints_ids = array(); // Declare array to collect new product id's
if( $file_ids ) {
foreach( $file_ids as $field_id => $_file_ids ) {
foreach( $_file_ids as $file_id ) {
// Clone product template for each uploaded file
$duplicate_product = new WC_Admin_Duplicate_Product;
$new_product = $duplicate_product -> product_duplicate( wc_get_product( '3552' ) );
$new_product_id = $new_product->get_id(); // Get new product ID
$fresh_prints_ids[] = $new_product_id;
// Get current user details
$current_user = wp_get_current_user(); // Get WP_User object
$display_name = $current_user->display_name; // Get Display name
// Update new product
$new_product->set_status( 'private' );
$new_product->set_name( 'Photo by '.$display_name );
$new_product->set_image_id( $file_id );
$new_product->save();
}
}
}
return $fresh_prints_ids;
return $confirmation;
}
Footnote:
I am generating a new product for every uploaded file to allow users to set and order different quantities for each upload. Also this way users can order different variations of the same upload and they will display nicely separated on the cart. I found that this approach allows me to use as much as possible of the core WooCommerce capabilities, without the need for much further customization when dealing with the cart and checkout system. For security reasons, the products are private and direct access to media files are blocked thru a plugin. Image files stored on Gravity Forms folders are auto-erased after the products are created.
I couldn't really "pass the values", but I ended up using the following workaround:
Before the foreach loop:
Create new temporary category using wp_insert_term.
Set "Unique ID field" value from form to category name and slug.
Inside the foreach loop:
Assign temporary category to newly created products using wp_set_object_terms.
On "Confirmation" settings page of the Gravity form:
Redirect to site_url/category/{Unique_ID} using merge-tags to pull value from form entry.
This allowed me to present the user with a temporary Shop page using the same JetWoo Builder templates I already had set up, which was just what I needed. I still have some work to do, namely on bulk adding presented products to cart with one click and then cleaning up after all the process is done.
I'm looking for someone who can help me out with a problem I'm running into at the moment.
I am trying to add a link onto my product page which redirects the user to the same product but under another category:
We have 2 different Bannersets for sale:
productname: Testproduct 1
productcategory: Static bannerset
info: A bannerset that exists of 7 sizes with a JPG extension
productname: Testproduct 1
productcategory: Interactive bannerset
info: A bannerset that exists of 7 formats in .jpg + HTML5 extension
now, on each individual productpage I want a link (basic html href link or button doesn't really matter) that redirects the customer to the same product but in the other category. for instance:
On the static bannerset product page I want a link that says:
I would like this product in the interactive version.
now if you click on this link, I want the customer to be redirected FROM the current url: http://examplesite.com/shop/static-banners/testproduct-1 TO: http://examplesite.com/shop/interactive-banners/testproduct-1
Is there any way to do this?
I already tried fiddling around with some php codes but I only managed to get the permalink and not change the url ...
I hope my explanation was clear enough to understand.
Thanks in advance,
Joost
<a href="<?php echo home_url(); ?>/shop/interactive-banners/<?php echo $post->post_name; ?>">
-add this inside the loop in your product template
What you need to do is check, under which category the product is currently display. i.e. Static bannerset or Interactive bannerset. And based on that information you can format the target link and/or the button text as well.
I assume you are okay with using the respective category slugs directly.
Following is the code for your reference.
//get current category object
$current_category = get_queried_object();
//get current category id and slug
$current_cat_id = $current_category->term_id;
$current_cat_slug = $current_category->slug;
//identify target category based on current category's slug.
$target_cat = ($current_cat_slug == 'static-banners') ? 'interactive-banners' : 'static-banners';
//for display text (if required)
$display_text = ($current_cat_slug == 'static-banners') ? 'I would like this product in the Interactive version.' : 'I would like this product in the Static version.'
//get target category link
$target_cat_link = get_term_link($current_category->term_id);
//display link as anchor tag
<?php echo($display_text); ?>
Hope this helps.
I would need to reach a custom section by page id (or PageModel Object), but I don't find a way to get the FrontendTemplate. I would like to use it in a dropdown navigation to echo a custom section of the hovered (parent) page.
If somebody will search for, the answer is:
<?php
$articles = \ArticleModel::findPublishedByPidAndColumn($id, $column_name);
echo static::getArticle($articles[0]); // for ex. the first article
?>
I have made a Custom Product type for Woo commerce, only problem is getting it to work.
i've added a filter for the Product type and it appears in the Dropdown box, however when i click it , it has no boxes other than the SKU .
Is there a certain location that the file has to go that im missing? As i have currently just lobbed it with the other product type files.
Thanks for any help you may give
After you add the product class, you also have to manipulate the meta-boxes for the General tab on the product setup pages. The Price box is there, it is just initially hidden unless the product type is "simple" or "external"
There are a few ways to do this. One (not recommended) is to edit the html-variation-admin.php file directly and add "show_if_customproduct" to the price group div.
<div class="options_group pricing show_if_simple show_if_external">
becomes this:
<div class="options_group pricing show_if_simple show_if_external show_if_customproduct">
Another (better) is to add some javascript by calling the woocommerce_product_options_general_product_data action:
add_action('woocommerce_product_options_general_product_data','showType');
function showType(){
echo "<script>jQuery('.show_if_simple').addClass('show_if_customproduct');</script>";
}
That will turn all "simple" product boxes on for "customproduct" boxes in the general section.
Or, your action can just echo a new div with new boxes, set the div class to "show_if_customproduct," and copy the initial content from the group price div in the html_variations file.
You'll also likely have to add templates for the pages, at the very least to get an "add to cart" buttom. Look under templates/single-product to get the idea.
First of all you need to create a class for your custom product type. Let say it is CustomProduct
class WC_Product_CustomProduct extends WC_Product{
public function __construct( $product ) {
$this->product_type = 'CustomProduct';
parent::__construct( $product );
}
}
and you will put this class under includes/ folder. After that, you need to activate this product type;
add_filter( 'product_type_selector', 'add_custom_product_type' );
function add_custom_product_type( $types ){
$types[ 'CustomProduct' ] = __( 'Custom Product' );
return $types;
}
Add this to functions.php
I trying to create product programmatically, but it not showing at category page (at "frontend").
I use next code (yes, bad code, but it is one-time task)
$product = new Mage_Catalog_Model_Product();
...
$product->getResource()->save($product);
$product_id = $product->getId();
foreach (array($default_cat, $category_id) as $cat_id) {
Mage::getSingleton('catalog/category_api')->assignProduct($cat_id,$product_id);
}
It succesfully create product, but when I open category page - it doesn't contain product. When I open product edit page and click "save" - product also added to category page...
What I can do?
UPD. Cache clearing and rendexing doesn't help... And, when I open product edit page - categories list are correct
UPD2. Reason was in qty/stock.
$product->setCategoryIds(1,2,3,4,5)->save()
?