Error when trying create WooCommerce subscription as admin - php

I am trying to manually add a subscription in WooCommerce and keep getting this error.
I have located the PHP file but am unsure of the error. Any help would be appreciated. Thank you.
Fatal error: Uncaught Error: [] operator not supported for strings
in /nas/content/live/montkdev/wp-content/plugins/woocommerce-subscriptions/includes/data-stores/class-wcs-customer-store-cached-cpt.php on line 128
Call stack:
WCS_Customer_Store_Cached_CPT::add_subscription_id_to_cache()
wp-content/plugins/woocommerce-subscriptions/includes/data-stores/class-wcs-customer-store-cached-cpt.php:236
WCS_Customer_Store_Cached_CPT::maybe_update_for_post_meta_change()
wp-includes/class-wp-hook.php:303
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:327
WP_Hook::do_action()
wp-includes/plugin.php:470
do_action()
wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-post-meta-cache-manager.php:234
WCS_Post_Meta_Cache_Manager::trigger_update_cache_hook()
wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-post-meta-cache-manager.php:220
WCS_Post_Meta_Cache_Manager::maybe_trigger_update_cache_hook()
wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-post-meta-cache-manager.php:124
WCS_Post_Meta_Cache_Manager::meta_updated_with_previous()
wp-includes/class-wp-hook.php:303
WP_Hook::apply_filters()
wp-includes/plugin.php:189
apply_filters()
wp-includes/meta.php:205
update_metadata()
wp-includes/post.php:2335
update_post_meta()
wp-content/plugins/woocommerce/includes/data-stores/class-wc-data-store-wp.php:254
WC_Data_Store_WP::update_or_delete_post_meta()
wp-content/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php:214
WC_Order_Data_Store_CPT::update_post_meta()
wp-content/plugins/woocommerce-subscriptions/includes/data-stores/class-wcs-subscription-data-store-cpt.php:218
WCS_Subscription_Data_Store_CPT::update_post_meta()
wp-content/plugins/woocommerce/includes/data-stores/abstract-wc-order-data-store-cpt.php:173
Abstract_WC_Order_Data_Store_CPT::update()
wp-content/plugins/woocommerce-subscriptions/includes/data-stores/class-wcs-subscription-data-store-cpt.php:186
WCS_Subscription_Data_Store_CPT::update()
wp-content/plugins/woocommerce/includes/class-wc-data-store.php:179
WC_Data_Store::update()
wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php:185
WC_Abstract_Order::save()
wp-content/plugins/woocommerce/includes/class-wc-order.php:221
WC_Order::save()
wp-content/plugins/woocommerce-subscriptions/includes/admin/meta-boxes/class-wcs-meta-box-subscription-data.php:375
WCS_Meta_Box_Subscription_Data::save()
wp-includes/class-wp-hook.php:303
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:327
WP_Hook::do_action()
wp-includes/plugin.php:470
do_action()
wp-content/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php:222
WC_Admin_Meta_Boxes::save_meta_boxes()
wp-includes/class-wp-hook.php:305
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:327
WP_Hook::do_action()
wp-includes/plugin.php:470
do_action()
wp-includes/post.php:4468
wp_insert_post()
wp-includes/post.php:4570
wp_update_post()
wp-admin/includes/post.php:423
edit_post()
wp-admin/post.php:227

WCS has this issue in the function in the line $subscription_ids[] = $subscription_id; .
/**
* Add a subscription ID to the cached subscriptions for a given user.
*
* #param int $user_id The user the subscription belongs to.
* #param int $subscription_id A subscription to link the user in the cache.
*/
protected function add_subscription_id_to_cache( $user_id, $subscription_id ) {
$subscription_ids = $this->get_users_subscription_ids( $user_id );
if ( ! in_array( $subscription_id, $subscription_ids ) ) {
$subscription_ids[] = $subscription_id;
$this->update_subscription_id_cache( $user_id, $subscription_ids );
}
}
Try adding the below code snippet while adding the subscription if the issue persists.
remove_all_actions('wcs_update_post_meta_caches');

Related

Error in Wordpress - elementor after ugrading PHP 7.4

After upgrading to PHP 7.4, elemntor started giving error
url: www.aldon.ltd/
Cloud Service: GCP
Server: Openlitespeed Wordpress
Linux: Ubuntu 20.04 LTS
Notice: Trying to access array offset on value of type null in /var/www/html/wp-content/plugins/elementor/includes/base/controls-stack.php on line 1449
Notice: Trying to access array offset on value of type null in /var/www/html/wp-content/plugins/elementor/includes/base/controls-stack.php on line 1451
Except this notice shown at the top of webpage, all other functions are working as expected.
Code causing error
/**
* End controls section.
*
* Used to close an existing open controls section. When you use this method
* it stops adding new controls to this section.
*
* This method should be used inside _register_controls().
*
* #since 1.4.0
* #access public
*/
Public function end_controls_section() {
$stack_name = $this->get_name();
// Save the current section for the action.
$current_section = $this->current_section;
$section_id = $current_section['section'];
$args = [
'tab' => $current_section['tab'],
];
Error Log at /usr/local/lsws/logs/error.log
PHP Notice: wp_deregister_script was called incorrectorrectly. Do not deregister the jquery-core script in the administration area. To target the front-end theme, use the wp_enqueue_scrip>
PHP Notice: /var/www/html/wp-content/plugins/elementor/includes/base/controls-stack.php on line 1449
PHP Notice: /var/www/html/wp-content/plugins/elementor/includes/base/controls-stack.php on line 1451
I resolved it for a temporary solution. Changing line no. 1204 from
if ( $dynamic_property ) ) {
to
if ( $dynamic_property && isset($settings[ $control_name ][ $dynamic_property ]) ) {

Get random posts through WordPress API

I am building a self-test project which can give 10 questions at one time from a question list. I want the 10 questions should be different every time I start the test. The front-end is React and the back-end is WordPress by using WordPress API.
Previously I used orderby=rand in the query by implementing a plug-in
<?php
/**
* Plugin Name: REST API - Post list randomize
* Description: Randomize the content list in REST API passing `orderby=rand` as parameter.
* Version: 1.0.0
* Author: Felipe Elia | Codeable
* Author URI: https://codeable.io/developers/felipe-elia?ref=qGTOJ
*/
/**
* Add `rand` as an option for orderby param in REST API.
* Hook to `rest_{$this->post_type}_collection_params` filter.
*
* #param array $query_params Accepted parameters.
* #return array
*/
function add_rand_orderby_rest_post_collection_params( $query_params ) {
$query_params['orderby']['enum'][] = 'rand';
return $query_params;
}
add_filter( 'rest_post_collection_params', 'add_rand_orderby_rest_post_collection_params' );
It worked perfectly until 2 weeks ago. Without modifying any code, it was just broken. I used Postman to test, such as http://localhost/wp/wp-json/wp/v2/questions?per_page=10&orderby=rand. The response is
"code": "rest_invalid_param",
"message": "Invalid parameter(s): orderby",
"data": {
"status": 400,
"params": {
"orderby": "orderby is not one of author, date, id, include, modified, parent, relevance, slug, include_slugs, title."
}
}
Two weeks ago if I used the same query, it could give me 10 random questions. It looks like the plug-in cannot add rand successfully as a parameter for orderby in WordPress like before.
BTW, the functionality of orderby=rand in WP isn't broken because if I manually add rand as a parameter in WP core code, the above query can work again.
Does anybody know what's wrong with the plug-in or some latest updates in WP causing the problem?
Another thing is I saw some articles mentioning ORDERBY = RAND() in MySQL will affect the performance severely when the database is large. So I wonder whether I should use orderby=rand in the query to get random questions or think about other ways to do the job. Does anybody have any suggestions for this performance issue? Thanks!
Found the answer. I need to make the first parameter rest_post_collection_params of add_filter function to the correspondent post type.
The original rest_post_collection_params is for the WP default posts type. Because I use ACF(Advanced Custom Fields, another plug-in to create customized post types) to create my own post types, such as books, I need to change the first parameter to rest_books_collection_params. If you have more customized post types, just create as many as add_filter functions for each of them.
Just have no ideas why I could use rest_post_collection_params for all my customized post types 3 weeks ago but not now. Anyway, since it is solved, don't bother. My project is more front-end oriented. WP is just storage.
BTW, probably someone has noticed that rest_post_collection_params is for the WP default post type posts. In the parameter it uses single form post for the plural form posts. This only works for the WP default post type. For customized types, if it is books, the parameter should be rest_books_collection_param; if questions, then rest_questions_collection_param. Keep the parameter exactly the same as the post type.
For Getting Unique Questions:
table: wp_question_filter_list
id ip_address question_list
1 1.21.23 1,2,3,4,5
2 1.21.24 1,4,6,7,8
3 1.21.25 4,5,6,8,9
function get_unique_questions($ip_address){
global $wpdb;
$table_name = $wpdb->prefix . 'question_filter_list';
$unique_id_list=$wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE ip_address=%d",$ip_address), ARRAY_A);
if (count($unique_id_list) > 0) {
$question_data=$unique_id_list[0];
$arr_question=array();
if(isset($question_data['question_list']) && $question_data['question_list']!=''){
$arr_old_question_id_list=explode(",",$question_data['question_list']);
}
$excludes = implode(',', $arr_old_question_id_list);
$arr_question_id_list=$wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE ip_address='".$ip_address."' AND id NOT IN('".$excludes."') "), ARRAY_A);
set_unique_questions($ip_address,$arr_question_id_list);
}
function set_unique_questions($ip_address,$arr_question_id_list){
global $wpdb;
$table_name = $wpdb->prefix . 'question_filter_list';
$unique_id_list=$wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE ip_address=%d",$ip_address), ARRAY_A);
if (count($unique_id_list) > 0) {
$question_data=$unique_id_list[0];
$arr_question=array();
if(isset($question_data['question_list']) && $question_data['question_list']!=''){
$arr_old_question_id_list=explode(",",$question_data['question_list']);
}
if(is_array($arr_question_id_list) && !empty($arr_question_id_list)){
foreach($arr_question_id_list as $single_question){
array_push($arr_question,$single_question);
}
}
$str_question_id_list=implode(",",$arr_question);
$wpdb->update($table_name, array(
'question_list' => $str_question_id_list
), array(
'ip_address' => $ip_address
));
}else{
$str_question_id_list=implode(",",$arr_question_id_list);
$data = array(
'ip_address' => $ip_address,
'question_list' => $str_question_id_list,
);
$wpdb->insert($table_name, $data);
}
$result = $wpdb->insert($table_name, $item);
}
$ip_address=$_SERVER['REMOTE_ADDR'];
$arr_question_list=get_unique_questions($ip_address);
echo "<pre>";
print_r($arr_question_list);

Magento: White page on order view page

Posted this on http://magento.stackexchange.com but I haven't gotten an answer.
I am getting this error for a while and I am clueless to what is wrong with it. Whenever I click the view order page, I get a blank page. Now I installed some code on my index.php that displays the errors.
This is the error that I get:
Array ( [type] => 1 [message] => Class
'Mage_Sales_Block_Order_Info' not found [file] =>
/app/code/core/Mage/Core/Model/Layout.php [line] => 491 )
This is line 491 (and context)
/**
* Create block object instance based on block type
*
* #param string $block
* #param array $attributes
* #return Mage_Core_Block_Abstract
*/
protected function _getBlockInstance($block, array $attributes=array())
{
if (is_string($block)) {
if (strpos($block, '/')!==false) {
if (!$block = Mage::getConfig()->getBlockClassName($block)) {
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
}
}
if (class_exists($block, false) || mageFindClassFile($block)) {
$block = new $block($attributes);
}
}
if (!$block instanceof Mage_Core_Block_Abstract) {
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
}
return $block;
}
Seems nothing is wrong there (and why should it, its magento default code)
Now the next part is somewhat strange. I expected the class to live in app/code/core/Mage/Sales/Block/Order/Info I looked it up. The folders Order/Info didnt exist in my folder. Nor in the fresh install zip.
What is going on and more importantly, how can I fix this?
I hope you are using latest version of magento never mind even if it is old looks like you are missing some order block files try copying all the files properly. some times you may miss some files while you download the files from magento website check your magento zip file size that you downloaded.

when using add_user_to_blog getting error "Call to undefined function get_userdata"

I am working on WordPress MU and trying to build one plugin to add user to multiple sites. so far did everything to loop through sites. But while assigning user to site as below using add_user_to_blog am getting error see below.
add_user_to_blog( $blogid, $amsuserid, $urole );
Getting the following error:
Fatal error: Call to undefined function get_userdata() in wp-includes\ms-functions.php on line 181
if I disable the line "add_user_to_blog" no errors.
If the user is not on the blog, it will error out due to line 184 in ms-functions.php.
$user = get_userdata( $user_id );
if ( ! $user ) {
restore_current_blog();
return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
}
So you should check to see if the user exist on the blog by using username_exist method, if they do not exist call wp_create_user. http://codex.wordpress.org/Function_Reference/wp_create_user

How to send new password from Magento CSV import of Customers

We have been trying to modify the Customer.php file ( import/export ) to get it to send out the new account details automatically as it imports the customers from a CSV file.
We are working in the right area as we dropped a simple mail() call which was called (we received the emails) for every new row. The problem arises when trying to get it to generate a new random password and send out the new account details - it never sends any mail and we cannot figure out why! Code follows ( Edited from app/code/local/Mage/ImportExport/Model/Import/Entity/Customer.php )
/**
* Update and insert data in entity table.
*
* #param array $entityRowsIn Row for insert
* #param array $entityRowsUp Row for update
* #return Mage_ImportExport_Model_Import_Entity_Customer
*/
protected function _saveCustomerEntity(array $entityRowsIn, array $entityRowsUp)
{
if ($entityRowsIn) {
$this->_connection->insertMultiple($this->_entityTable, $entityRowsIn);
// BEGIN: Send New Account Email
$cust = Mage::getModel('customer/customer');
$cust->setWebsiteId(Mage::app()->getWebsite()->getId());
foreach($entityRowsIn as $idx => $u){
// Failed
$cust->loadByEmail($u['email']);
$cust->setConfirmation(NULL);
$cust->setPassword($cust->generatePassword(8));
$cust->save();
$cust->sendNewAccountEmail();
//$cust->sendPasswordReminderEmail(); // this call doesnt work either
}
// END: Send New Account Email
}
if ($entityRowsUp) {
$this->_connection->insertOnDuplicate(
$this->_entityTable,
$entityRowsUp,
array('group_id', 'store_id', 'updated_at', 'created_at')
);
}
return $this;
}
Inorder to improve performance magento 'caches' object, so when trying to load an object in a loop you either need to load a new instance or call it reset() method
$website_id = Mage::app()->getWebsite()->getId();
foreach($entityRowsIn as $idx => $u){
$cust = Mage::getModel('customer/customer');
$cust->setWebsiteId($website_id);
$cust->loadByEmail($u['email']);
$cust->setPassword($cust->generatePassword(8));
$cust->save();
$cust->sendNewAccountEmail(); // if you are getting 2 email then remove this line
}
If you are load a large number of customers then you may run in to memory issues and my need to look a other technique using reset()
If this code is being run from the Administration section of Magento then you will need to make sure that you set the correct Website ID ( Mage::app()->getWebsite()->getId() will return the incorrect one ).
From the front end this code will work fine but for Admin work you should use '1' or whatever your Front end websites ID is as the getId() method was returning '0' in the Admin area! Small gotcha but had me going for hours!
I had found this script on how to Auto generate password for existing customers
$passwordLength = 10;
$customers = Mage::getModel('customer/customer')->getCollection();
foreach ($customers as $customer){
$customer->setPassword($customer->generatePassword($passwordLength))->save();
$customer->sendNewAccountEmail();
}

Categories