Add WooCommerce product to cart by SKU - php

Maybe someone could give me a hand resolving this challenge.
I was looking for a solution which would allow me to add products to cart by SKU's instead of WooCommerce generated ID's as I would like to use the same products across different CMS systems.
I have stumbled upon the below code, but it seems not to be compatible anymore? Any advice?
<?php
/**
* Plugin Name: WooCommerce: Add Product to Cart by SKU
* Plugin URI: http://remicorson.com
* Description: Just a demo!
* Version: 1.0
* Author: Remi Corson
* Author URI: http://remicorson.com/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC Product Add to Cart by SKU class
*/
class WC_Add_to_Cart_by_SKU {
/**
* Constructor
*/
public function __construct() {
define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
}
/**
* get_product_id_by_product_sku()
*
* Return product ID from product SKU
*/
public function get_product_id_by_product_sku( $add_to_cart ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );
if ( $product_id ) return $product_id;
return $add_to_cart;
}
}
add_filter( 'woocommerce_add_to_cart_product_id', array( new WC_Add_to_Cart_by_SKU(), 'get_product_id_by_product_sku' ) );
Source: https://gist.github.com/corsonr/c02b46bd34a8471327bbf3adee6507c8

There is no need to use a custom SQL query, as you can use the wc_get_product_id_by_sku() WooCommerce function.
Using this function is much more lighter and effective but also takes into account products that are not in trash. Something your current code doesn't do.
So, this snippet will suffice:
function filter_woocommerce_add_to_cart_product_id( $product_id ) {
// Retrieves the post type of the current post or of a given post
if ( get_post_type( $product_id ) === 'product' ) {
return $product_id;
} else {
$sku = $product_id;
}
// Get product ID by SKU
$product_id = wc_get_product_id_by_sku( $sku );
return $product_id;
}
add_filter( 'woocommerce_add_to_cart_product_id', 'filter_woocommerce_add_to_cart_product_id', 10, 1 );
Note: SKU is assumed to be a numerical value
As you can read in the note, the above answer will only work for numerical values, to make this work for all SKU values, ​​you can use as custom query string = /?add-to-cart-sku=THE-SKU
So you get:
function action_wp_loaded( $url = false ) {
// Make sure WC is installed and add-to-cart-sku query arg exists
if ( ! class_exists( 'WC_Form_Handler' ) || ! isset( $_REQUEST['add-to-cart-sku'] ) || ! is_string( $_REQUEST['add-to-cart-sku'] ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
wc_nocache_headers();
$product_id = wc_get_product_id_by_sku( wp_unslash( $_REQUEST['add-to-cart-sku'] ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
return;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart );
if ( 'variable' === $add_to_cart_handler || 'variation' === $add_to_cart_handler ) {
$was_added_to_cart = woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_variable', $product_id );
} elseif ( 'grouped' === $add_to_cart_handler ) {
$was_added_to_cart = woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_grouped', $product_id );
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ) {
do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url ); // Custom handler.
} else {
$was_added_to_cart = woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_simple', $product_id );
}
// If we added the product to the cart we can now optionally do a redirect.
if ( $was_added_to_cart && 0 === wc_notice_count( 'error' ) ) {
$url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
if ( $url ) {
wp_safe_redirect( $url );
exit;
} elseif ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'action_wp_loaded', 15 );
/**
* Invoke class private method
*
* #since 0.1.0
*
* #param string $class_name
* #param string $methodName
*
* #return mixed
*/
function woo_hack_invoke_private_method( $class_name, $methodName ) {
if ( version_compare( phpversion(), '5.3', '<' ) ) {
throw new Exception( 'PHP version does not support ReflectionClass::setAccessible()' );
}
$args = func_get_args();
unset( $args[0], $args[1] );
$reflection = new ReflectionClass( $class_name );
$method = $reflection->getMethod( $methodName );
$method->setAccessible( true );
$args = array_merge( array( $reflection ), $args );
return call_user_func_array( array( $method, 'invoke' ), $args );
}
Based on: Allow adding multiple products to the cart via the add-to-cart query string & /includes/class-wc-form-handler.php

please try this one, i have include the filter call inside __constructor:
<?php
/**
* Plugin Name: WooCommerce: Add Product to Cart by SKU
* Plugin URI: http://remicorson.com
* Description: Just a demo!
* Version: 1.0
* Author: Remi Corson
* Author URI: http://remicorson.com/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC Product Add to Cart by SKU class
*/
class WC_Add_to_Cart_by_SKU {
/**
* Constructor
*/
public function __construct() {
define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
add_filter( 'woocommerce_add_to_cart_product_id', array(
$this,
'get_product_id_by_product_sku'
) );
}
/**
* get_product_id_by_product_sku()
*
* Return product ID from product SKU
*/
public function get_product_id_by_product_sku( $add_to_cart ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );
if ( $product_id ) {
return $product_id;
}
return $add_to_cart;
}
}
new WC_Add_to_Cart_by_SKU();

Related

Adding multiple products to WooCommerce cart at once with SKU and URL parameters

i found this plugin from Rémi Corson on his website : https://remicorson.com/add-woocommerce-product-to-cart-from-url-using-products-sku/
that uses SKU on add to cart link paramater to work like this : https://storelink.com/?add-to-cart=2312323123 (it can only accept numbers )
my request is i need it to accept multiple SKU like this https://storelink.com/?add-to-cart=2312,2454,5934
and add them to cart.
/**
* Plugin Name: WooCommerce: Add Product to Cart by SKU
* Plugin URI: http://remicorson.com
* Description: Just a demo!
* Version: 1.0
* Author: Remi Corson
* Author URI: http://remicorson.com/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC Product Add to Cart by SKU class
*/
class WC_Add_to_Cart_by_SKU {
/**
* Constructor
*/
public function __construct() {
define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
}
/**
* get_product_id_by_product_sku()
*
* Return product ID from product SKU
*/
public function get_product_id_by_product_sku( $add_to_cart ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );
if ( $product_id ) return $product_id;
return $add_to_cart;
}
}
add_filter( 'woocommerce_add_to_cart_product_id', array( new WC_Add_to_Cart_by_SKU(), 'get_product_id_by_product_sku' ) ); ```
I'm not sure if this will work exactly, but will get you on the right path. You basically create a helper method to create an array and loop through each of the sku or post id values.
/**
* WC Product Add to Cart by SKU class
*/
class WC_Add_to_Cart_by_SKU {
/**
* Constructor
*/
public function __construct() {
define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
}
public function wpso_72553517_add_multiple_products( $add_to_cart ) {
// If a comma isn't found, send to the method immediately
if ( ! strpos( ',', $add_to_cart ) ) :
$this->get_product_id_by_product_sku( $add_to_cart );
endif;
// Convert the product ids to an array.
$product_ids = explode( ',', $add_to_cart );
// Loop through each product sku/id
foreach ( $product_ids as $product_id ) :
$this->get_product_id_by_product_sku( $product_id );
endforeach;
}
public function get_product_id_by_product_sku( $add_to_cart ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );
if ( $product_id ) {
return $product_id;
}
return $add_to_cart;
}
}
add_filter( 'woocommerce_add_to_cart_product_id', array( new WC_Add_to_Cart_by_SKU(), 'wpso_72553517_add_multiple_products' ) );

Add Woocommerce Subscription switch to cart programmatically

I'm looking for a way to programmatically add a switch between two Woocommerce subscription variations to the cart.
We're building a headless WP site, so I don't want to do it with a link, as described in this question
I've tried the following simple way, but it won't allow it that way, since the user is already subscribed:
WC()->cart->add_to_cart( 1907, 1, 1908 );
I want to mimic what is happening when the user goes to the upgrade/downgrade page, selects a new variation and presses Switch subscription. Usually that means that the user is sent to checkout with the switch in cart, I just want it to be added to the cart, since we have our own checkout.
The main issue is that Woocommerce thinks that a subscription product is unpurchaseable when it is added to the cart any other way than through the subscription switch form. The form sends a couple of GET-parameters which sends WC through some extra validation, and then allows the item to be added to the cart if everything else is valid.
My solution to this is to mimic this validation with some functions of my own, mostly stolen from the subscription plugin.
/**
* Must be set to true before programmatically adding subscription switch to cart.
*
* #var bool
*/
$kbhl_adding_switch_to_cart = false;
/**
* Array filled with data about current subscription.
* Should only be retrieved by kbhl_get_current_subscription_data().
*
* #var array
*/
$kbhl_global_current_subscription_data = array();
/**
* Cache whether a given product is purchasable or not to save running lots of queries for the same product in the same request
*
* $is_purchasable_cache taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #var array
*/
$kbhl_purchasable_cache = array();
/**
* $user_subscriptions_to_product taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #var array
*/
$kbhl_user_subscriptions_to_product = array();
/**
* If a product is being marked as not purchasable because it is limited and the customer has a subscription,
* but the current request is to switch the subscription, then mark it as purchasable.
*
* Function is_purchasable_switch() taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #param bool $is_purchasable Current purchasable status.
* #param obj $product Product being checked.
*
* #return bool New purchasable status.
*/
function kbhl_is_purchasable_switch( $is_purchasable, $product ) {
global $kbhl_purchasable_cache;
$kbhl_current_subscription_data = kbhl_get_current_subscription_data();
// Only process this filter if running custom add switch function.
if ( ! empty( $kbhl_current_subscription_data['id'] ) ) {
return $is_purchasable;
}
$product_key = wcs_get_canonical_product_id( $product );
// Set an empty cache if one isn't set yet.
if ( ! isset( $kbhl_purchasable_cache[ $product_key ] ) ) {
$kbhl_purchasable_cache[ $product_key ] = array();
}
// Exit early if we've already determined this product's purchasability via switching.
if ( isset( $kbhl_purchasable_cache[ $product_key ]['switch'] ) ) {
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
// If the product is already purchasble, we don't need to determine it's purchasibility via switching/auto-switching.
if ( true === $is_purchasable || ! is_user_logged_in() || ! wcs_is_product_switchable_type( $product ) || ! WC_Subscriptions_Product::is_subscription( $product->get_id() ) ) {
$kbhl_purchasable_cache[ $product_key ]['switch'] = $is_purchasable;
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
$user_id = get_current_user_id();
$product_limitation = wcs_get_product_limitation( $product );
if ( 'no' == $product_limitation || ! wcs_user_has_subscription( $user_id, $product->get_id(), wcs_get_product_limitation( $product ) ) ) {
$kbhl_purchasable_cache[ $product_key ]['switch'] = $is_purchasable;
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
// Adding to cart.
if ( array_key_exists( $kbhl_current_subscription_data['id'], kbhl_get_user_subscriptions_to_product( $product, $user_id, $product_limitation ) ) ) {
$is_purchasable = true;
}
$kbhl_purchasable_cache[ $product_key ]['switch'] = $is_purchasable;
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
add_filter( 'woocommerce_subscription_is_purchasable', 'kbhl_is_purchasable_switch', 13, 2 );
/**
* Gets a list of the customer subscriptions to a product with a particular limited status.
*
* Function get_user_subscriptions_to_product() taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #param WC_Product|int $product The product object or product ID.
* #param int $user_id The user's ID.
* #param string $limit_status The limit status.
*
* #return WC_Subscription[] An array of a customer's subscriptions with a specific status and product.
*/
function kbhl_get_user_subscriptions_to_product( $product, $user_id, $limit_status ) {
global $user_subscriptions_to_product;
$product_id = is_object( $product ) ? $product->get_id() : $product;
$cache_key = "{$product_id}_{$user_id}_{$limit_status}";
if ( ! isset( $user_subscriptions_to_product[ $cache_key ] ) ) {
// Getting all the customers subscriptions and removing ones without the product is more performant than querying for subscriptions with the product.
$subscriptions = wcs_get_subscriptions(
array(
'customer_id' => $user_id,
'status' => $limit_status,
)
);
foreach ( $subscriptions as $subscription_id => $subscription ) {
if ( ! $subscription->has_product( $product_id ) ) {
unset( $subscriptions[ $subscription_id ] );
}
}
$user_subscriptions_to_product[ $cache_key ] = $subscriptions;
}
return $user_subscriptions_to_product[ $cache_key ];
}
/**
* When a subscription switch is added to the cart, store a record of pertinent meta about the switch.
*
* #since 1.4
*/
/**
* When a subscription switch is added to the cart, store a record of pertinent meta about the switch.
*
* Function set_switch_details_in_cart() taken from plugins\woocommerce-subscriptions\includes\class-wc-subscriptions-switcher.php
*
* #param array $cart_item_data Current cart item data.
* #param int $product_id ID of current product.
* #param int $variation_id ID of current product variation.
*
* #return array Updated cart item data.
*/
function kbhl_set_switch_details_in_cart( $cart_item_data, $product_id, $variation_id ) {
try {
$kbhl_current_subscription_data = kbhl_get_current_subscription_data();
if ( empty( $kbhl_current_subscription_data['id'] ) || empty( $kbhl_current_subscription_data['item'] ) ) {
return $cart_item_data;
}
$subscription = wcs_get_subscription( $kbhl_current_subscription_data['id'] );
// Requesting a switch for someone elses subscription.
if ( ! current_user_can( 'switch_shop_subscription', $subscription->get_id() ) ) {
wc_add_notice( __( 'You can not switch this subscription. It appears you do not own the subscription.', 'woocommerce-subscriptions' ), 'error' );
WC()->cart->empty_cart( true );
return array();
}
$item = wcs_get_order_item( absint( $kbhl_current_subscription_data['item'] ), $subscription );
// Else it's a valid switch.
$product = wc_get_product( $item['product_id'] );
$parent_products = WC_Subscriptions_Product::get_parent_ids( $product );
$child_products = array();
if ( ! empty( $parent_products ) ) {
foreach ( $parent_products as $parent_id ) {
$child_products = array_unique( array_merge( $child_products, wc_get_product( $parent_id )->get_children() ) );
}
}
if ( $product_id != $item['product_id'] && ! in_array( $item['product_id'], $child_products ) ) {
return $cart_item_data;
}
$next_payment_timestamp = $subscription->get_time( 'next_payment' );
// If there are no more payments due on the subscription, because we're in the last billing period, we need to use the subscription's expiration date, not next payment date.
if ( false == $next_payment_timestamp ) {
$next_payment_timestamp = $subscription->get_time( 'end' );
}
$cart_item_data['subscription_switch'] = array(
'subscription_id' => $subscription->get_id(),
'item_id' => absint( $kbhl_current_subscription_data['item'] ),
'next_payment_timestamp' => $next_payment_timestamp,
'upgraded_or_downgraded' => '',
);
return $cart_item_data;
} catch ( Exception $e ) {
wc_add_notice( __( 'There was an error locating the switch details.', 'woocommerce-subscriptions' ), 'error' );
WC()->cart->empty_cart( true );
return array();
}
}
add_filter( 'woocommerce_add_cart_item_data', 'kbhl_set_switch_details_in_cart', 11, 3 );
/**
* Gets subscription data for current user.
*
* #return array Subscription data, also stored to global variable $kbhl_global_current_subscription_data
*/
function kbhl_get_current_subscription_data() {
global $kbhl_adding_switch_to_cart, $kbhl_global_current_subscription_data;
if ( ! $kbhl_adding_switch_to_cart ) {
return array();
}
if ( ! empty( $kbhl_global_current_subscription_data ) ) {
return $kbhl_global_current_subscription_data;
}
$subscription_data = array();
$subs = wcs_get_users_subscriptions();
if ( ! empty( $subs ) ) {
foreach ( $subs as $sub ) {
$subscription_data['id'] = $sub->get_id();
foreach ( $sub->get_items() as $item_id => $item ) {
$subscription_data['item'] = $item_id;
break; // There should only be 1 order item.
}
break; // There should only be 1 subscription.
}
}
$kbhl_global_current_subscription_data = $subscription_data;
return $kbhl_global_current_subscription_data;
}
With this added you can add the switch to the cart as follows:
global $kbhl_adding_switch_to_cart; // If run inside a function.
WC()->cart->empty_cart( true );
$kbhl_adding_switch_to_cart = true;
WC()->cart->add_to_cart( 1907, 1, 1927 );
$kbhl_adding_switch_to_cart = false; // Reset after to get back to default validation.

Override Avada Catalog sorting hook back to default in Woocommerce

I am trying to modify Woocommerce sorting option to get a customized one by adding the following code to Avada child function.php file:
// add custom sorting option
add_filter( 'woocommerce_get_catalog_ordering_args',
'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean(
$_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby',
get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'random_list' == $orderby_value ) {
$args['orderby'] = 'menu_order Date';
$args['order'] = 'ASC';
$args['meta_key'] = '';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options',
'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby',
'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['random_list'] = 'Menu_order_date';
return $sortby;
}
//end custom ordering code
this code work fine if i didn't enable WooCommerce Shop Page Ordering Boxes and actually i wanted the ordering box to be displayed on my shop page and product category, so i contact Avada Support for this issue and it turn out Avada use thier own hook for WooCommerce Shop Page Ordering Boxes with the following code:
/**
* Controls the actions adding the ordering boxes.
*
* #access public
* #since 5.0.4
* #param object $query The main query.
* #return void
*/
public function product_ordering( $query ) {
// We only want to affect the main query.
if ( ! $query->is_main_query() || $query->is_search() ) {
return;
}
if ( $query->get( 'page_id' ) ) {
$page_id = absint( $query->get( 'page_id' ) );
} else {
$page_id = absint( Avada()->fusion_library->get_page_id() );
}
if ( wc_get_page_id( 'shop' ) === $page_id || $query->is_post_type_archive( 'product' ) || $query->is_tax( get_object_taxonomies( 'product' ) ) ) {
if ( Avada()->settings->get( 'woocommerce_avada_ordering' ) ) {
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
add_action( 'woocommerce_before_shop_loop', array( $this, 'catalog_ordering' ), 30 );
add_action( 'woocommerce_get_catalog_ordering_args', array( $this, 'get_catalog_ordering_args' ), 20 );
}
}
}
Avada Support said I can remove Avada function from this hook and add my own using child theme.
add_action( 'woocommerce_get_catalog_ordering_args', array( $this,
'get_catalog_ordering_args' ), 20 );
I searched all over the interent seeking help and I found many pepole asked about almost same question but without answer.
if anyone could help removing avada function that will be very helpful for me and other pepole who's looking for that as well.
How I can remove Avada hooked function from woocommerce_get_catalog_ordering_args action hook?
after several test i disocoverd the following code which added by Avada team as follow:
/**
* Modified the ordering of products.
*
* #access public
* #since 5.1.0
*/
public function catalog_ordering() {
get_template_part( 'templates/wc-catalog-ordering' );
}
/**
* Gets the catalogue ordering arguments.
*
* #access public
* #since 5.1.0
* #param array $args The arguments.
* #return array
*/
function get_catalog_ordering_args( $args ) {
global $woocommerce;
$woo_default_catalog_orderby = get_option( 'woocommerce_default_catalog_orderby' );
// Get the query args.
if ( isset( $_SERVER['QUERY_STRING'] ) ) {
parse_str( sanitize_text_field( wp_unslash( $_SERVER['QUERY_STRING'] ) ), $params );
}
// Get order by.
$pob = ( ! empty( $params['product_orderby'] ) ) ? $params['product_orderby'] : $woo_default_catalog_orderby;
// Get order.
$po = 'asc';
if ( isset( $params['product_order'] ) ) {
// Dedicated ordering.
$po = $params['product_order'];
} else {
// Get the correct default order.
$po = 'asc';
if ( 'date' === $pob || 'popularity' === $pob || 'rating' === $pob || 'price-desc' === $pob ) {
$po = 'desc';
}
}
// Remove posts_clause filter, if default ordering is set to rating or popularity to make custom ordering work correctly.
if ( 'default' !== $pob ) {
if ( 'popularity' === $woo_default_catalog_orderby || 'rating' === $woo_default_catalog_orderby ) {
WC()->query->remove_ordering_args();
}
}
$orderby = 'date';
$order = strtoupper( $po );
$meta_key = '';
switch ( $pob ) {
case 'menu_order':
case 'default':
$orderby = $args['orderby'];
break;
case 'date':
$orderby = 'date';
break;
case 'price':
case 'price-desc':
add_filter( 'posts_clauses', array( $this, 'order_by_price_post_clauses' ) );
add_action( 'wp', array( $this, 'remove_ordering_args_filters' ) );
break;
case 'popularity':
$meta_key = 'total_sales';
add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
add_action( 'wp', array( $this, 'remove_ordering_args_filters' ) );
break;
case 'rating':
$meta_key = '_wc_average_rating';
$orderby = array(
'meta_value_num' => strtoupper( $po ),
'ID' => 'ASC',
);
break;
case 'name':
$orderby = 'title';
break;
}
$args['orderby'] = $orderby;
$args['order'] = $order;
$args['meta_key'] = $meta_key;
return $args;
}
if i just modify the following:
// Remove posts_clause filter, if default ordering is set to rating or popularity to make custom ordering work correctly.
if ( 'default' !== $pob ) {
if ( 'popularity' === $woo_default_catalog_orderby || 'rating' === $woo_default_catalog_orderby ) {
WC()->query->remove_ordering_args();
}
}
$orderby = 'date';
$order = strtoupper( $po );
$meta_key = '';
to
// Remove posts_clause filter, if default ordering is set to rating or popularity to make custom ordering work correctly.
if ( 'default' !== $pob ) {
if ( 'popularity' === $woo_default_catalog_orderby || 'rating' === $woo_default_catalog_orderby ) {
WC()->query->remove_ordering_args();
}
}
$orderby = 'menu_order Date';
$order = strtoupper( $po );
$meta_key = '';
$orderby = 'menu_order Date'; instead of $orderby = 'date';
it's simply am forcing the avada to use my own custom ordering instead of date.
but that not really practical solution as i modified class-avada-woocommerce.php
is there any suggestion to do find solution by add some code to avada child them instead
There is nothing wrong with the Avada Code.
Avada intentionally unhooks the filter 'woocommerce_get_catalog_ordering_args' and writes its own filter "catalog_ordering" in
'/wp-content/themes/Avada/includes/class-avada-woocommerce.php'
That calls
'/wp-content/themes/Avada/templates/wc-catalog-ordering.php'
Please overwrite the template by using your child theme and copying the contents of wc-catalog-ordering.php.
i.e. "/wp-content/themes/Avada-Child-Theme/templates/wc-catalog-ordering.php"
You'll be able to handle the orders however you'd like once complete.
Previously mentioned, the function "remove_avada_function" is hooked on 'after_setup_theme' and is used to remove the hook. However since 'remove_avada_function' is called before the action 'pre_get_post' is called, 'remove_avada_function' won't remove the action that does not currently exist.
Update 2:
I think that if you add a highest priority than Avada's theme hook it will work (here I have set it to 100), and the only change to do in this function is $args['orderby'] = 'menu_order date'; to get the similar change you have done on Avada's files.
The code:
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 100 );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$args['orderby'] = 'menu_order date';
return $args;
}
This code goes in function.php file of your active child theme.
It should work
The very strange thing is that woocommerce_get_catalog_ordering_args is a filter hook, but NOT an action hook (so I have changed my code)…
Then something is wrong in the Avada's theme code and you should report it to the support.
You just need to increase your hooked function, so it will override Avada's:
add_action('after_setup_theme', 'remove_avada_function' );
function remove_avada_function(){
remove_action( 'woocommerce_before_shop_loop', 'catalog_ordering', 30 );
add_action( 'woocommerce_before_shop_loop', 'custom_catalog_ordering', 30 );
}
You can use this code to set Avada catalog sort hook back to woocommerce default.
function avada_remove_ordering() {
global $avada_woocommerce;
remove_action('pre_get_posts', array( $avada_woocommerce, 'product_ordering' ), 5);
add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
}
add_action( 'init', 'avada_remove_ordering' );
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' , 12);
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sale_price' == $orderby_value ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = '_sale_price';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby', 12 );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby', 12 );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['sale_price'] = 'Sale';
return $sortby;
}

How do I flip images on the homepage using "Woocommerce Product Image Flipper" plugin?

I am using the Wordpress plugin "Woocommerce Product Image Flipper" to flip images on hover.
It works perfectly on a product category page, but the not on the homepage.
How do I make the images flip on the homepage?
I am displaying the Homepage Template and it shows the following sections:
"New In" , "On Sale", "Bestsellers"
When I hover over them, I would like the images to flip like they do on the product category pages.
The code of the plugin is as follows:
<?php
/*
Plugin Name: WooCommerce Product Image Flipper
Plugin URI:
Version: 0.4.0
Description: Adds a secondary image on product archives that is revealed on hover. Perfect for displaying front/back shots of clothing and other products.
Author: jameskoster
Author URI:
Text Domain: woocommerce-product-image-flipper
Domain Path: /languages/
License: GNU General Public License v3.0
License URI:
*/
/**
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
/**
* Localisation (with WPML support)
*/
add_action( 'init', 'plugin_init' );
function plugin_init() {
load_plugin_textdomain( 'woocommerce-product-image-flipper', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Image Flipper class
*/
if ( ! class_exists( 'WC_pif' ) ) {
class WC_pif {
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'pif_scripts' ) );
add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'woocommerce_template_loop_second_product_thumbnail' ), 11 );
add_filter( 'post_class', array( $this, 'product_has_gallery' ) );
}
/**
* Class functions
*/
public function pif_scripts() {
if ( apply_filters( 'woocommerce_product_image_flipper_styles', true ) ) {
wp_enqueue_style( 'pif-styles', plugins_url( '/assets/css/style.css', __FILE__ ) );
}
}
public function product_has_gallery( $classes ) {
global $product;
$post_type = get_post_type( get_the_ID() );
if ( ! is_admin() ) {
if ( $post_type == 'product' ) {
$attachment_ids = $this->get_gallery_image_ids( $product );
if ( $attachment_ids ) {
$classes[] = 'pif-has-gallery';
}
}
}
return $classes;
}
/**
* Frontend functions
*/
public function woocommerce_template_loop_second_product_thumbnail() {
global $product, $woocommerce;
$attachment_ids = $this->get_gallery_image_ids( $product );
if ( $attachment_ids ) {
$attachment_ids = array_values( $attachment_ids );
$secondary_image_id = $attachment_ids['1'];
$secondary_image_alt = get_post_meta( $secondary_image_id, '_wp_attachment_image_alt', true );
$secondary_image_title = get_the_title($secondary_image_id);
echo wp_get_attachment_image(
$secondary_image_id,
'shop_catalog',
'',
array(
'class' => 'secondary-image attachment-shop-catalog wp-post-image wp-post-image--secondary',
'alt' => $secondary_image_alt,
'title' => $secondary_image_title
)
);
}
}
/**
* WooCommerce Compatibility Functions
*/
public function get_gallery_image_ids( $product ) {
if ( ! is_a( $product, 'WC_Product' ) ) {
return;
}
if ( is_callable( 'WC_Product::get_gallery_image_ids' ) ) {
return $product->get_gallery_image_ids();
} else {
return $product->get_gallery_attachment_ids();
}
}
}
$WC_pif = new WC_pif();
}
}

How to skip cart page on woocomerce for certain products only?

I added this to my functions.php file :
add_filter ('woocommerce_add_to_cart_redirect', 'woo_redirect_to_checkout');
function woo_redirect_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}
But now, all the products are re-directing strait to check-out. I would like to have this option only in one product. Is that a way I can add a product ID to that same filer?
Thank you!
You need to get the product when It is just added to cart , then check if for this product you want to redirect the cart page to checkout page . You need to change $desire_product = 'certain_product'; line on below code and It will definitely work.
add_filter( 'woocommerce_add_to_cart_redirect', 'woo_redirect_checkout' );
function woo_redirect_checkout() {
global $woocommerce;
$desire_product = 'certain_product';
//Get product ID
$product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
//Check if current product is subscription
if ( $product_id == $desire_product ){
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
exit;
} else {
$cart_url = $woocommerce->cart->get_cart_url();
return $cart_url;
exit;
}
}
I wrote a little plugin for this, sharing it here. The plugin adds a small checkbox to the product metabox, so you can specify which products should trigger the automatic skip to checkout. Basically using the same woocommerce_add_to_cart_redirect filter as in the other answers, but providing the admin backend option to determine which products trigger the redirection.
<?php
/**
* Plugin Name: Redirect to checkout
* Plugin URI: http://stackoverflow.com/q/32962653/383847
* Description: redirect to checkout for certain products
* Version: 1.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* Requires at least: 3.8
* Tested up to: 3.9
*
* Text Domain: kia-redirect-to-checkout
* Domain Path: /languages/
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/*
* Add text inputs to product metabox
*/
function kia_add_to_wc_metabox(){
global $post;
echo '<div class="options_group">';
// Suggested Price
echo woocommerce_wp_checkbox( array(
'id' => '_redirect_to_checkout',
'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' );
/*
* Save extra meta info
*/
function kia_process_wc_meta_box( $post_id, $post ) {
if ( isset( $_POST['_redirect_to_checkout'] ) ) {
update_post_meta( $post_id, '_redirect_to_checkout', 'yes' );
} else {
update_post_meta( $post_id, '_redirect_to_checkout', 'no' );
}
}
add_action( 'woocommerce_process_product_meta', 'kia_process_wc_meta_box', 1, 2 );
/*
* Redirect to checkout
*/
function kia_add_to_cart_redirect( $url ){
// If product is one of our special types
if ( is_numeric( $_REQUEST['add-to-cart'] ) && kia_maybe_redirect_cart( (int) $_REQUEST['add-to-cart'] ) ) {
// Remove default cart message
WC()->clear_messages();
// Redirect to checkout
$url = WC()->cart->get_checkout_url();
}
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect' );
/*
* check if an item has custom field
*/
function kia_maybe_redirect_cart( $product_id ){
if ( 'yes' == get_post_meta( $product_id, '_redirect_to_checkout', true ) ){
return TRUE;
} else {
return false;
}
}
Updating WooCommerce 3.0+
<?php
/**
* Plugin Name: WC Redirect to checkout
* Plugin URI: http://stackoverflow.com/q/32962653/383847
* Description: Redirect to checkout for certain products
* Version: 1.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* Requires at least: 3.8
* Tested up to: 3.9
* WC requires at least: 3.1.0
* WC tested up to: 4.0.1
*
* Text Domain: kia-redirect-to-checkout
* Domain Path: /languages/
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Add text inputs to product metabox
*/
function kia_add_to_wc_metabox(){
global $post;
echo '<div class="options_group">';
// Suggested Price
echo woocommerce_wp_checkbox( array(
'id' => '_redirect_to_checkout',
'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' );
/**
* Save extra meta info
*
* #param WC_Product $product
*/
function kia_process_wc_meta_box( $product ) {
if ( isset( $_POST['_redirect_to_checkout'] ) ) {
$product->update_meta_data( '_redirect_to_checkout', 'yes' );
} else {
$product->update_meta_data( '_redirect_to_checkout', 'no' );
}
}
add_action( 'woocommerce_admin_process_product_object', 'kia_process_wc_meta_box' );
/**
* Redirect to checkout
*
* #param WC_Product $product
*/
function kia_add_to_cart_redirect( $url, $product ) {
// If product is one of our special products.
if ( kia_maybe_redirect_cart( $product ) ) {
// Remove default cart message.
wc_clear_notices();
// Redirect to checkout.
$url = wc_get_checkout_url();
}
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect', 10, 2 );
/**
* Check if an item has custom field.
*
* #param WC_Product $product
*/
function kia_maybe_redirect_cart( $product ) {
return wc_string_to_bool( $product instanceof WC_Product && $product->get_meta( '_redirect_to_checkout', true ) );
}
https://gist.github.com/helgatheviking/f76b97d7d19813538e32b8f5f2dae6ec
There are a few action hooks as well that you can use, for eg: woocommerce_add_to_cart which passes the product id to the callback function:
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart', 10, 2 );
function custom_add_to_cart( $cart_item_key, $product_id ) {
// replace 123 with a valid product id
if( 123 == $product_id ) {
wp_redirect( WC()->cart->get_checkout_url() );
exit;
}
}

Categories