PHP Warning: in_array() expects parameter 2 to be array, null given - php

Please help. I cannot figure this out, and granted I am just learning php
I tried to add code to an existing woocommerce function, to remove a payment gateway if a group of products were in the cart.
I think the 4th line from the bottom "added for auto pay if statement" is the issue here. Thanks in advance.
public function is_available() {
//added for autopay 1 line
global $woocommerce;
$is_available = parent::is_available();
//added for autopay 2 lines
foreach ($woocommerce->cart->cart_contents as $key => $values )
$autopays = array(26108,30619,35613);
// don't show on checkout page
if ( ! $this->is_express_checkout() && is_checkout() && ! $this->show_on_checkout() ) {
$is_available = false;
}
// don't display when order review table is rendered via AJAX
if ( ! $this->is_express_checkout() && defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) && $_POST['action'] == 'woocommerce_update_order_review' && ! $this->show_on_checkout() ) {
$is_available = false;
}
// don't show on checkout > pay page
if ( is_checkout_pay_page() ) {
$is_available = false;
}
// don't show if the cart contains a subscription and manual renewals are not enabled
if ( $this->get_plugin()->is_subscriptions_active() && WC_Subscriptions_Cart::cart_contains_subscription() && 'no' === get_option( WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals', 'no' ) ) {
$is_available = false;
}
//added for auto pay if statement
if(in_array($values['product_id'],$autopays)){$is_available = false;
}
return $is_available;
}

The syntax for foreach is
foreach (array_expression as $key => $value) {
statement1...
statement2...
}
In your code, you're missing the opening and closing brackets after the foreach statement.

Related

WordPress doesn't read if my checkbox is checked

I need to add an additional product to the cart. I have differents options to choose so I asign one checkbox to everyone of them. Then, depends the checkbox clicked it should add the right product to the cart. The problem begans when in my function it's not detected if the checkbox is checked, I have an if() but always returns the 'else'.
It should add the additional product if the checkbox is checked but it does not. The code to add the product works correctly
add_action('woocommerce_add_to_cart', 'custom_add_to_cart');
function custom_add_to_cart() {
$id = get_the_ID();
if($id = '147430'){
global $woocommerce;
$product_id_mes = 147054;
$product_id_anual = 147295;
$found = false;
if(isset($_POST['checkmes']) && $_POST['checkmes'] == 'Si'){
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id_mes )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id_mes );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id_mes );
}
} else{
//if 'checkmes' isn't checked don't add the additional product
}
}
}
There are 2 problems in your code:
if($id = '147430') here you're doing the assignment. you need to use == loose comparison or === strict comparison.
You should not use the get_the_ID() function for getting the product ID, you need to use 2nd param of action which is $product_id
Other suggestions:
global $woocommerce; line if not required to add since you're using the WC() function not the global $woocommerce object.
use count() instead of sizeof function.

Sanitizing the code as per wpcs for yoda condition checks

i am trying to cleanup the file with phpcs --standard=Wordpress ../filename.php . But it seems phpcbf is unable to perform the action. So, can anyone share some help as how can i manually fix these doc comment and yoda errors. Attached is related chunk of code and the snip of the actual php file and the errors encountered. Any help would be highly appreciated.
<?php
function is_blog() {
return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag() ) && 'post' == get_post_type();
}
?>
<?php do_action( 'generate_before_footer' ); ?>
<div <?php generate_footer_class(); ?>>
<?php
do_action( 'generate_before_footer_content' );
// Get how many widgets to show.
$widgets = generate_get_footer_widgets();
if ( ! empty( $widgets ) && 0 !== $widgets ) :
// Set up the widget width.
$widget_width = '';
if ( $widgets == 1 ) {
$widget_width = '100';
}
if ( $widgets == 2 ) {
$widget_width = '50';
}
if ( $widgets == 3 ) {
$widget_width = '33';
}
if ( $widgets == 4 ) {
$widget_width = '25';
}
if ( $widgets == 5 ) {
$widget_width = '20';
}
?>
Above the is_blog() function, you'll need to add a docblock:
/**
* Function comment here.
*
*/
function is_blog() {
}
Yoda conditions are when you write the value to compare against before the variable you are comparing. So instead of:
if ( $widgets == 1 ) {
$widget_width = '100';
}
It wants you to write:
if ( 1 == $widgets ) {
$widget_width = '100';
}
Changing those things should get the file passing the checks.

woocommerce override of product

I have a problem with the following code when I make the price variable the function doesn't work but when I hardcode a price it works. (note I am in a testing face and haven't worried about security)
this, down't work:
function add_custom_price( $cart_obj ) {
global $product, $woocommerce ,$wpdb;
if ( isset( $_POST['fra'] ) ){
$GLOBALS['$_fra'] = urldecode( $_POST["fra"] );
} else {
$GLOBALS['$_fra'] = "";
}
if ( isset( $_POST['til'] ) ){
$GLOBALS['$_til'] = urldecode($_POST["til"]);
} else {
$GLOBALS['$_til'] = "";
}
if ( $GLOBALS['$_til'] !="" && $GLOBALS['$_fra'] !="" ){
$t1 = urldecode( $GLOBALS['$_til'] );
$t2 = urldecode( $GLOBALS['$_fra']);
$objArray = $wpdb->get_results("SELECT $t1 FROM test_priser WHERE city = '$t2'");
if ( isset($objArray[0]->$t1) ){
$priser = explode("/",$objArray[0]->$t1);
if ( !isset($priser[1]) ){
$priser[1] = intval($priser[0]);
}
}
echo "priser[1] = ".$priser[1]; //this outputs the expected value
}
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
foreach ( $cart_obj->get_cart() as $key => $value ) {
$value['data']->set_price( $priser[1] );
}
}
When I add this line, and override the variable, it works:
$priser[1] = 1000;
I am quite confused as to what the problem might be.
for anyone else who might stumble upon this. Seems I need to pass the variable price in the session then it works.
you can find the answer here: custom price on woocommerce product

Disabling Amazon Payments Advanced method from WooCommerce checkout page

I'm trying to disable a couple of payment gateways based on a user's role. The function & hook I found works on the Paypal method but not Amazon Payments Advanced. Here's my code:
function wk_disable_gateways( $available_gateways ) {
global $woocommerce;
$wholesale_cust = check_user_role( array( 'wholesale', 'orig-wholesale' ) );
if ( isset( $available_gateways['paypal'] ) && $wholesale_cust ) {
unset( $available_gateways['paypal'] );
}
if ( isset( $available_gateways['amazon_payments_advanced'] ) && $wholesale_cust ) {
unset( $available_gateways['amazon_payments_advanced'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'wk_disable_gateways' );
The "Pay with Amazon" code is still running on the checkout page. Any ideas?
This is not the best solution, but I have not been able to find a more viable answer.
First off I did what you did and disabled all gateways expect the one I wanted the user to use. In my case I only want someone to check out with the nmigateway.
This goes inside of your theme functions.php
add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
foreach ( $gateways as $gateway_key => $gateway ) {
if ( $gateway_key !== 'nmipay' ) {
unset( $gateways[ $gateway_key ] );
}
}
}
}
return $gateways;
}
Next here is the part that makes this not the best solution editing the plugins source code.
Change the following two functions inside of the plugins/woocommerce-gateway-amazon-payments-advanced/amazon-payments-advanced.php
/**
* Checkout Button
*
* Triggered from the 'woocommerce_proceed_to_checkout' action.
*/
function checkout_button() {
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
$disable_button = true;
}
}
if(!isset($disable_button) && $disable_button !== true ){
?><div id="pay_with_amazon"></div><?php
}
}
/**
* Checkout Message
*/
function checkout_message() {
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
$disable_button = true;
}
}
if(!isset($disable_button) && $disable_button !== true ){
if ( empty( $this->reference_id ) ) {
echo '<div class="woocommerce-info info"><div id="pay_with_amazon"></div> ' . apply_filters( 'woocommerce_amazon_pa_checkout_message', __( 'Have an Amazon account?', 'woocommerce-gateway-amazon-payments-advanced' ) ) . '</div>';
}
}
}
Keep in mind that when the plugin is updated all of your changes will be lost.

Woocommerce: add free product if cart has 3 items

What I'm trying to do is to add free product if user have 3 product in cart.
I choosed woocommerce_add_cart_item hook for this.
Here is my code :
add_filter('woocommerce_add_cart_item', 'set_item_as_free', 99, 1);
function set_item_as_free($cart_item) {
global $woocommerce;
$products_with_price = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if($values['data']->price == 0) {
continue;
} else {
$products_with_price++;
}
}
if( $products_with_price > 1 && $products_with_price % 3 == 1) {
$cart_item['data']->set_price(0);
return $cart_item;
}
return $cart_item;
}
I also tried $cart_item['data']->price = 0; but it doesn't work out either :(
Is there is something I do wrong or maybe there is some other way to get this done?
Thanks.
Try this modified code: (Have changed the condition.)
add_filter('woocommerce_add_cart_item', 'set_item_as_free', 99, 1);
function set_item_as_free($cart_item) {
global $woocommerce;
$products_with_price = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if($values['data']->price == 0) {
continue;
} else {
$products_with_price++;
}
}
if( $products_with_price >= 3 && $products_with_price % 3 == 1) {
$cart_item['data']->set_price(0);
return $cart_item;
}
return $cart_item;
}
If you don't want to add a free product again after a user purchases another 3 products above the existing cart then remove " && $products_with_price % 3 == 1" from the last condition.
Do not use the woocommerce_add_cart_item hook to set the price of the product, a lot of plugin like to refetch the price of the product in the database in the hook woocommerce_before_calculate_totals (that's the case of WPML/WCML)
So instead use this hook
add_action( 'woocommerce_before_calculate_totals', function ( $cart_obj ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// Avoiding hook repetition (when using price calculations for example)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) {
return;
}
// Loop through cart items
foreach ( $cart_obj->get_cart() as $cart_item ) {
if ( isset( $cart_item['free_item'] ) ) {
$cart_item['data']->set_price( 0 );
}
}
} );

Categories