{"id":1057,"date":"2024-04-16T20:04:34","date_gmt":"2024-04-16T11:04:34","guid":{"rendered":"http:\/\/inwelly.com\/?p=1057"},"modified":"2024-04-23T05:33:22","modified_gmt":"2024-04-22T20:33:22","slug":"wordpress-woocommerce-add-to-cart","status":"publish","type":"post","link":"https:\/\/inwelly.com\/?p=1057","title":{"rendered":"wordpress Woocommerce &#8211; add to cart"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>public&nbsp;function&nbsp;setQuickOrder()<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    {\n        $jsonId     = $_POST['data_id']['myDataId'];\n        $jsonQty    = $_POST['data_qty']['myDataQty'];\n        \n        $myDataIdArray  = json_decode($jsonId,true);\n        $myDataQtyArray = json_decode($jsonQty,true);\n\n        foreach ($myDataIdArray as $key =&gt; $value)\n        {\n            WC()-&gt;cart-&gt;add_to_cart( $value , $myDataQtyArray[$key] );\n        }\n\n        $specific_ids = array($myDataIdArray);\n        foreach( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item )\n        {\n            $product_id = $cart_item['data']-&gt;get_id();\n\n            $findIdx = array_search($product_id,$myDataIdArray);\n\n\n            if( in_array( $product_id, $specific_ids[0] ) &amp;&amp; $cart_item['quantity'] != $myDataQtyArray[$findIdx] )\n            {\n                WC()-&gt;cart-&gt;set_quantity( $cart_item_key, $myDataQtyArray[$findIdx] ); \/\/ Change quantity\n            }\n        }\n        wp_die();\n    }<\/pre>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">            $this-&gt;add_action(\n                'wp_ajax_setquickorder',\n                'QuickOrderController@setQuickOrder'\n            );\n\n            $this-&gt;add_action(\n                'wp_ajax_nopriv_setquickorder',\n                'QuickOrderController@setQuickOrder'\n            ); <\/pre>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">&lt;script&gt;\n\nlet proidArray = [];\nlet proqtyArray = [];\n\nfunction cart_action_ajax()\n{\n    jQuery.ajax({\n    url: '\/wp-admin\/admin-ajax.php', \/\/just hard coded to see\n    type : 'post',\n        data:\n        {\n            'action' : 'setquickorder',\n            'data_id' : { myDataId : JSON.stringify(proidArray) },\n            'data_qty' : { myDataQty : JSON.stringify(proqtyArray) },\n        },\n        success: function(data)\n        {\n            jQuery(document.body).trigger('wc_fragment_refresh');              alert('Items successfully added to cart');              \/\/ console.log(data); \n        }\n    });   \n}\n\nfunction each_cart_action(proId)\n{\n    proidArray = [];\n    proqtyArray = [];\n\n    proidArray.push(proId);\n\n    if( document.getElementById(\"bsf_qty_\"+proId).value )\n    {\n        let bsfqtyNo = document.getElementById(\"bsf_qty_\"+proId).value;\n        let numberBsfqty = bsfqtyNo * 1 ;\n        if( numberBsfqty &gt; 0 )\n        {\n            proqtyArray.push(numberBsfqty);\n        }\n        else\n        {\n            return;\n        }\n    }\n    else\n    {\n        return;\n    }\n    cart_action_ajax();\n}\n\nfunction bulk_cart_action()\n{\n    proidArray = [];\n    proqtyArray = [];\n    let j = 0 ;\n\n    const elements = document.getElementsByClassName(\"variation-quantity-value\");\n\n    for (let i = 0; i &lt; elements.length; i++)\n    {\n        if( elements[i].value )\n        {\n            let numberBsfqty = elements[i].value * 1 ;\n            if( numberBsfqty &gt; 0 )\n            {\n                let productid = elements[i].getAttribute(\"data-product-id\");\n                let proId = productid * 1 ;\n                proidArray.push(proId);\n                proqtyArray.push(numberBsfqty);\n            }\n        j++;\n        }\n    }\n\n    if( j == 0 )\n    {\n        return;\n    }\n    else\n    {\n        cart_action_ajax();\n    }\n}\n\n&lt;\/script&gt;<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>public&nbsp;function&nbsp;setQuickOrder() { $jsonId = $_POST[&#8216;data_id&#8217;][&#8216;myDataId&#8217;]; $jsonQty = $_POST[&#8216;data_qty&#8217;][&#8216;myDataQty&#8217;]; $myDataIdArray = json_decode($jsonId,true); $myDataQtyArray = json_decode($jsonQty,true); foreach ($myDataIdArray as $key =&gt; $value) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1250,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109],"tags":[],"class_list":["post-1057","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1057"}],"version-history":[{"count":1,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1057\/revisions"}],"predecessor-version":[{"id":1058,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1057\/revisions\/1058"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/media\/1250"}],"wp:attachment":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}