I keep getting this error Fatal error: Call to a member function get_cart() on a non-object in...on line 1113 Here is my full code
custom_dashboard.php
require_once(dirname(__FILE__) . '/../../../wp-load.php');
global $woocommerce;
woocommerce_mini_cart();
line 1113 is located in functions.php
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 0 );
function add_custom_price() {
global $woocommerce;
$loop = 0;
$item_data = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//content
}
}
How do i solve? I want to show the shopping cart on custom_dashboard.php.
Check for the following condition first.
if ( sizeof( WC()->cart->get_cart() ) > 0 )
so after adding this condition your code will look like
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 0 );
function add_custom_price() {
global $woocommerce;
$loop = 0;
$item_data = array();
$valid_for_cart = false;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//content
}
}
}
Related
I have tried to hide a certain subcategory from the category widget on shop page and other pages, there is a category widget.
I found that the code below works for all pages except the shop page. If I change is_product_category() for the is_shop(), so it works for the shop page, but not for the other pages.
How can I do that it works for ALL pages (shop and all others)?
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_product_category() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'seinakellad','nastennye-chasy','wall-clock' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
Just remove the additional check in your if condition:
add_filter( 'get_terms', 'filter_get_terms', 10, 3 );
function filter_get_terms( $terms, $taxonomies, $args ) {
$new_terms = [];
// if a product category and on the shop page
if ( ! is_admin() ) {
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, [ 'seinakellad', 'nastennye-chasy', 'wall-clock' ] ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
I want it to automatically switch to the special order status when an order is placed from the product category I have determined in the Woocommerce. How should the code be, can you help me?
For example; If a product from the Personalized Products category is in the order, I want it to automatically switch to "wc-prepare" status.
Thank you
Untested code, but should do the trick
add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order ){
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( has_term( 'special-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e special-flowers
$order->update_status( 'wc-prepare' ); // your status here
break;
}
}
}
if you wanna do this for multiple cats
add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order ){
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( has_term( 'special-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e special-flowers
$order->update_status( 'wc-prepare' ); // your status here
break;
} else if ( has_term( 'cheap-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e cheap-flowers
$order->update_status( 'wc-prepare' ); // your status here
break;
}
}
}
or if you wanna get fancy you can check if they have items from multiple cats in their cart
add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order ){
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( has_term( 'special-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e special-flowers
if ( has_term( 'cheap-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e cheap-flowers
$order->update_status( 'wc-prepare' ); // your status here
break;
}
}
}
}
again, this is untested, but I believe it should work!
I am creating a plugin where I need to update the input field post meta when the actual post is updated. So here is my code:
function save_meta_function ( ) {
global $post;
$post_id = $post->ID;
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );
but it's showing several errors:
Notice: Trying to get property of non-object // that's $post_id
= $post->ID;
Can you tell me why $post_id = $post->ID; line showing that error?
Replace your code with follows -
function save_meta_function ( $post_id ) {
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );
Actually, I have a Woocommerce website which has approximately 800 products.
And many of the products are variable products and each product have some attributes, which are used as a variation, but the problem is many products have assigned unused attributes, which are not used in that particular product variations.
so I want to remove unused attributes from each product, which are not used in the variation of that particular product.
Looking for a query or some code snippet that will help me, rather than to check each and every product.
for now, I have no idea how to do this.
I just want to remove un-used attributes, which are not used in that product variations.
Try running this script: https://gist.github.com/yratof/f21242d4263c461f4a5b6b766cd24373
add_action( 'init', function() {
ini_set( 'memory_limit', '2048M' );
set_time_limit( 0 );
$posts = get_posts( [
'post_type' => 'product',
'posts_per_page' => -1
] );
$count = 0;
foreach ( $posts as $post ) {
$product = get_product( $post );
if ( $product->product_type !== 'variable' ) {
continue;
}
$count ++;
$va = $product->get_variation_attributes();
$vas = [];
foreach ( $product->get_attributes() as $attribute ) {
if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) {
$terms = wp_get_post_terms( $product->id, $attribute['name'] ) ;
// var_dump( $terms );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $va[ $attribute['name'] ] ) ) {
// var_dump( $term );
if ( ! isset( $vas[$attribute['name']] ) ) {
$vas[$attribute['name']] = [];
}
$vas[$attribute['name']][] = $term->term_id;
}
}
}
}
foreach ($vas as $tax => $vals) {
wp_set_post_terms( $product->id, $vals, $tax );
}
}
wp_die( 'All attributes have been filtered: Total products changed: '. $count );
} );
I'm trying to add a custom field on each product on cart. I got the code from
WooCommerce: Add input field to every item in cart
but the update is not working. I tried to change the global $woocommerce to WC() but still it gives me 500 (Internal Server Error) on chrome console everytime I click update. heres the code:
cart.php:
<td class="product-url">
<?php
$html = sprintf( '<div class="url"><input type="text" name="cart[%s][url]" value="%s" size="4" title="Url" class="input-text url text" /></div>', $cart_item_key, esc_attr( $values['url'] ) );
echo $html;
?>
function.php (This is not a snippet so do not run):
// get from session your URL variable and add it to item
add_filter('woocommerce_get_cart_item_from_session', 'cart_item_from_session', 99, 3);
function cart_item_from_session( $data, $values, $key ) {
$data['url'] = isset( $values['url'] ) ? $values['url'] : '';
return $data;
}
// this one does the same as woocommerce_update_cart_action() in plugins\woocommerce\woocommerce-functions.php
// but with your URL variable
// this might not be the best way but it works
add_action( 'init', 'update_cart_action', 9);
function update_cart_action() {
global $woocommerce;
if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['url'] ) ) {
$woocommerce->cart->cart_contents[ $cart_item_key ]['url'] = $cart_totals[ $cart_item_key ]['url'];
}
}
}
}
}
// this is in Order summary. It show Url variable under product name. Same place where Variations are shown.
add_filter( 'woocommerce_get_item_data', 'item_data', 10, 2 );
function item_data( $data, $cart_item ) {
if ( isset( $cart_item['url'] ) ) {
$data['url'] = array('name' => 'Url', 'value' => $cart_item['url']);
}
return $data;
}
// this adds Url as meta in Order for item
add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
function add_item_meta( $item_id, $values ) {
woocommerce_add_order_item_meta( $item_id, 'Url', $values['url'] );
}