Create Woocommerce shortcodes with order payment_method - woocommerce

I am trying to create a shortcodes of payment_method.
I have a custom page thank you page, and i'm looking for the working code
Found here such code:
add_shortcode( 'custom-woocommerce-name' , 'custom_first_name' );
function custom_first_name(){
$customer_id = get_current_user_id();
$order = wc_get_customer_last_order( $customer_id );
return $order->get_billing_first_name();
}

Related

Sorry, this product cannot be purchased. Woocommerce

I have created a advance custom field using ACF wordpress plugin to check if the product is available for this month or not. The code I am using is below but when it does work to point where it shows add to cart button if the product is available for that month if not then shows the message.
Now the issue is when product is available to purchase if I click on add to cart it says "Sorry, this product cannot be purchased." I am not able to find what is wrong with it.
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
$months = (array) get_field('availability');
$purchasable = in_array( date('F'), $months ) ? $purchasable : false;
return $purchasable;
}
add_action( 'woocommerce_single_product_summary', 'unavailable_product_display_message', 20 );
function unavailable_product_display_message() {
global $product;
if(! $product->is_purchasable() ){
echo '<p style="color:#e00000;">' . __("This product is currently unavailable.") . '</p>';
}
}
Try to change this line of code:
$months = (array) get_field('availability');
to:
$months = (array) get_field('availability', $product->get_id());
The get_field function doesn`t know from which product to get, so this will be an empty array. The variable $purchasable will always be false when this happens.

Developing a Custom WooCommerce Payment Method Plugin

I am development a plugin for custom payment method for WOoCommerce, where in "public function result()" function I want to access/get product id which is bought.
Below is my code so far but it is not working;
public function result() {
global $woocommerce,$wp;
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-
received'] ) );
$orderr = wc_get_order( $order_id );
$itemss = $orderr->get_items();
foreach($itemss as $itemm){
$product_id = $item->get_product_id();
$authenticationcode = get_post_meta($product_id, 'authenticationcode', true );
$merchantcode = get_post_meta($product_id, 'merchantcode', true );
}}
Any help would be very helpful.
I think Inside your foreach loop using a wrong variable.
wrong variable : $item->get_product_id();
Correct varible itemm->get_product_id();

Sort Woocommerce products by most viewed using Post View Counter

Any ideas would be much appreciated.
I am trying to reorder my woocommerce products by most viewed using the post-views-counter plugin.
Followed these examples which did not seem to work Sort products by most viewed
function my_view_filter($query){
if ($query->is_main_query() && ( $query->is_home() || $query- >is_archive()
)
) {
$query->set('post_type', 'product');
$query->set('suppress_filters', false);
$query->set('orderby', 'post_views');
$query->set('order', 'asc');
$query->set('fields', '');
}
}
add_action( 'pre_get_posts', 'my_view_filter' );
First of all, the plugin you're trying to use is outdated. Secondly, it seems to create a table to hold the views and this would require changing the actual MySQL query to retrieve posts ordered by date which is a lot of work looking what you need to do.
You could simply just save views on visit to post meta and use that to order products on the catalog like this:
/**
* Setting post count on each visit
*
*/
add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' );
function prefix_save_product_views( ) {
$product_id = get_the_ID();
$increment = 1;
$current_visit_count = get_post_meta( $product_id, 'product_visit_count', true );
$total_visit_count = (int)$current_visit_count + $increment;
update_post_meta( $product_id, 'product_visit_count', $total_visit_count );
}
/**
* Change the display order based on visit count only in Catalog
*
*/
add_filter('woocommerce_get_catalog_ordering_args', 'prefix_woocommerce_catalog_orderby');
function prefix_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'product_visit_count';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
return $args;
}
You'd run into multiple issues using pre_get_posts
// Remove product category/tag meta from its original position
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
// Add product meta in new position
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 5 );

Woocommerce Product publish, update and delete hooks

I need Woocommerce Product publish, update and delete hooks if any one know then please inform me.
I find this hook :
add_action('transition_post_status', 'wpse_110037_new_posts', 10, 3);
function wpse_110037_new_posts($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'publish'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
//add some cde here
}
}
but it's only display product id, title, publish status etc....but i want product price, category, tag, brand and stock status.
So please replay me if any one know.
Thanks,
Ketan.
Woocommerce Products are basically wordpress posts. You can use wordpress hooks
add_action( 'before_delete_post', 'wpse_110037_new_posts' );
add_action( 'save_post', 'wpse_110037_new_posts' );
function wpse_110037_new_posts($post_id){
$WC_Product = wc_get_product( $post_id);
}
wc_get_product() will return WC_Product object and you can get the product details from it.
I Prefer to check if the status if not a draft. Also you can have the third parameter updateto check if it's an update or not
add_action( 'save_post', array($this, 'wpse1511_create_or_update_product' ), 10, 3);
function wpse1511_create_or_update_product($post_id, $post, $update){
if ($post->post_status != 'publish' || $post->post_type != 'product') {
return;
}
if (!$product = wc_get_product( $post )) {
return;
}
// Make something with $product
// You can also check $update
}
The save_post and save_post_product hooks run before the post_meta is updated and since most of the WooCommerce product data is stored as post_meta, using them might cause issues.
Thankfully, since v3, there are specific WooCommerce hooks that run after a product is updated (woocommerce_update_product) and when a product is created (woocommerce_new_product).
add_action( 'woocommerce_new_product', 'on_product_save', 10, 1 );
add_action( 'woocommerce_update_product', 'on_product_save', 10, 1 );
function on_product_save( $product_id ) {
$product = wc_get_product( $product_id );
// do something with this product
}
This hook will run after WC has updated the product in the DB:
add_action('save_post_product', 'ns_sync_on_product_save', 10, 3);
function ns_sync_on_product_save( $post_id, $post, $update ) {
$product = wc_get_product( $post_id );
}

wooCommerce call function in checkout page

I have a function in my themes functions.php file which displays some information about the product. On the checkout page below the billing address I want to out put the information there.
Here is my function in the themes functions.php
function wc_checkout_description_so_1( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
echo '<div>HTML OUTPUT HERE</div>';
}
I have tried to use add_filter to below the billing address but doesnt not work:
add_filter( 'woocommerce_before_checkout_shipping_form', 'wc_checkout_description_so_1', 10, 2 );
All I need is the output below the shipping information and the above I think should work?
Thanks
J
woocommerce_after_checkout_shipping_form might be more appropriate for displaying something after the shipping address. Either way, the only variable that is passed to the woocommerce_after_checkout_shipping_form hook is the $checkout variable. You can var_dump that variable to see what it is available.
add_action( 'woocommerce_after_checkout_shipping_form', 'wc_checkout_description_so_1' );
function wc_checkout_description_so_1( $checkout )
{
var_dump( $checkout );
}

Resources