为elementor创建了一个自定义的小部件,可以使用 AJAX 一次将追加销售项目添加到购物车中。为此,我利用了wocoomerce追加销售产品的功能和折扣,我创建了ACF字段来保存优惠券信息,并将在结帐时应用。所有的前端工作正常,但当我点击elementor页面生成器更新500错误弹出,但更新发生。只是担心如果下面的代码有任何安全问题。
这是我的自定义控件php文件:
<?php
/**
* bundles item widgets
*
* Elementor bundles item widget to display in the single product page
*/
// namespace WPC\Widgets;
// use Elementor\Controls_Manager;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Elementor_Widget_3 extends \Elementor\Widget_Base
{
public function get_name() {
return 'Bundle item';
}
public function get_title() {
return esc_html__( 'Bundle item', 'custom-widget');
}
public function get_icon() {
return 'eicon-header';
}
public function get_custom_help_url() {}
public function get_categories() {
return ['basic'];
}
public function get_keywords() {}
public function get_script_depends() {
wp_register_script( 'widget-script-3', '/wp-content/themes/astra-child/custom-widgets/custom-widgets-assets/js/custom-bundle-item.js', __FILE__ );
return
[
'widget-script-3'
];
}
public function get_style_depends() {
wp_register_style( 'widget-style-3', '/wp-content/themes/astra-child/custom-widgets/custom-widgets-assets/css/custom-bundle-item.css', __FILE__ );
return
[
'widget-style-3'
];
}
protected function register_controls() {
$this -> start_controls_section(
'section_content',
[
'label'=> 'settings',
]
);
$this -> add_control(
'label_heading',
[
'label' => 'Label Heading',
'type' => \Elementor\Controls_Manager::TEXT,
'default' => 'Bundles items'
]
);
$this->end_controls_section();
}
protected function render() {
global $product;
$product_id = get_the_ID($product);
$product = wc_get_product($product_id);
$coupons_value = get_field('bundle_discount', $product_id);
$product_coupons = $coupons_value['value'];
$product_coupon = $coupons_value['label'];
$upsells = $product->get_upsell_ids();
if(!empty($upsells)){
?>
<div class="bundled-upsell">
<p class="bundle-title"> Save More With Bundles</p>
<div class="bundled-images">
<?php
$total = $product->get_price();//current product price.
?>
<div class="custom-item-bundles">
<?php
echo $product->get_image();
?>
<span>A$ <?php echo $product->get_price(); ?></span>
<label>
<input type="checkbox" class="selected-item" data-product-quantity="1" data-product-id="<?php echo $product_id ?>" checked>
This item: <strong><?php echo $product->get_title() ?></strong>
</label>
</div>
<div class="plus-sign">+</div>
<?php
foreach($upsells as $upsell)
{
$up = wc_get_product($upsell);
$price = $up->get_price();
$image = $up->get_image();
$title = $up->get_title();
$item_link = $up->get_permalink();
$total += $price;
?>
<div class="custom-item-bundles">
<a href="<?php echo $item_link ?>"><?php echo $image?></a>
<span>A$ <?php echo $price ?></span>
<label>
<input type="checkbox" class="selected-item" data-product-quantity="1" data-product-id="<?php echo $up->get_id()?>" checked>
<a href="<?php echo $item_link ?>"><?php echo $title?></a>
</label>
</div>
<?php
if($upsell !== end($upsells))
{
?><div class="plus-sign">+</div><?php
}
}
$final_total = round(($total - $total*($product_coupon/100)), 2);
?>
</div>
<div class="bundled-total">
<div class="final-price">
Was <span class="initial-price"> A$<?php echo $total?></span>
<strong>(<?php echo $product_coupon?>% OFF)</strong><br>
<span> Bundled Offer: A$<?php echo $final_total ?></span>
</div>
<span> ⚡ Discount will be applied on checkout ! ⚡</span>
<button class="add-to-cart-bundled" id="add-to-cart-bundle">Add to cart</button>
</div>
</div>
<div class="bundled-item-ids" data-product-profile="<?php echo $product_coupons ?>"></div>
</div>
<?php
}
else
{
;
}
}
protected function content_template() {}
}
这是我 AJAX 文件:
// Multi Ajax adda to cart
$ = jQuery;
function multiAddToCart( bundle ) {
$.ajax({
url: '/wp-admin/admin-ajax.php/',
type: 'POST',
dataType: 'JSON',
data: {
'action': 'multi_add_to_cart',
'discount': coupons,
'items' : Object.assign({}, bundle)
},
success : function(response) {
$(document.body).trigger('wc_fragment_refresh'); // Refresh cart fragments
},
beforeSend: function (response) {
$('#add-to-cart-bundle').html("Adding to cart");
},
complete: function (response) {
$('#add-to-cart-bundle').html("Added to cart").prop('disabled', true).removeClass('add-to-cart-bundled').addClass('added-to-cart-bundled');
},
error : function(error) {
console.log(error);
}
});
}
var itemCoupons = $('.bundled-item-ids').data('product-profile');
var coupons = itemCoupons.toString();
$( '#add-to-cart-bundle' ).on('click', function() {
bundle = [];
$('.selected-item').each(function(){
if($(this).prop('checked')== true)
{
var cart_item = $(this).data('product-id');
var cart_quantity = $(this).data('product-quantity');
bundle.push({ product_id: cart_item, quantity: cart_quantity });
}
})
multiAddToCart( bundle);
});
这是我的服务器端脚本
<?php
add_action('wp_ajax_multi_add_to_cart', 'multi_ajax_add_to_cart');
add_action('wp_ajax_nopriv_multi_add_to_cart', 'multi_ajax_add_to_cart');
function multi_ajax_add_to_cart() {
if (isset($_POST['items']) ) {
$items = $_POST['items'];
$discount = $_POST['discount'];
if ( ! is_array( $items ) || empty( $items ) ) {
wp_send_json_error( 'Invalid items list' );
}
// Add each item to the cart
foreach ( $items as $item ) {
$product_id = absint( $item['product_id'] );
$quantity = absint( $item['quantity'] );
// Validate the product ID
if ( ! $product_id ) {
continue;
}
// Add the item to the cart
WC()->cart->add_to_cart( $product_id, $quantity);
}
$previous_coupons = WC()->cart->get_applied_coupons();
WC()->cart->remove_coupons($previous_coupons);
// Apply the discount code
$result = WC()->cart->apply_coupon($discount);
if ( $result === true ) {
// Discount applied successfully
wc_add_notice( __( 'Discount code applied successfully.', 'woocommerce' ) );
}
else {
// Handle error message
wc_add_notice( __( 'Invalid discount code.', 'woocommerce' ), 'error' );
}
// Get the updated cart fragments
$fragments = WC()->cart->get_cart_for_session();
// Send the response
wp_send_json_success( array(
'fragments' => $fragments,
'cart_hash' => WC()->cart->get_cart_hash(),
) );
}
die();
}
我确实尝试增加内存限制,但没有运气,我也有其他自定义小部件工作正常。
最后这是我的错误日志:
PHP Fatal error: Uncaught Error: Call to a member function get_upsell_ids() on bool in /var/www/html/site/public_html/wp-content/themes/astra-child/custom-widgets/custom-bundle-item.php:84\nStack trace:\n#0 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/base/controls-stack.php(2268): Elementor_Widget_3->render()\n#1 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/base/widget-base.php(609): Elementor\\Controls_Stack->render_by_mode()\n#2 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/base/widget-base.php(673): Elementor\\Widget_Base->render_content()\n#3 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/db.php(202): Elementor\\Widget_Base->render_plain_content()\n#4 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/db.php(208): Elementor\\DB->render_element_plain_content(Array)\n#5 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/db.php(208): Elementor\\DB->render_element_plain_content(Array)\n#6 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/db.php(558): Elementor\\DB->render_element_plain_content(Array)\n#7 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/db.php(539): Elementor\\DB->get_plain_text_from_data(Array)\n#8 /var/www/html/site/public_html/wp-content/plugins/elementor/includes/db.php(230): Elementor\\DB->get_plain_text(732)\n#9 /var/www/html/site/public_html/wp-content/plugins/elementor/core/base/document.php(1230): Elementor\\DB->save_plain_text(732)\n#10 /var/www/html/site/public_html/wp-content/plugins/elementor/core/base/document.php(742): Elementor\\Core\\Base\\Document->save_elements(Array)\n#11 /var/www/html/site/public_html/wp-content/plugins/elementor/core/documents-manager.php(510): Elementor\\Core\\Base\\Document->save(Array)\n#12 [internal function]: Elementor\\Core\\Documents_Manager->ajax_save(Array, Object(Elementor\\Core\\Common\\Modules\\Ajax\\Module))\n#13 /var/www/html/site/public_html/wp-content/plugins/elementor/core/common/modules/ajax/module.php(172): call_user_func(Array, Array, Object(Elementor\\Core\\Common\\Modules\\Ajax\\Module))\n#14 /var/www/html/site/public_html/wp-includes/class-wp-hook.php(308): Elementor\\Core\\Common\\Modules\\Ajax\\Module->handle_ajax_request('')\n#15 /var/www/html/site/public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)\n#16 /var/www/html/site/public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)\n#17 /var/www/html/site/public_html/wp-admin/admin-ajax.php(188): do_action('wp_ajax_element...')\n#18 {main}\n thrown in /var/www/html/site/public_html/wp-content/themes/astra-child/custom-widgets/custom-bundle-item.php on line 84, referer: https://evpowerhouse523.e.wpstage.net/wp-admin/post.php?post=732&action=elementor
[Sat Jan 21 13:00:10.019386 2023] [php:warn] [pid 1383] [client 49.12.33.216:54300] PHP Warning: Attempt to read property "id" on null in /var/www/html/site/public_html/wp-content/plugins/woo-product-attachment/admin/class-woocommerce-product-attachment-admin.php on line 462, referer: https://evpowerhouse523.e.wpstage.net/wp-admin/post.php?post=732&action=elementor
[Sat Jan 21 13:00:10.054040 2023] [php:warn] [pid 1383] [client 49.12.33.216:54300] PHP Warning: Attempt to read property "id" on null in /var/www/html/site/public_html/wp-content/plugins/woo-product-attachment/admin/class-woocommerce-product-attachment-admin.php on line 462, referer: https://evpowerhouse523.e.wpstage.net/wp-admin/post.php?post=732&action=elementor
[Sat Jan 21 13:00:10.299971 2023] [php:warn] [pid 1383] [client 49.12.33.216:54300] PHP Warning: Trying to access array offset on value of type null in /var/www/html/site/public_html/wp-content/themes/astra-child/custom-widgets/custom-bundle-item.php on line 80, referer: https://evpowerhouse523.e.wpstage.net/wp-admin/post.php?post=732&action=elementor
[Sat Jan 21 13:00:10.299993 2023] [php:warn] [pid 1383] [client 49.12.33.216:54300] PHP Warning: Trying to access array offset on value of type null in /var/www/html/site/public_html/wp-content/themes/astra-child/custom-widgets/custom-bundle-item.php on line 81, referer: https://evpowerhouse523.e.wpstage.net/wp-admin/post.php?post=732&action=elementor`
1条答案
按热度按时间rwqw0loc1#
试着在wp-config中启用错误日志记录,并查看错误是什么。您可以通过更改以下内容来启用:
然后检查在wp-content中创建的错误日志文件。
正如您所建议的,这可能是Elementor的内存限制,我会在php.ini中设置为512 M。